SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 10/09/2019, 06:36 PM   #1
XStormiest
Gangsta
 
Join Date: Jun 2012
Location: Continent: Europe, Country: Romania, City: Buzau
Posts: 558
Reputation: 46
Default 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..
__________________

Last edited by XStormiest; 10/09/2019 at 07:06 PM.
XStormiest is offline   Reply With Quote
Old 11/09/2019, 07:09 AM   #2
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,177
Reputation: 73
Default 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.
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Old 11/09/2019, 07:14 PM   #3
XStormiest
Gangsta
 
Join Date: Jun 2012
Location: Continent: Europe, Country: Romania, City: Buzau
Posts: 558
Reputation: 46
Default 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?
__________________
XStormiest is offline   Reply With Quote
Old 12/09/2019, 10:17 AM   #4
Mugala
High-roller
 
Mugala's Avatar
 
Join Date: Nov 2012
Location: Georgia, Tbilisi
Posts: 1,177
Reputation: 73
Default 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); 
__________________
Coding in 8 languages including Pawn, C#, C++ and SQL.
Available for hiring.

If you have a coding question or need an assistance in a code, just PM me.
If you want me to create something for you, here is a Discord Mugala#5651

Currently helping Community members in a coding.
Mugala is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question for arrays DusanInfinity Scripting Help 2 26/06/2015 02:10 PM
Arrays question PaulDinam Scripting Help 3 21/03/2013 05:37 PM
Arrays question! Kush Scripting Help 4 09/09/2012 07:10 PM
[Question] Arrays ArTisT Help Archive 1 20/06/2010 06:44 PM
Question about arrays Outbreak Help Archive 5 23/04/2009 10:56 PM


All times are GMT. The time now is 07:52 AM.


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