Zeex / sampgdk

Write SA-MP gamemodes in C/C++
http://zeex.github.io/sampgdk
Apache License 2.0
153 stars 83 forks source link

[ Question ] How to determine the data type of a variable? #204

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hello I have a question on the type of data

In the "OnPublicCall" callback, there is an argument "params", which contains an array of parameters, but there is no information about the data type of the parameter

How to determine the data type of a variable?

Example

Code:

PLUGIN_EXPORT bool PLUGIN_CALL OnPublicCall(AMX *amx, const char *name, cell *params, cell *retval) {
    int argc = params[0] / sizeof(cell);
    std::cout << name << '(';
    for (int i = 0; i < argc; i++)
    {
        std::cout << "PARAM #" << i << ":" << params[i+1] << ", ";
    }
    std::cout << ')' << std::endl;
    return true;
}

Ouput:

OnGameModeInit()
OnIncomingConnection(PARAM #0:0, PARAM #1:0, PARAM #2:50665, )
OnPlayerConnect(PARAM #0:0, )
OnPlayerRequestClass(PARAM #0:0, PARAM #1:0, )
OnPlayerRequestClass(PARAM #0:0, PARAM #1:0, )
OnPlayerRequestSpawn(PARAM #0:0, )
OnPlayerStateChange(PARAM #0:0, PARAM #1:8, PARAM #2:0, )

In OnIncomingConnection(PARAM #0:0, PARAM #1:0, PARAM #2:50665, ) second params (PARAM#1) must be string, but I don't know how to determine this

Zeex commented 5 years ago

You need to know the types of parameters in advance, they can't be determined at runtime. This is why functions like SetTimerEx and CallLocalFunction have a string parameter that describes the types of arguments passed to them.

For example, you could make a map of callbacks where keys are function names and values are parameter info, and the look up that info based on the name parameter.

ghost commented 5 years ago

@Zeex ok, thank you

I am create a callback map