Return a float value
#1

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?
Reply
#2

Код:
return Float:distanceb;
Reply
#3

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

Forward a stock? lol
Reply
#5

Thanks. Changing it to Float:GetDistance worked.

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

pawn Код:
return _:distanceb;
Reply
#7

Код:
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) );
}
Reply
#8

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;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)