Help with retrieving a float from PAWN
#1

Hey! I was wondering if any of you would have an answer to my slight problem that I've been having, I've researched around, tried many different methods but I can't seem to retrieve a float from PAWN to my plugin, and let me explain how I try...

So, let's say I have the native function set up like so...
pawn Code:
native TestFunction({Float,_}:...);
pawn Code:
static cell AMX_NATIVE_CALL TestFunction(AMX *amx, cell *params)
{
    float returnedfloat = amx_ctof(params[1]);
    logprintf("Float parameter: %f", returnedfloat);
    return 1;
}
And then I call it from PAWN in my gamemode like so...

pawn Code:
public OnGameModeInit()
{
    TestFunction(69.345);
    return 1;
}
When I run the gamemode, it prints out "Float parameter: 0.00000" instead of my float, but if I have the native set up like this...

pawn Code:
native TestFunction(Float:fparam);
It will print out my float fine on runtime...

Is there any specific reason for this? Am I using a wrong method to get the parameters? Keep in mind I need the first method of declaring the native in PAWN.

EDIT: Also, I've managed to print out a string, integer and character from the first native method, it's just the floats that are not working.
Reply
#2

Code:
static cell AMX_NATIVE_CALL TestFunction(AMX *amx, cell *params)
{
    float returnedfloat = params[1];
    logprintf("Float parameter: %f", returnedfloat);
    return amx_ctof(params[1]);
}
Return the parameter value in floating number.
Reply
#3

Quote:
Originally Posted by oOFotherOo
View Post
Code:
static cell AMX_NATIVE_CALL TestFunction(AMX *amx, cell *params)
{
    float returnedfloat = params[1];
    logprintf("Float parameter: %f", returnedfloat);
    return amx_ctof(params[1]);
}
Return the parameter value in floating number.
I used the amx_ctof function for a reason, "params" is a cell, not a float, but amx_ctof (amx_celltofloat) converts the cell to a float. I knew that this wasn't going to work and tried it anyways, didn't work.

EDIT:

Ahh hell yes, found a method to get the float argument properly!

pawn Code:
cell GetArg(AMX* amx, cell param)
{
    cell *xx;
    amx_GetAddr(amx, param, &xx);
    return xx[0];
}
Usage (in my case):
pawn Code:
static cell AMX_NATIVE_CALL TestFunction(AMX *amx, cell *params)
{
    cell returnedcell = GetArg(amx, params[1]);
    logprintf("params[1] = %f", amx_ctof(returnedcell));
    return 1;
}
Hope this helps someone having the same problems as me, g'day.
Reply
#4

if you want to return a float value from a cell you need to use amx_ftoc (float to cell, because the return type of the natives are CELLS!) and NOT amx_ctof !
It's that little difference everybody can have some problems with, it's worth to make a #define so you won't make these mistakes.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)