[House] Get New House ID
#1

Hi,

I'm trying to get the ID of a new house and I did this:


Код:
stock GetNewHouseID()
{
	new query[128];
	mysql_format(mysql, query, sizeof(query), "SELECT * FROM `maisons`");
	mysql_tquery(mysql, query, "OnGetHouseID", "");
	return 1;
}

public OnGetHouseID()
{
	new rows, fields; 
    cache_get_data(rows, fields, mysql);
    if(rows)
    {
		new id = cache_get_row_count();
		return id; // Here it gives me the good ID.
    }
	return ID_MAISON_INVALIDE;
}

In my createhouse CMD, the World of the house = the new ID.


So I got something like this:

mInfo[m][mWorld] = GetNewHouseID();

But it always give me 1 as ID.

I think that is cause I did "return 1".
But how could I return the value from my public ?
I mean "return id;" but from my public.


Thank you !
Reply
#2

The public is called by the mysql plugin.
Returning anything in that function returns your data to the plugin, not your script.

AFAIK, you should always "return 1;" in those callbacks to inform the plugin to clear the cache.

I think you need to store the result (ID) in a global variable and read that variable where you need it.

Are you trying to determine which ID to use for a new house you create with a command like /createhouse?
Personally, I would load all houses during OnGameModeInit using non-threaded queries (to block the server from being opened before all houses are loaded) and store all data in an array.

Then you could check which index is not used yet (no data) in the array and create your house there, set default data and use an INSERT query to save it to MySQL.
For this to work, you'll need to set the ID column to NOT use auto_increment, as the ID of the house would be the index in the array.
Reply
#3

You will need something like this. NOTE, dont use this until u've edited it to your configuration, e.g: YIni or MySQL
pawn Код:
stock GetNextHouseID()
{
for(new i = 0; i < MAX_HOUSES; i++)
{
if(fexist(HousePath(i))) continue;
return i;
}
return -1;
}
Reply
#4

Thank you both of you, I think that I m gonna create a global variable then see if it works.

I have auto increment in my database for the ID, so I just have to "cache_get_row_count()" and I have the next ID.
And then I will use it to set it as world for the house.


I think that doing something like this is faster than what you proposed me.
And I pref this way, but if It fails, I will try what you told me to do.


Thank you guys, I will try this tomorrow and see
Reply
#5

Hi

So I tried to do what you told me and everything works EXCEPT for the 2 first ID's ( 0 and 1)

This is the code:



Код:
(( Top of my script I have new idMaison;
ID_MAISON_INVALIDE is defined to 0))

stock GetNewHouseID()
{
	new query[128];
	mysql_format(mysql, query, sizeof(query), "SELECT * FROM `maisons`");
	mysql_tquery(mysql, query, "OnGetHouseID", "");
	return 1;
}

public OnGetHouseID()
{
	new rows, fields, id;
    cache_get_data(rows, fields, mysql);
    if(rows)
    {
		id = cache_get_row_count();
		printf("Il y a %d maison", cache_get_row_count());
		idMaison = id;
		printf("Il y a dйjа des maison, le futur ID est: %d", id);
		return id;
    }
	id = ID_MAISON_INVALIDE;
	idMaison = id;
	return id;
}
The part of my createhouse CMD:

Код:
new id = GetNewHouseID();
		mInfo[id][mWorld] = idMaison;
format(string, sizeof(string), "La maison ID %d a bien йtй crйe.", mInfo[id][mWorld]);
		SendClientMessage(playerid, -1, string);
But when I create a house, I got something like this:
House ID 0 have been created.
House ID 0 have been created.
House ID 1 have been created.
House ID 2 have been created.
House ID 3 have been created.

So the second house created always have id equal 0.



I'm still trying to correct this but I have no ideas right now.
Could somebody help me with this please ?

Thank you !



EDIT: I modified my cmd but it still the same,

Код:
GetNewHouseID();
		new id = idMaison;
Reply
#6

When working with threaded functions you've got to smartly produce everything so it always keeps running on the thread you assign it to:

If you have a CMD which requieres data from the SQL to continue, you've got to continue the CMD in the function you've threaded the CMD into, you can never rely nor it will work if you call a threaded function to retrieve a value since threaded functions run in a different thread (hence the name) and may finish during or after the execution of the whole command.

This is an example of using threaded data with cmds, see how I continue the command in the function I thread it to?

pawn Код:
CMD:bans(playerid, params[])
{
    if(GetPVarInt(playerid,"AccountLevel") < 5 && !IsPlayerAdmin(playerid))
        return 0;

    if(!isnull(params)) return Usage(playerid,"/bans");

    mysql_format(dbHandle, query, 128, "SELECT * FROM `bans` WHERE `ban_status` = 1  ORDER BY `ban_id`");
    mysql_tquery(dbHandle, query, "OnPlayerRequestListBans", "i", playerid);
   
    SetPVarInt(playerid, "listviewingmax", 10);
    SetPVarInt(playerid, "listviewingmin", 0);
    return 1;
}

forward OnPlayerRequestListBans(playerid);
public OnPlayerRequestListBans(playerid)
{
    new rows, fields, characterstr[1536], result[24];
    cache_get_data(rows, fields);
    if(rows)
    {
        new internal, type, title[64], temp;
        new bans = GetPVarInt(playerid,"listviewingmin");
        SetPVarInt(playerid, "listviewingtotal", rows);
        for(new i = 0; i < 10; i ++)
            listban[playerid][i] = -1;
       
        while(bans < rows && bans < GetPVarInt(playerid,"listviewingmax"))
        {
            temp = cache_get_field_content_int(bans, "ban_id");
            format(characterstr, 1536, "%s{FF6600}ID: {FFFFFF}%02d",characterstr,temp);
            listban[playerid][internal] = temp;


            type = cache_get_field_content_int(bans, "type");
            format(characterstr, 1536, "%s\t{FF6600}Type: {FFFFFF}%s",characterstr, GetBanTypeForDialogStr(type));

            if(type == 1) // char
            {
                cache_get_field_content(bans, "char_name", result);
                format(characterstr, 1536, "%s\t{FF6600}Character: {FFFFFF}%s",characterstr, result);
            }
            else if(type == 2) // account
            {
                cache_get_field_content(bans, "account_name", result);
                format(characterstr, 1536, "%s\t{FF6600}Account: {FFFFFF}%s",characterstr, result);
            }
            else if(type == 4) //route
            {
                cache_get_field_content(bans, "ip", result);
                format(characterstr, 1536, "%s\t{FF6600}IP: {FFFFFF}%s\t",characterstr, result);
            }
            else //ip/full
            {
                cache_get_field_content(bans, "ip", result);
                format(characterstr, 1536, "%s\t{FF6600}IP: {FFFFFF}%s",characterstr, result);
            }
           
            cache_get_field_content(bans, "admin", result);
            format(characterstr, 1536, "%s\t{FF6600}Issuer: {FFFFFF}%s",characterstr, result);
           
            format(characterstr, 1536, "%s\n",characterstr);
            bans ++;
            internal ++;
        }

        if(rows > 10)
        {
            new Float:totalpages;
            new Float:currentpage;

            totalpages = floatround(rows/10.0, floatround_ceil);
            currentpage = floatround(GetPVarInt(playerid,"listviewingmax")/10.0, floatround_ceil);
       
            format(title, sizeof(title),"{FFFFFF}Click the ban for more information. {BBBBBB}(%d/%d)", floatround(currentpage), floatround(totalpages));
            ShowPlayerDialog(playerid, 900, DIALOG_STYLE_LIST, title,characterstr,"Expand","Next >");
        }
        else
        {
            ShowPlayerDialog(playerid, 900, DIALOG_STYLE_LIST, "Select the list item to view ban information",characterstr,"Expand","Exit");
        }
    }
    else
    {
        TDInfo(playerid, "No active bans.");
        return 1;
    }

    return 1;
}
Reply
#7

Man OMG.

I was trying and trying for about 6 hours now, thank you !

Finally it worked !

I wanted to rep you, but I already done it in the past and right now I can't do it again.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)