MySQL Queries (Vehicle System).
#1

Okay, so I'm trying to make a vehicle system for my script, but I'm having a wee' bit of trouble ((Sorry if I have sloppy code!!))

pawn Код:
public CreateVeh(model, Float:X, Float:Y, Float:Z, Float:F, colour1, colour2, playerid)
{
    new vID, qString[512], conversion[4];
    vInfo[vID][vModel] = model;
    vInfo[vID][vPosX] = X;
    vInfo[vID][vPosY] = Y;
    vInfo[vID][vPosZ] = Z;
    vInfo[vID][vAngle] = F;
    vInfo[vID][vColour1] = colour1;
    vInfo[vID][vColour2] = colour2;

    mysql_format(mysql, qString, sizeof(qString), "INSERT INTO `vehicles` (`Model`, `posX`, `posY`, `posZ`, `Angle`, `Colour1`, `Colour2`) VALUES ('%i', '%f', '%f', '%f', '%f', '%i', '%i')", vInfo[vID][vModel], vInfo[vID][vPosX], vInfo[vID][vPosY], vInfo[vID][vPosZ], vInfo[vID][vAngle], vInfo[vID][vColour1], vInfo[vID][vColour2]);
    for(new i = 1; i < sizeof(vInfo); i++)
    {
        valstr(conversion, i);
        if(maxHit == 1) {
            break;
        } else {
            mysql_format(mysql, qString, sizeof(qString),"SELECT * FROM `vehicles` WHERE `ID` = '%s'", conversion);
            mysql_tquery(mysql, qString, "CheckVehicle", "i", i);
        }
    }
    maxHit = 0;
    return 1;
}

public CheckVehicle(vehicleid, playerid)
{
    new rows, fields;
    cache_get_data(rows, fields, mysql);
    if(cache_get_row_int(0, 1) == 0 && maxHit == 0)
    {
        printf("%i", vehicleid);
        return maxHit = 1;
    }
    return 1;
}



When the command /createveh [model] [colour1] [colour2] is typed, the script then initiates the CreateVeh function. This is then supposed to read the MySQL and get the lowest row which can have a new input.. Therefore, when the CreateVeh function runs, it checks the table `vehicles` for every ID within the maximum vehicle limit (temporarily 20), and for each ID, it checks whether the model number is NOT equal to 0, if it is, it's supposed to 'break' the CheckVeh loop and report the available ID (in this case, print it to the console to debug);

The problem is this;
A) I'm having to use a Public "maxHit" variable to check whether it has hit the lowest value (to prevent it reading EVERY value without a record in).

B) It's not setting the "maxHit" variable back to 0 everytime the CreateVeh is complete (Causing the player to have to type the /createveh command twice..)

C) I'm sure there would be another method to do something like this without the need for an additional variable / function?.. However, tquery seems to like sending the command to another function to carry out the rest of the command.



+REP for helpful comments!

Thanks.
Reply
#2

Bump -- Anyone? Or should I just try again? :P
Reply
#3

I don't really understand what you want to do and what the code you've posted is supposed to do. Some parts are half, I'll show you:

You format a query with INSERT INTO clause but you never execute a query. So what's the point of doing it if you don't want to insert a new row?

What's the point of the loop, the convertion of integer to string when you can use %i or %d specifier and the maxHit?

Last one, the callback. Your arguments are 2 in the callback: vehicleid, playerid but you only pass 1 argument in the callback in mysql_tquery.
Reply
#4

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
I don't really understand what you want to do and what the code you've posted is supposed to do. Some parts are half, I'll show you:

You format a query with INSERT INTO clause but you never execute a query. So what's the point of doing it if you don't want to insert a new row?

What's the point of the loop, the convertion of integer to string when you can use %i or %d specifier and the maxHit?

Last one, the callback. Your arguments are 2 in the callback: vehicleid, playerid but you only pass 1 argument in the callback in mysql_tquery.
Okay, the reason for no query execution is for debugging reasons -- I know that the execution of the query works, but not the ID.

The loop is checking each row of the `vehicles` table for an invalid model (empty row) where it might insert a new vehicle rather than constantly auto incrementing (this way if the /dveh command is executed, it can delete the noted row -- without it however when using DestroyVehicle(id) -- it uses a server id, not the table id.).. IN THEORY.

The second argument on the callback was a test (playerid) to relay a message back to the sender... It's not needed.


The hitMax was the only way I could think of to say to break the loop since it's using an if statement OUTSIDE of the loop (custom function)
Reply
#5

Quote:
Originally Posted by Ryan_Undering
Посмотреть сообщение
Okay, the reason for no query execution is for debugging reasons -- I know that the execution of the query works, but not the ID.

The loop is checking each row of the `vehicles` table for an invalid model (empty row) where it might insert a new vehicle rather than constantly auto incrementing (this way if the /dveh command is executed, it can delete the noted row -- without it however when using DestroyVehicle(id) -- it uses a server id, not the table id.).. IN THEORY.

The second argument on the callback was a test (playerid) to relay a message back to the sender... It's not needed.


The hitMax was the only way I could think of to say to break the loop since it's using an if statement OUTSIDE of the loop (custom function)
Well, you should've mentioned everything in your thread. Even if I read the above quote, I still don't get few things.

What ID?

The loop goes from 1 to the size of vInfo array and it only checks if maxHit is 1. How is that supposed to check for an empty row?

If it's not needed, then pass INVALID_PLAYER_ID but keep the correct arguments.

I believe you've confused about hitMax. Is it supposed to be an array and check for it? In the loop above, if hitMax is 1, the loop will break from the start.
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Well, you should've mentioned everything in your thread. Even if I read the above quote, I still don't get few things.

What ID?

The loop goes from 1 to the size of vInfo array and it only checks if maxHit is 1. How is that supposed to check for an empty row?

If it's not needed, then pass INVALID_PLAYER_ID but keep the correct arguments.

I believe you've confused about hitMax. Is it supposed to be an array and check for it? In the loop above, if hitMax is 1, the loop will break from the start.
Okay, sorry, please ignore me. I had like 4 hours sleep and 8 hours in the morning coding... I overlooked the obvious things.. so after playing without scripting for a bit and whatnot, I realized the problem... so yeah...

Again -- it was my failed knowledge to MySQL within samp... I'll try sleeping... or maybe caffeine next time :l
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)