SA-MP Forums Archive
Return a float value - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Return a float value (/showthread.php?tid=151827)



Return a float value - Badger(new) - 01.06.2010

Hello I'm having some problem returning a float value. This function gets the distance from a player to a point using x,y and z:
pawn Код:
stock GetDistance(playerid,Float:X,Float:Y,Float:Z){
    new Float:distancea,Float:distanceb;
    new Float:PX,Float:PY,Float:PZ;
    GetPlayerPos(playerid,PX,PY,PZ);
    new Float:distx=X-PX;
    new Float:disty=Y-PY;
    new Float:distz=Z-PZ;
    distancea=floatsqroot((distx*distx)+(disty*disty));
    distanceb=floatsqroot((distz*distz)+(distancea*distancea));
    return distanceb;
}
The problem is using the distance float value.

If I set a float's value to the returned value, I get
1107778176.000000 instead of 33.838558.
pawn Код:
new Float:test=GetDistance(playerid...coods...);// 1107778176.000000
If I put printf("%f",distanceb); before the return, I get 33.838558 printed.
pawn Код:
distanceb=floatsqroot((distz*distz)+(distancea*distancea));
    printf("%f",distanceb);//33.838558
    return distanceb;
}
What is wrong?


Re: Return a float value - coole210 - 01.06.2010

Код:
return Float:distanceb;



Re: Return a float value - Simon - 01.06.2010

No, your function definition should include Float: i.e. stock Float:GetDistance(...), you'll need to forward the function also afaik


Re: Return a float value - coole210 - 01.06.2010

Forward a stock? lol


Re: Return a float value - Badger(new) - 01.06.2010

Thanks. Changing it to Float:GetDistance worked.

But yeah, "stock" means that it doesn't need to be forwarded or used.


Re: Return a float value - smeti - 01.06.2010

pawn Код:
return _:distanceb;



Re: Return a float value - Stepashka - 01.06.2010

Код:
Float:GetDistance(playerid,Float:x,Float:y,Float:z){
	new Float:px,Float:py,Float:pz;
	GetPlayerPos(playerid,px,py,pz);
	return = floatsqroot( floatpower(px-x,2.0) + floatpower(py-y,2.0) + floatpower(pz-z,2.0) );
}



Re: Return a float value - Simon - 12.06.2010

It's important to forward tagged functions if you use the function before it's defined (this doesn't matter for untagged)... If you don't forward it before use in this scenario then you will get "warning 208: function with tag result used before definition, forcing reparse".

An example is the following script, if you don't forward it you will get a warning when you compile. If you put the line "forward MyFunc();" before it's used then all will be fine.

pawn Код:
#include <a_samp>

main() {
  printf("%f", MyFunc());
}

stock Float:MyFunc()
  return 13.37;