SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Scripting Help (https://forum.sa-mp.com/forumdisplay.php?f=12)
-   -   Question about Arrays/MySQL (https://forum.sa-mp.com/showthread.php?t=669239)

XStormiest 10/09/2019 06:36 PM

Question about Arrays/MySQL
 
So, let's say I have a table populated with a player's car (player_cars) and another table called server_cars, that car also has properties.
I want to load every single car into the script.
My Question is how do I do that? using a 3 diminesional array to load every single car of every single player or is there a shortcut?
Or if you can give me an example of a script, that would be great..

Mugala 11/09/2019 07:09 AM

Re: Question about Arrays/MySQL
 
use one array for both of them, add variable where will be placed a vehicle owner type,
for example, lets call server cars type 0 and player cars type 1.

XStormiest 11/09/2019 07:14 PM

Re: Question about Arrays/MySQL
 
So what are you telling me is that I sould do something like this?

Code:

enum PLAYER_CARS
{
    //etc variables
    Vehicle[MAX_PLAYERS_VEHICLES]
}
new P_CARS[MAX_PLAYERS][PLAYER_CARS]

And then reaching out to it like
Code:

P_CARS[playerid][Vehicle][0] = 451;
P_CARS[playerid][Vehicle][3] = 45;

Where for instance 451 is the model and 45 is the color, but what if I want to also use the name of the vehicle?


Is that right?

Mugala 12/09/2019 10:17 AM

Re: Question about Arrays/MySQL
 
No, you don't need 3 dimensional array, also, there is a wrong placement in your code.
I suggest to use something like that

PHP Code:

enum VEHICLE_DATA
{
    
vModel,
    
vOwner[MAX_PLAYER_NAME+1],
    
vOwnable,
    
vType,
    
vFaction,
    
Float:vPos[4],
    
vColor1,
    
vColor2 //and so on...
};
new 
VehicleData[MAX_VEHICLES][VEHICLE_DATA];

public 
OnPlayerEnterVehicle(playeridvehicleidispassenger//example
{
    if(
VehicleData[vehicleid][vOwnable]) //if 1 = ownable, if 0 = not ownable.
    
{
         new 
str[128];
         
format(str,sizeof(str),"Vehicle owner is %s",VehicleData[vehicleid][vOwner]);
         
SendClientMessage(playerid,-1,str);
    }
    return 
1;


in a gamemodeinit (or vehicle load func) make only not-ownable vehicles loadable.
PHP Code:

public OnGameModeInit()
{
    
mysql_tquery(handle,"SELECT * FROM `vehicles` WHERE `Ownable` != 1","LoadVehicles"); //example for last mysql plugin
    
return 1;
}

forward LoadVehicles();
public 
LoadVehicles()
{
    new 
rowstotal 0;
    
rows cache_num_rows();
    if(
rows 0)
    {
        for(new 
irowsi++)
        {
            
cache_get_value_name_int(i"Model"VehicleData[i][vModel]);
            
cache_get_value_name_int(i"Color1"VehicleData[i][vColor1]);
            
cache_get_value_name_int(i"Color2"VehicleData[i][vColor2]);
            
cache_get_value_name_int(i"Type"VehicleData[i][vType]); //by type, you can access various things
            
cache_get_value_name_int(i"Faction"VehicleData[i][vFaction]); //if a vehicle is owned by a faction, faction ID can be placed here.
            
cache_get_value_name_int(i"PosX"VehicleData[i][vPos[0]]);
            
cache_get_value_name_int(i"PosY"VehicleData[i][vPos[1]]);
            
cache_get_value_name_int(i"PosZ"VehicleData[i][vPos[2]]);
            
cache_get_value_name_int(i"PosA"VehicleData[i][vPos[3]]);
   
            
CreateVehicle(VehicleData[i][vModel],VehicleData[i][vPos[0]],VehicleData[i][vPos[1]],VehicleData[i][vPos[2]],VehicleData[i][vPos[3]],VehicleData[i][vColor1],VehicleData[i][vColor2],900);
        
            
total++;
        }
        
printf("Loaded %i vehicles from %i",total,rows);
    }
    return 
1;


so, for a player, make a dialog to choose which vehicle he wants to be loaded/spawned or you can make it with mSelection (list of a cars)

so, list of the player's cars can be obtained like this
PHP Code:

new name[MAX_PLAYER_NAME+1], query[128];
GetPlayerName(playerid,name,sizeof(name));
mysql_format(handlerquerysizeof(query), "SELECT * FROM `vehicles` WHERE `Owner` = '%e'",name);
new 
Cache:result mysql_query(handle,query,true);
new 
rows cache_num_rows();
if(
rows 0)
{
    new 
str[256];
    
str "ID\tModel"
    
for(new irowsi++)
    {
        new 
temp[2];
        
cache_get_value_name_int(i,"ID",temp[0]);
        
cache_get_value_name_int(i,"Model",temp[1]);
        
format(str,sizeof(str),"%s\n%i\t%i",str,temp[0],temp[1]);//you can also display a Model by its name, if you have a custom function for that.
    
}
    
ShowPlayerDialog(playerid,1234DIALOG_STYLE_TABLIST_HEADERS"Your Vehicles"str"Select" "Close");
}
else 
SendClientMessage(playerid,-1,"You don't own a vehicle");
cache_delete(result); 

loading can be similar, like first one, but now query must be like that
PHP Code:

new query[256],name[MAX_PLAYER_NAME+1];
GetPlayerName(playerid,name,sizeof(name));
mysql_format(handle,query,sizeof(query),"SELECT * FROM `vehicles` WHERE `Ownable` = '1' AND `Owner` = '%s'",name); 



All times are GMT. The time now is 02:30 AM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.