Loop to spawn all vehicles
#1

I dont know how to make it spawn all the vehicles in the database...

Could someone please help me?

I am trying to make a vehicle system for my gamemode and I have made the whole saving system for the vehicles. Just need them all to load and spawn. It loads the first vehicle in the database but not the rest, because I dont know how to make it loop through them all.

Here is my loading function:
Код:
#define SCRIPT_CARS 1999

stock Load_Vehicles()
{
	new Query[512], savestr[128], rows, fields;
	mysql_format(dbHandle,Query, sizeof(Query), "SELECT * FROM `Vehicles`");
	mysql_query(dbHandle,Query);
	cache_get_data(rows, fields);
	if(rows)
	{
		
		cache_get_field_content(0, "ID", savestr);			vInfo[SCRIPT_CARS][vID] = strval(savestr);
		cache_get_field_content(0, "Owner", savestr);		vInfo[SCRIPT_CARS][vOwner] = strval(savestr);
		cache_get_field_content(0, "Model", savestr);		vInfo[SCRIPT_CARS][vModel] = strval(savestr);
		cache_get_field_content(0, "PosX", savestr);		vInfo[SCRIPT_CARS][Pos][vX] = floatstr(savestr);
		cache_get_field_content(0, "PosY", savestr);		vInfo[SCRIPT_CARS][Pos][vY] = floatstr(savestr);
		cache_get_field_content(0, "PosZ", savestr);		vInfo[SCRIPT_CARS][Pos][vZ] = floatstr(savestr);
		cache_get_field_content(0, "PosA", savestr);		vInfo[SCRIPT_CARS][Pos][vR] = floatstr(savestr);
		cache_get_field_content(0, "Color1", savestr);		vInfo[SCRIPT_CARS][vColor1] = strval(savestr);
		cache_get_field_content(0, "Color2", savestr);		vInfo[SCRIPT_CARS][vColor2] = strval(savestr); 
		
	}
	AddStaticVehicleEx(vInfo[SCRIPT_CARS][vModel], vInfo[SCRIPT_CARS][Pos][vX], vInfo[SCRIPT_CARS][Pos][vY], vInfo[SCRIPT_CARS][Pos][vZ], vInfo[SCRIPT_CARS][Pos][vR], vInfo[SCRIPT_CARS][vColor1], vInfo[SCRIPT_CARS][vColor2], 10000); 
}
Reply
#2

Using "0" as first argument of cache_get_field_content, it will only load for the first row found. Use a loop instead and you have to use cache_delete otherwise you're going to have problems with memory leaks. Also using "SCRIPT_CARS" as index will give error about array index out of bounds.

In fact, I just remember I helped you with that few days ago: http://forum.sa-mp.com/showpost.php?...84&postcount=8
so I'm not going to write some code.
Reply
#3

I have fixed the array index out of bounds. now all I am asking is just how I can loop so all the vehicles spawn...
Reply
#4

pawn Код:
for (new i = 0, rows = cache_get_row_count(); i != rows; ++i)
{
    // cache functions..
    // create vehicle..
}
Reply
#5

I cant get that working

Could you please explain how and what I must do?
Reply
#6

Anyone able to help with this?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)