Loading only one vehicle from the database.
#1

I made this script so it spawns all vehicles from the database, but it only spawns the first car in the database.

This is my OnGamemodeInit.

Код:
	mysql_format(mysql, query, sizeof(query), "SELECT * FROM `vehicles`");
	mysql_tquery(mysql, query, "OnCarsLoad");
This is my OnCarsLoad.

Код:
public OnCarsLoad(vehicleid)
{
	new tmp[256], string5[256];
	cInfo[vehicleid][cModel] = cache_get_field_content_int(0, "carModel");
	cInfo[vehicleid][cPosX] = cache_get_field_content_float(0, "carPosX");
	cInfo[vehicleid][cPosY] = cache_get_field_content_float(0, "carPosY");
	cInfo[vehicleid][cPosZ] = cache_get_field_content_float(0, "carPosZ");
	cInfo[vehicleid][cPosF] = cache_get_field_content_float(0, "carPosF");
	cInfo[vehicleid][cGroup] = cache_get_field_content_int(0, "carGroup");
	cInfo[vehicleid][cVW] = cache_get_field_content_int(0, "carVirtualWorld");
	cInfo[vehicleid][cColor1] = cache_get_field_content_int(0, "carColor1");
	cInfo[vehicleid][cColor2] = cache_get_field_content_int(0, "carColor2");
	cache_get_field_content(0, "carPlate", tmp, mysql), cInfo[vehicleid][cPlate] = strval(tmp);
	CreateVehicle(cInfo[vehicleid][cModel], cInfo[vehicleid][cPosX], cInfo[vehicleid][cPosY], cInfo[vehicleid][cPosZ], cInfo[vehicleid][cPosF], cInfo[vehicleid][cColor1], cInfo[vehicleid][cColor2], -1);
	SetVehicleVirtualWorld(vehicleid, cInfo[vehicleid][cVW]);
	SetVehicleNumberPlate(vehicleid, cInfo[vehicleid][cPlate]);
	vCount++;
	format(string5, sizeof(string5), " %d cars loaded from database.", vCount);
	print(string5);
	return 1;
}
Reply
#2

Perhaps because you are only accessing row 0? You will need a loop.
Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Perhaps because you are only accessing row 0? You will need a loop.
And how exactly do I do that?
I've tried with "for(new i" etc.. But it didn't work.. :c
Reply
#4

Quote:
Originally Posted by danielpalade
Посмотреть сообщение
And how exactly do I do that?
I've tried with "for(new i" etc.. But it didn't work.. :c
Simply because your code is wrong, you are expecting a vehicleid parameter when loading, which shouldn't be there
PHP код:
public OnCarsLoad()
{
    for(new 
i,j=cache_num_rows();i<j;i++)
    {
    new 
tmp[256], string5[256];
    
cInfo[i][cModel] = cache_get_field_content_int(i"carModel");
    }

Here's an example, fix your code according to this

Oh, and I'm assuming you don't use this OnCarsLoad again, if you are you should assign a global var, and increment it everytime you create a car, and load your array according to that global variable.
Reply
#5

Quote:
Originally Posted by PrO.GameR
Посмотреть сообщение
Simply because your code is wrong, you are expecting a vehicleid parameter when loading, which shouldn't be there
PHP код:
public OnCarsLoad()
{
    for(new 
i,j=cache_num_rows();i<j;i++)
    {
    new 
tmp[256], string5[256];
    
cInfo[i][cModel] = cache_get_field_content_int(i"carModel");
    }

Here's an example, fix your code according to this

Oh, and I'm assuming you don't use this OnCarsLoad again, if you are you should assign a global var, and increment it everytime you create a car, and load your array according to that global variable.
One bad thing you are doing is creating variables inside a loop, never do that!
Reply
#6

One more problem I have, is its not creating the vehicle when it inits the callback.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)