PDA

View Full Version : Forwarding Function to Callback


Faskis
19/11/2012, 12:22 AM
I'm trying to write a function that grabs a player's username based on their MySQL ID (reference). The issue that I'm having with it is that it appears that the initial function isn't forwarding to the callback and is simply returning on the query.


Here are the functions/callback:

stock GetPlayerSQLName(id)
{
new query[128];
format(query, sizeof(query), "SELECT `Username` FROM `accounts` WHERE `id` = %d", id);
mysql_function_query(MainPipeline, query, true, "OnUsernameGet", "i", id);
printf(query);
return 1;
}

forward OnUsernameGet(id);
public OnUsernameGet(id)
{
new rows, fields, name[24];
cache_get_data(rows, fields, MainPipeline);
if(rows)
{
cache_get_field_content(0, "Username", name, MainPipeline);
ReturnName(name);
printf(name);
}
}

stock ReturnName(name[])
{
printf(name);
new string[24];
return string;
}


And here's is an example command I made for testing this:

CMD:farva(playerid, params[])
{
new string[48];
format(string, sizeof(string), "- %s", GetPlayerSQLName(GetPlayerSQLId(playerid)));
SendClientMessage(playerid, COLOR_GRAD2, string);
printf(string);
return 1;
}


Here's what it's outputting:

http://puu.sh/1r4BU


That third line is what's outputting from the command, which leads me to believe that, because it's outputting that before the callback output, it's simply not forwarding to the callback so it won't display the correct result. If anyone could provide some assistance on this, that would be greatly appreciated.

Scott
19/11/2012, 02:00 AM
mysql_function_query creates a threaded query call. The callback that you specify is where the MySQL plugin will return the result of the query after it is finished while the main SA-MP thread continues to run.

It appears based on this:

format(string, sizeof(string), "- %s", GetPlayerSQLName(GetPlayerSQLId(playerid)));


That you are expecting the players username to return immediately within the format function. What will be returned is "1" because that is what you are returning within the GetPlayerSQLName function. Here is an example based on what I think it is that you are wanting to do:


stock GetPlayerSQLName(id)
{
new query[128];
format(query, sizeof(query), "SELECT `Username` FROM `accounts` WHERE `id` = %d", id);
mysql_function_query(MainPipeline, query, true, "OnUsernameGet", "i", id);
return 1;
}

forward OnUsernameGet(id);
public OnUsernameGet(id)
{
new rows, fields, name[24];
cache_get_data(rows, fields, MainPipeline);
if(rows)
{
cache_get_field_content(0, "Username", name, MainPipeline);
format(string, sizeof(string), "- %s", name);
SendClientMessage(id, COLOR_GRAD2, string);
printf(string);
}
}

CMD:farva(playerid, params[])
{
GetPlayerSQLName(GetPlayerSQLId(playerid));
return 1;
}