ID not valid
#1

Basically on the garage system im making, i did a /sellgarage cmd where players can put for sale their garages.

Admins can do /creategarage for creating them. I create a garage, their ID is 1 (which is correct), so the creation part works.

The problem comes with the /sellgarage CMD, even if the garageID exists, it says i've specified an invalid garage ID.

pawn Код:
CMD:sellgarage(playerid, params[])
{
    new id;
   
    new price;

    if(sscanf(params, "di", id, price)) return SCM(playerid, COLOR_BELGREEN, "» [USAGE]: /sellgarage [garage ID] [price]");

    if((id < 1 || id >= MAX_GARAGES) || !GarageInfo[id][garageExists]) return SCM(playerid, COLOR_ERROR, "» You have specified an invalid garage ID.");

    if(GarageInfo[id][garageOwnerID] != Player[playerid][ID]) return SCM(playerid, COLOR_ERROR, "» This garage is not yours.");

    if(!GarageInfo[id][garageOwned]) return SCM(playerid, COLOR_ERROR, "» This garage can't be put for sale because it's unowned.");
   
    if(GarageInfo[id][garagePlayerSellPrice] > 0) return SCM(playerid, COLOR_ERROR, "» This garage is already for sale. If you wish to change the price, you can use /changegarageprice.");

    if((price < 1) || (price > 999999)) return SCM(playerid, COLOR_ERROR, "» Invalid price.");

    PutGarageForSale(playerid, id, price);

    RefreshGarage(id);
    return 1;
}
What the hell is wrong?

garageID is set as AUTO_INCREMENT on database, which increases by 1 everytime i create a new garage with /creategarage.

I've created 1 garage for testing, ID 1, i'm trying to sell it but i get that stupid message "you've specified an invalid garage ID" even if the garage ID 1 exists in the database.
Reply
#2

Do you set a value when creating a garage for GarageInfo[id][garageExists]?
If so, check which ID (value) is used for the creation.
Reply
#3

When i create a garage, this is what i do with garageExists:

pawn Код:
GarageInfo[i][garageExists] = true;
Just this, nothing more.
Reply
#4

Quote:
Originally Posted by SymonClash
Посмотреть сообщение
When i create a garage, this is what i do with garageExists:

pawn Код:
GarageInfo[i][garageExists] = true;
Just this, nothing more.
Why is it in a loop? Just use the transferred variable instead of "i"

Ex: If you have a function called CreateGarage(id, ...) Edit the variable like this:

pawn Код:
GarageInfo[id][garageExists] = true;
Reply
#5

@TheToretto:

This is what the debug prints:

Quote:

[16:35:50] Garage is saved for id: 0

Which is not true since the garage ID is saved as ID 1 in database. I dont understand why it says 0.

This is the full /creategarage CMD:

pawn Код:
CMD:creategarage(playerid, params[])
{
    if(Player[playerid][AdminLevel] < 5) return 0;

    new id;

    new price, size;

    if(sscanf(params, "dd(0)", price, size))
    {
        SCM(playerid, COLOR_BELGREEN, "» [USAGE]: /creategarage [price] [size(default 0)]");
        return SCM(playerid, COLOR_BELGREEN, "» Size: 0 - Small Garage , 1 - Medium Garage, 2 - Big Garage.");
    }

    if((size < 0) || (size > 2)) return SCM(playerid, COLOR_ERROR, "» Size must be from 0 to 2.");
    if((price < 1) || (price > 999999)) return SCM(playerid, COLOR_ERROR, "» Invalid price.");
   
    new query[300];
    mysql_format(g_SQL, query, sizeof query, "INSERT INTO `garages` (`garagePrice`, `garageSize`) VALUES ('%d', '%d')", price, size);
    mysql_tquery(g_SQL, query, "OnGarageCreated", "d", id);

    GarageInfo[id][garageExists] = true;

    GetPlayerPos(playerid, GarageInfo[id][garagePos][0], GarageInfo[id][garagePos][1], GarageInfo[id][garagePos][2]);
    GetPlayerFacingAngle(playerid, GarageInfo[id][garagePos][3]);

    GarageInfo[id][garagePos][0] = GarageInfo[id][garagePos][0] + (1.5 * floatsin(-GarageInfo[id][garagePos][3], degrees));
    GarageInfo[id][garagePos][1] = GarageInfo[id][garagePos][1] + (1.5 * floatcos(-GarageInfo[id][garagePos][3], degrees));

    GarageInfo[id][garageVW] = GetPlayerVirtualWorld(playerid);

    GarageInfo[id][garagePrice] = price;
    GarageInfo[id][garageSize] = size;

    RefreshGarage(id);

    SCMEX(playerid, COLOR_YELLOW, "» You have successfully created a garage. ID: %d - Price: %s - Size: %d", id, formatInt(price), size);
   
    printf("Garage is saved for id: %d", id);

    if(id == -1) return SCM(playerid, COLOR_ERROR, "» The server has reached the limit for garages.");
    return 1;
}
pawn Код:
function OnGarageCreated(gid)
{
    if(gid == -1 || !GarageInfo[gid][garageExists]) return 0;

    GarageInfo[gid][garageID] = cache_insert_id();

    SaveGarage(gid);
    return 1;
}
And this is how i load garages when server starts (however the loading and saving part works, it also shows the correct garage ID on the label. Just when i create it shows wrong ID.

pawn Код:
function LoadGarages()
{
    for(new i, j = cache_num_rows(); i != j; i++)
    {
        if(i < MAX_GARAGES)
        {
                GarageInfo[i][garageExists] = true;

                cache_get_value_int(i, "garageID", GarageInfo[i][garageID]);

                cache_get_value_float(i, "garageX", GarageInfo[i][garagePos][0]);
                cache_get_value_float(i, "garageY", GarageInfo[i][garagePos][1]);
                cache_get_value_float(i, "garageZ", GarageInfo[i][garagePos][2]);
                cache_get_value_float(i, "garageA", GarageInfo[i][garagePos][3]);

                cache_get_value_int(i, "garageVW", GarageInfo[i][garageVW]);

                cache_get_value_int(i, "garageOwnerID", GarageInfo[i][garageOwnerID]);
                cache_get_value_int(i, "garageOwned", GarageInfo[i][garageOwned]);

                cache_get_value_int(i, "garagePrice", GarageInfo[i][garagePrice]);
                cache_get_value_int(i, "garagePlayerSellPrice", GarageInfo[i][garagePlayerSellPrice]);

                cache_get_value_int(i, "garageSize", GarageInfo[i][garageSize]);

                RefreshGarage(i);
        }
    }
    return 1;
}
Reply
#6

Well your

pawn Код:
new id;
never changes in your script, are you aware of it? So it is normal to output 0

You should assign your variable a value, which should be the free faction ID.

Edit: Use y_iterate, it's does all the job and is very easy to manipulate, ideal for stuff like that.
Reply
#7

I used already y_iterate by doing Iter_Add, Iter_Remove and similar, doesn't work.

How i can fix?
Reply
#8

Define "doesn't work"? Show me how you did that before?
Reply
#9

Just rewrote the code to show you. removed garageExists and added iterator:

pawn Код:
new Iterator:Garages<MAX_GARAGES>;
Loading:

pawn Код:
function LoadGarages()
{
    new rows;
   
    cache_get_row_count(rows);

    if(!rows) return 1;

    for(new i; i < rows; i++)
    {
        cache_get_value_int(i, "garageID", GarageInfo[i][garageID]);

        cache_get_value_float(i, "garageX", GarageInfo[i][garagePos][0]);
        cache_get_value_float(i, "garageY", GarageInfo[i][garagePos][1]);
        cache_get_value_float(i, "garageZ", GarageInfo[i][garagePos][2]);
        cache_get_value_float(i, "garageA", GarageInfo[i][garagePos][3]);

        cache_get_value_int(i, "garageVW", GarageInfo[i][garageVW]);

        cache_get_value_int(i, "garageOwnerID", GarageInfo[i][garageOwnerID]);
        cache_get_value_int(i, "garageOwned", GarageInfo[i][garageOwned]);

        cache_get_value_int(i, "garagePrice", GarageInfo[i][garagePrice]);
        cache_get_value_int(i, "garagePlayerSellPrice", GarageInfo[i][garagePlayerSellPrice]);

        cache_get_value_int(i, "garageSize", GarageInfo[i][garageSize]);

        RefreshGarage(i);
       
        Iter_Add(Garages, i);
    }
    return 1;
}
Creating:

pawn Код:
CMD:creategarage(playerid, params[])
{
    if(Player[playerid][AdminLevel] < 5) return 0;

    if(Iter_Free(Garages) >= MAX_GARAGES) return SCM(playerid, COLOR_ERROR, "» Server has reached max garages limit.");

    new id = Iter_Free(Garages);

    new price, size;

    if(sscanf(params, "dd(0)", price, size))
    {
        SCM(playerid, COLOR_BELGREEN, "» [USAGE]: /creategarage [price] [size(default 0)]");
        return SCM(playerid, COLOR_BELGREEN, "» Size: 0 - Small Garage , 1 - Medium Garage, 2 - Big Garage.");
    }

    if((size < 0) || (size > 2)) return SCM(playerid, COLOR_ERROR, "» Size must be from 0 to 2.");
    if((price < 1) || (price > 999999)) return SCM(playerid, COLOR_ERROR, "» Invalid price.");
   
    GetPlayerPos(playerid, GarageInfo[id][garagePos][0], GarageInfo[id][garagePos][1], GarageInfo[id][garagePos][2]);
    GetPlayerFacingAngle(playerid, GarageInfo[id][garagePos][3]);

    GarageInfo[id][garagePos][0] = GarageInfo[id][garagePos][0] + (1.5 * floatsin(-GarageInfo[id][garagePos][3], degrees));
    GarageInfo[id][garagePos][1] = GarageInfo[id][garagePos][1] + (1.5 * floatcos(-GarageInfo[id][garagePos][3], degrees));

    GarageInfo[id][garageVW] = GetPlayerVirtualWorld(playerid);

    GarageInfo[id][garagePrice] = price;
    GarageInfo[id][garageSize] = size;

    Iter_Add(Garages, id);

    new query[300];
    mysql_format(g_SQL, query, sizeof query, "INSERT INTO `garages` (`garagePrice`, `garageSize`) VALUES ('%d', '%d')", price, size);
    mysql_tquery(g_SQL, query, "OnQueryFinished", "dd", id, THREAD_CREATE_GARAGE);

    RefreshGarage(id);

    SCMEX(playerid, COLOR_YELLOW, "» You have successfully created a garage. ID: %d - Price: %s - Size: %d", GarageInfo[id][garageID], formatInt(price), size);
   
    printf("Garage is saved for id: %d", id);
    return 1;
}
THREAD_CHREATE_GARAGE:

pawn Код:
function OnQueryFinished(extraid, threadid)
{
    switch(threadid)
    {
        case THREAD_CREATE_GARAGE:
        {
            GarageInfo[extraid][garageID] = cache_insert_id();
            SaveGarage(extraid);
        }
    }
    return 1;
}
/sellgarage CMD:

pawn Код:
CMD:sellgarage(playerid, params[])
{
    new id, price;

    if(sscanf(params, "dd", id, price)) return SCM(playerid, COLOR_BELGREEN, "» [USAGE]: /sellgarage [garage ID] [price]");
   
    if((id < 0 || id >= MAX_GARAGES || !GarageInfo[id][garageID])) return SCM(playerid, COLOR_ERROR, "» You have specified an invalid garage ID.");

    if(GarageInfo[id][garageOwnerID] != Player[playerid][ID]) return SCM(playerid, COLOR_ERROR, "» This garage is not yours.");

    if(!GarageInfo[id][garageOwned]) return SCM(playerid, COLOR_ERROR, "» This garage can't be put for sale because it's unowned.");

    if(GarageInfo[id][garagePlayerSellPrice] > 0) return SCM(playerid, COLOR_ERROR, "» This garage is already for sale. If you wish to change the price, you can use /changegarageprice.");

    if((price < 1) || (price > 999999)) return SCM(playerid, COLOR_ERROR, "» Invalid price.");

    PutGarageForSale(playerid, id, price);

    RefreshGarage(id);
    return 1;
}
I don't understand WHY if the first created garage ever, is ALWAYS ID 0! I seriously don't understand why. Because if i do /sellgarage 0 *price* it works. But there is NO ID 0 on the "garages" table!

Also the debug and the SCM on /creategarage shows ID 0 too while creating.

But in the garages table EVERYTHING is perfect. (Correct ID, position price size etc.)
Reply
#10

Don't use the auto incremented ID from the database, instead use your generated ID from the script so everything matches and will be coordinated, basically save the Iter Free generated ID into the database, and whenever you're trying to access data, use the SELECT query and point it to the saved ID, not the AI ID.
Reply
#11

Quote:
Originally Posted by TheToretto
Посмотреть сообщение
Don't use the auto incremented ID from the database, instead use your generated ID from the script so everything matches and will be coordinated, basically save the Iter Free generated ID into the database, and whenever you're trying to access data, use the SELECT query and point it to the saved ID, not the AI ID.
Better yet just save the auto incremented ID into that enum use it for reference. That way you don't need to worry about aligning anything.
Reply
#12

Quote:
Originally Posted by Pottus
Посмотреть сообщение
Better yet just save the auto incremented ID into that enum use it for reference. That way you don't need to worry about aligning anything.
Sometimes or always, the auto incremented value, even if deleted manually from the database keeps incrementing, example : I remove all rows, last row has an AI value of 8, logically it should reset and restart from 0, but it keeps 9-10-11...
Reply
#13

It doesn't matter what id it is in the database you just add to the first open array then save the database id reference in the enum. You will never have a problem doing that.
Reply
#14

Quote:
Originally Posted by TheToretto
Посмотреть сообщение
Don't use the auto incremented ID from the database, instead use your generated ID from the script so everything matches and will be coordinated, basically save the Iter Free generated ID into the database, and whenever you're trying to access data, use the SELECT query and point it to the saved ID, not the AI ID.
I thought to do the same thing. Can you give me an example on how to save the Iter Free generated ID in /creategarage CMD?

And for /sellgarage, can i do in this way to check if the garageid the player is trying to sell is valid or not? :

pawn Код:
if(!Iter_Contains(Garages, id) return "Garage ID does not exist."
Is it right?

Insted of using the SELECT query. ^

@Pottus: You're also right but i've been strugglin' with this from 2 days and tried everything.
Reply
#15

pawn Код:
mysql_format(g_SQL, query, sizeof query, "INSERT INTO `garages` (`ID`, `garagePrice`, `garageSize`) VALUES ('%d', '%d', '%d')", id, price, size);
mysql_tquery(g_SQL, query, "OnGarageCreated", "d", id);
And yes you can use Iter_Contains for that but your code isn't going to work, make it:

pawn Код:
if(!Iter_Contains(Garages, id))
    return SendClientMessage(playerid, -1, "Garage ID does not exist.");
Reply
#16

Sorry for double post, just tried now. While doing /creategarage i get this in console log:

Quote:

[13:04:22] sscanf warning: Format specifier does not match parameter count.

However, the garage is created and it's ID 0 in database.

Is it normal?

EDIT: Just tried creating 3 garages, the first one with assigned ID 0 works. The last two garages have their X, Y and Z to 0 (so their position is not saved), and their XYZ too replaces the garage ID 0 XYZ, why?

EDIT 2: Edited /creategarage CMD and manage to make it work the position save.

pawn Код:
CMD:creategarage(playerid, params[])
{
    if(Player[playerid][AdminLevel] < 5) return 0;

    if(Iter_Free(Garages) >= MAX_GARAGES) return SCM(playerid, COLOR_ERROR, "» Server has reached max garages limit.");

    new id = Iter_Free(Garages);

    new price, size;

    if(sscanf(params, "dd(0)", price, size))
    {
        SCM(playerid, COLOR_BELGREEN, "» [USAGE]: /creategarage [price] [size(default 0)]");
        return SCM(playerid, COLOR_BELGREEN, "» Size: 0 - Small Garage , 1 - Medium Garage, 2 - Big Garage.");
    }

    if((size < 0) || (size > 2)) return SCM(playerid, COLOR_ERROR, "» Size must be from 0 to 2.");
    if((price < 1) || (price > 999999)) return SCM(playerid, COLOR_ERROR, "» Invalid price.");
   
    new Float:X, Float:Y, Float:Z, Float:A;
   
    GetPlayerPos(playerid, X, Y, Z);
    GetPlayerFacingAngle(playerid, A);
   
    GarageInfo[id][garagePos][0] = X;
    GarageInfo[id][garagePos][1] = Y;
    GarageInfo[id][garagePos][2] = Z;
    GarageInfo[id][garagePos][3] = A;
   
    GarageInfo[id][garageVW] = GetPlayerVirtualWorld(playerid);

    GarageInfo[id][garagePrice] = price;
    GarageInfo[id][garageSize] = size;

    Iter_Add(Garages, id);

    new query[400];
   
    mysql_format(g_SQL, query, sizeof query, "INSERT INTO `garages` (`garageID`, `garagePrice`, `garageSize`, `garageX`, `garageY`, `garageZ`, `garageA`) VALUES ('%d', '%d', '%d', '%.4f', '%.4f', '%.4f', '%.4f')",
    id, price, size, X, Y, Z, A);
   
    mysql_tquery(g_SQL, query, "OnGarageCreated", "d", id);

    SCMEX(playerid, COLOR_YELLOW, "» You have successfully created a garage. ID: %d - Price: %s - Size: %d", id, formatInt(price), size);
   
    printf("Garage is saved for id: %d", id);
    return 1;
}
Now there is yet a problem, the last created garage replaces automatically garage with ID 0 (by copying XYZ, price and size), but still gets created normally (and its ID gets assigned correctly).
Reply
#17

This doesn't work like that:

pawn Код:
if(Iter_Free(Garages) >= MAX_GARAGES) return SCM(playerid, COLOR_ERROR, "» Server has reached max garages limit.");
As if there are no slots free, it will return -1, so either use Iter_Contains (as shown above) or check if Iter_Free's value is -1, then what do you mean by "the last created garage" ? Last loaded garage on the initialization?
Reply
#18

Lemme explain. When i create a garage, the first one created is ID 0. Everything is ok.

I create another garage, its ID is 1. Everything is ok BUT the ID 1 garage (XYZ, price and size) are copied even in ID 0 garage replacing old ID 0 garage values. Got it?
Reply
#19

I don't see what causes it. If someone else can, @Pottus maybe because honestly your code should work (assuming that it doesn't come from the function OnGarageCreated)
Reply
#20

So let's gonna wait someone...
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)