SFML community forums

Help => Graphics => Topic started by: Aerine on April 24, 2017, 12:42:11 am

Title: How do you return a vector2f from a function.
Post by: Aerine on April 24, 2017, 12:42:11 am
So im trying to return a vector2f from a function. So i can use it in a function in another class that takes a vector2f.


if i have the function

template <typename t, typename u>
        sf::Vector2f checkDirection(std::vector<t>  &vtower, std::vector<u>  &vmonster)
        {
                for (int i = 0; i < vmonster.size(); i++)
                {
                        for (int k = 0; k < vtower.size(); k++)
                        {
                                if (vmonster[i].getPositionX() + vmonster[i].getRadius() + vtower[k].getRadius() > vtower[k].getTowerRangePositionX()
                                        && vmonster[i].getPositionX() < vtower[k].getTowerRangePositionX() + vmonster[i].getRadius() + vtower[k].getRadius()
                                        && vmonster[i].getPositionY() + vmonster[i].getRadius() + vtower[k].getRadius() > vtower[k].getTowerRangePositionY()
                                        && vmonster[i].getPositionY() < vtower[k].getTowerRangePositionY() + vmonster[i].getRadius() + vtower[k].getRadius())
                                {
                                        return direction(vmonster[i].getPositionX(), vmonster[i].getPositionY());
                                       
                                       
                                }

                        }
                }
        }

Im trying to return a vector2f so i can use it in this function here

for (int i = 0; i < bullet.size(); i++)
                {
                       
                        bullet[i].fire(function.checkDirection(myicetower, mymonsters)); //fire takes a vector2f
                }
 

Title: Re: How do you return a vector2f from a function.
Post by: eXpl0it3r on April 24, 2017, 02:07:03 am
The third line misses a semicolon and what's Classname?
Title: Re: How do you return a vector2f from a function.
Post by: Aerine on April 24, 2017, 02:17:49 am
I updated my question to be alittle more specific sorry
Title: Re: How do you return a vector2f from a function.
Post by: eXpl0it3r on April 24, 2017, 02:33:43 am
And what is function now?
Title: Re: How do you return a vector2f from a function.
Post by: Aerine on April 24, 2017, 02:51:48 am
My function that returns a sf::vector2f is from the functions class. I simple have created a object called function to use it.

In my game class im tryiing to use this function to return a vector2f so i know where my bullet needs to go. However i get the error term does not evaluate to a function taking two arguments.
Title: Re: How do you return a vector2f from a function.
Post by: Hapax on April 24, 2017, 12:23:28 pm
Is checkDirection a free function? If so, you don't need the "function." prefix.

Remember too that checkDirection needs to always return a value regardless of whether or not that if condition becomes true.
Title: Re: How do you return a vector2f from a function.
Post by: Evaghetti on April 26, 2017, 12:43:30 am
is checkDirection static? if so you should call it with function::checkDirection instead of function.checkDirection
(also, if function is the name of a object use the class' name instead)