Explode mysql string
#1

Hello, I'm currently revamping the furniture system on my gamemode. So, first of all, I would like that the players will be able to texture these furnitures. I understood that there are a maximum of 5 indexes (for some objects) available.

Now, I would like that every index to be textured like modelid|txdname|texturename|matcolor (e.g. 18202|w_towncs_t|hatwall256hi|0) but I can't seem to understand how to explode these "|".

Current furniture loading system:
Код:
stock LoadHouseFurnitures(houseid)
{
	format(query, sizeof(query), "SELECT * FROM `furnitures` WHERE `houseid` = %d", HouseInfo[houseid][hID]);
	mysql_function_query(MO_ROLEPLAY_DB_HANDLE, query, true, "OnFurnituresLoad", "i", houseid);
	return 1;
}

....after hundreds of lines

public OnFurnituresLoad(houseid)
{
    new rows, fields;
	new total = 0;
    cache_get_data(rows, fields);
    if(rows)
    {
		while(total < rows)
		{
			FurnitureInfo[houseid][total][fID] = cache_get_row_int(total, 0);
			FurnitureInfo[houseid][total][fModel] = cache_get_row_int(total, 1);
			cache_get_row(total, 2, FurnitureInfo[houseid][total][fName], MODERN_ROLEPLAY_DB_HANDLE, 128);
			FurnitureInfo[houseid][total][fHouseID] = houseid;
			FurnitureInfo[houseid][total][fInterior] = cache_get_row_int(total, 4);
			FurnitureInfo[houseid][total][fVirtualWorld] = cache_get_row_int(total, 5);
			FurnitureInfo[houseid][total][fMarketPrice] = cache_get_row_int(total, 6);
			FurnitureInfo[houseid][total][fPosX] = cache_get_row_float(total, 7);
			FurnitureInfo[houseid][total][fPosY] = cache_get_row_float(total, 8);
			FurnitureInfo[houseid][total][fPosZ] = cache_get_row_float(total, 9);
			FurnitureInfo[houseid][total][fPosRX] = cache_get_row_float(total, 10);
			FurnitureInfo[houseid][total][fPosRY] = cache_get_row_float(total, 11);
			FurnitureInfo[houseid][total][fPosRZ] = cache_get_row_float(total, 12);
			FurnitureInfo[houseid][total][fOn] = 1;
			FurnitureInfo[houseid][total][fObject] = CreateDynamicObject(FurnitureInfo[houseid][total][fModel], FurnitureInfo[houseid][total][fPosX], FurnitureInfo[houseid][total][fPosY], FurnitureInfo[houseid][total][fPosZ], FurnitureInfo[houseid][total][fPosRX], FurnitureInfo[houseid][total][fPosRY], FurnitureInfo[houseid][total][fPosRZ], FurnitureInfo[houseid][total][fVirtualWorld], FurnitureInfo[houseid][total][fInterior], -1, 200.0);
			if(isHouseDoor(FurnitureInfo[houseid][total][fModel]))
			{
				FurnitureInfo[houseid][total][fLocked] = 1;
				FurnitureInfo[houseid][total][fOpened] = 0;
			}
			total++;
		}
    }
    return 1;
}
Reply
#2

Bump?
Reply
#3

Using sscanf.

Or a simple function:
pawn Код:
stock strsplit(dest[][], string[], const sub[], bool:ignorecase = false, pos = 0, limit = -1, len = sizeof (dest[]))
{
    if (pos <= -1)
        return 0;

    if (limit != -1 && limit <= 0)
        return 0;

    new sublen = strlen(sub);
    new count;

    while ((pos = strfind(string, sub, ignorecase, pos)) != -1)
    {
        strmid((dest[count][0] = EOS, dest[count]), string, 0, (pos + sublen), len);
        strdel(string, 0, (pos + sublen));

        count++;
        if (limit > 0 && count >= limit)
        {
            break;
        }
    }

    strcat((dest[count][0] = EOS, dest[count]), string, len);

    return count;
}
Example:
pawn Код:
new dest[5][128];

for (new i, j = strsplit(dest, your_string, "|"); i < j; i++)
{
    // dest[i] <- is your "i"th part of string
}
Reply
#4

Quote:
Originally Posted by Gammix
Посмотреть сообщение
Using sscanf.

Or a simple function:
pawn Код:
stock strsplit(dest[][], string[], const sub[], bool:ignorecase = false, pos = 0, limit = -1, len = sizeof (dest[]))
{
    if (pos <= -1)
        return 0;

    if (limit != -1 && limit <= 0)
        return 0;

    new sublen = strlen(sub);
    new count;

    while ((pos = strfind(string, sub, ignorecase, pos)) != -1)
    {
        strmid((dest[count][0] = EOS, dest[count]), string, 0, (pos + sublen), len);
        strdel(string, 0, (pos + sublen));

        count++;
        if (limit > 0 && count >= limit)
        {
            break;
        }
    }

    strcat((dest[count][0] = EOS, dest[count]), string, len);

    return count;
}
Example:
pawn Код:
new dest[5][128];

for (new i, j = strsplit(dest, your_string, "|"); i < j; i++)
{
    // dest[i] <- is your "i"th part of string
}
lol i thought sscanf is way harder to use before i read the topic. I'm so idiot. Thank you
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)