Spawning a vehicle from MySQL data.
#1

Hey,

I'm currently learning how to script based off knowledge from other languages, but I just can't seem to get my head around spawning a vehicle from the data stored in an SQL database.

I'm writing up a dealership system for an RP server and have everything working fine, but how to spawn the vehicle...

Define:
Код:
#define                 THREAD_INITIATE_PVEHICLES
enum:
Код:
enum pvinfoE {
	pvId,
	pvModel,
	pvOwnerId,
	Float: pvVPosition[3],
	Float: pvVRotation,
	pvColour[2],
	pvSpawned,
	pvScriptID,
}
Код:
new pvVariables[MAX_VEHICLES][pvinfoE];
Current basic test command(will be edited, this is just to test if the vehicle spawns):
Код:
CMD:vspawn(playerid, params[]) {
	if(playerVariables[playerid][pAdminLevel] >= 1) {
		CreateVehicle(pvVariables[playerid][pvModel], pvVariables[playerid][pvVPosition][0], pvVariables[playerid][pvVPosition][1], pvVariables[playerid][pvVPosition][2], pvVariables[playerid][pvVRotation], pvVariables[playerid][pvColour][0], pvVariables[playerid][pvColour][1], 1);
	}
}
case:
Код:
case THREAD_INITIATE_PVEHICLES: {
			mysql_store_result();

			new
			    x,
				bool: success = true;

			while(mysql_retrieve_row()) {
			    mysql_get_field("pvId", result);
			    x = strval(result);

				if(systemVariables[vehicleCounts][0] + systemVariables[vehicleCounts][1] + systemVariables[vehicleCounts][2] < MAX_VEHICLES) {
					mysql_get_field("pvModel", result);
					pvVariables[x][pvModel] = strval(result);
					
					mysql_get_field("pvOwnerId", result);
					pvVariables[x][pvOwnerId] = strval(result);

					mysql_get_field("pvPosX", result);
					pvVariables[x][pvVPosition][0] = floatstr(result);

					mysql_get_field("pvPosY", result);
					pvVariables[x][pvVPosition][1] = floatstr(result);

					mysql_get_field("pvPosZ", result);
					pvVariables[x][pvVPosition][2] = floatstr(result);

					mysql_get_field("pvPosZAngle", result);
					pvVariables[x][pvVRotation] = floatstr(result);

					mysql_get_field("pvColour1", result);
					pvVariables[x][pvColour][0] = strval(result);

					mysql_get_field("pvColour2", result);
					pvVariables[x][pvColour][1] = strval(result);
				}
			}
		}
Reply
#2

Still unable to figure it out.
Reply
#3

Where do you run the initial query to load the vehicles?
Reply
#4

Quote:
Originally Posted by VincentDunn
Посмотреть сообщение
Where do you run the initial query to load the vehicles?
Excuse me?
Reply
#5

Quote:
Originally Posted by Jonesy96
Посмотреть сообщение
Excuse me?
Show the code where the query is ran.
Reply
#6

This?

Код:
stock initiatePlayerVehicles() {
	return mysql_query("SELECT * FROM playervehicles", THREAD_INITIATE_PVEHICLES);
}
OnGameModeInit:
Код:
initiatePlayerVehicles();
Reply
#7

Anyone?
Reply
#8

Hmmm :/ ?
Reply
#9

What's your max vehicle limit? I can see the issue.
Reply
#10

Quote:
Originally Posted by AphexCCFC
Посмотреть сообщение
What's your max vehicle limit? I can see the issue.
2000
Reply
#11

Are you loading a MySQL database ID with this code?

pawn Код:
mysql_get_field("pvId", result);
I'm going to assume that you are and explain what the problem is...

You're loading a MySQL database ID and trying to pass that as the playerid, but MySQL database ID's and player ID's are not always the same. Yes, there is an off chance that you load MySQL DB ID 0 and try to pass 0 as the playerid, which would allow player ID 0 to have the information, but that isn't always going to be the case.
Reply
#12

What is the vehicle table called in your MySQL?
Reply
#13

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
Are you loading a MySQL database ID with this code?

pawn Код:
mysql_get_field("pvId", result);
I'm going to assume that you are and explain what the problem is...

You're loading a MySQL database ID and trying to pass that as the playerid, but MySQL database ID's and player ID's are not always the same. Yes, there is an off chance that you load MySQL DB ID 0 and try to pass 0 as the playerid, which would allow player ID 0 to have the information, but that isn't always going to be the case.
Nah, that's just the unique ID. I'm using pvOwnerId for the players ID which I get by using playerVariables[playerid][pInternalID].

Quote:
Originally Posted by AphexCCFC
Посмотреть сообщение
What is the vehicle table called in your MySQL?
It's called playervehicles.

Here's how I have it layed out:
Reply
#14

Try this command (syntax: /veh <vehicleid / despawn> <color 1> <color 2>):

pawn Код:
CMD:veh(playerid, params[])
{
    new id, Float:pos[4], col[2], string[256], query[500];
    if(playerVariables[playerid][pAdminLevel] < 1)
    {
        SendClientMessage(playerid, COLOR_LIGHTRED, "Error{FFFFFF}: You do not have the authorities to use this command.");
        return 1;
    }
    if(!strcmp(params, "despawn"))
    {
        if(GetPlayerState(playerid) != PLAYER_STATE_DRIVER)
        {
            SendClientMessage(playerid, COLOR_LIGHTRED, "Error{FFFFFF}: You cannot despawn a vehicle unless you're in the driver seat.");
            return 1;
        }
        for(new i=0; i<MAX_VEHICLES; i++)
        {
            if(pvVariables[i][pvModel] == cVeh[i])
            {
                DestroyVehicle(cVeh[i]);
                pvVariables[i][pvModel] = 0;
                pvVariables[i][pvVPosition][0] = 0;
                pvVariables[i][pvVPosition][1] = 0;
                pvVariables[i][pvVPosition][2] = 0;
                pvVariables[i][pvVRotation] = 0;
                pvVariables[i][pvColour][0] = 0;
                pvVariables[i][pvColour][1] = 0;
                format(query, sizeof(query), "DELETE FROM `Vehicles` WHERE Model = %d", pvVariables[i][pvModel]);
                mysql_query(query);
                i = MAX_VEHICLES;
                return 1;
            }
        }
        SendClientMessage(playerid, COLOR_LIGHTRED, "Error{FFFFFF}: You cannot despawn this vehicle.");
        return 1;
    }
    if(sscanf(params, "iii", id, col[0], col[1]))
    {
        SendClientMessage(playerid, COLOR_YELLOW, "Usage{FFFFFF}: /veh [vehicle id/despawn] [color 1] [color 2]");
        return 1;
    }
    if(id < 400 || id > 611)
    {
        SendClientMessage(playerid, COLOR_LIGHTRED, "Error{FFFFFF}: Vehicle ID's are between 400 and 611.");
        return 1;
    }
    GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
    GetPlayerFacingAngle(playerid, pos[3]);
    for(new i=0; i<MAX_VEHICLES; i++)
    {
        if(!cVeh[i])
        {
            pvVariables[i][pvModel] = id;
            pvVariables[i][pvVPosition][0] = pos[0];
            pvVariables[i][pvVPosition][1] = pos[1];
            pvVariables[i][pvVPosition][2] = pos[2];
            pvVariables[i][pvVRotation] = pos[3];
            pvVariables[i][pvColour][0] = col[0];
            pvVariables[i][pvColour][1] = col[1];
            format(query, sizeof(query), "INSERT INTO Vehicles (Model, XPos, YPos, ZPos, Angle, Color1, Color2) VALUES(%d, %f, %f, %f, %f, %d, %d)",
            pvVariables[i][pvModel],
            pvVariables[i][pvVPosition][0],
            pvVariables[i][pvVPosition][1],
            pvVariables[i][pvVPosition][2],
            pvVariables[i][pvVRotation],
            pvVariables[i][pvColour][0],
            pvVariables[i][pvColour][1],
            mysql_query(query);
            cVeh[i] = CreateVehicle(pvVariables[i][pvModel], pvVariables[i][pvVPosition][0], pvVariables[i][pvVPosition][1], pvVariables[i][pvVPosition][2], pvVariables[i][pvVRotation], pvVariables[i][pvColour][0], pvVariables[i][pvColour][1], 1200);
            i = MAX_VEHICLES;
        }
    }
    return 1;
}
Reply
#15

Well, gunna need to change the mysql names of the rows and table.
Reply
#16

Quote:
Originally Posted by AphexCCFC
Посмотреть сообщение
Well, gunna need to change the mysql names of the rows and table.
I'm trying to spawn a vehicle, not despawn it.
Reply
#17

Scroll down at the second part of the command, that spawns the vehicle and top part of it despawns the vehicle. /veh <vehicle id> <color1> <color2> spawns the vehicle near you, and /veh despawn gets rid of it.
Reply
#18

By the looks of it, all that does is spawns a vehicle and stores the information into the database... It doesn't spawn the vehicle using the data stored?

I want it so it reads the data and spawns the vehicle at the locations and with the colour specified in the database.
Reply
#19

Quote:
Originally Posted by Jonesy96
Посмотреть сообщение
Nah, that's just the unique ID. I'm using pvOwnerId for the players ID which I get by using playerVariables[playerid][pInternalID].
You didn't read his post.

You're using a unique id from your database as an index to an array on your actual server, which makes no logical sense whatsoever.

Just use a looping integer as an index.

This should work, haven't tested it.
pawn Код:
case THREAD_INITIATE_PVEHICLES: {
   
    mysql_store_result();

    new
        x,
        rows = mysql_num_rows();

    while(mysql_retrieve_row() && x < rows) {

        mysql_get_field("pvId", result);
        pvVariables[x][pvId]= strval(result);

        if(systemVariables[vehicleCounts][0] + systemVariables[vehicleCounts][1] + systemVariables[vehicleCounts][2] < MAX_VEHICLES) {
            mysql_get_field("pvModel", result);
            pvVariables[x][pvModel] = strval(result);
           
            mysql_get_field("pvOwnerId", result);
            pvVariables[x][pvOwnerId] = strval(result);

            mysql_get_field("pvPosX", result);
            pvVariables[x][pvVPosition][0] = floatstr(result);

            mysql_get_field("pvPosY", result);
            pvVariables[x][pvVPosition][1] = floatstr(result);

            mysql_get_field("pvPosZ", result);
            pvVariables[x][pvVPosition][2] = floatstr(result);

            mysql_get_field("pvPosZAngle", result);
            pvVariables[x][pvVRotation] = floatstr(result);

            mysql_get_field("pvColour1", result);
            pvVariables[x][pvColour][0] = strval(result);

            mysql_get_field("pvColour2", result);
            pvVariables[x][pvColour][1] = strval(result);
       
            pvVariables[x][pvScriptID] = CreateVehicle(pvVariables[x][pvModel], pvVariables[x][pvVPosition][0], pvVariables[x][pvVPosition][1], pvVariables[x][pvVPosition][2], pvVariables[x][pvVRotation], pvVariables[x][pvColour][0], pvVariables[x][pvColour][1], 60);
        }

        x++;
    }

    printf("%d private vehicles loaded.");
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)