SA-MP Forums Archive
ID's mixed up - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: ID's mixed up (/showthread.php?tid=588630)



ID's mixed up - conor565 - 10.09.2015

So basically in this gas pump system once I create the first pump obviously it creates it as ID 0 (ID 1 in the DB). If I then create another, it again says that it's ID 0 but creates it in the DB as ID 2. And when I try to edit the pump I am only able to edit pump ID 0 and it overwrites the original one created.

pawn Код:
CMD:createpump(playerid, params[]) {
    new
        localPumpID,
        bizID = INVALID_BUSINESS_ID;

    if(playerData[playerid][pAdmin] < 5)
        return sendErrorMessage(playerid, "You are not authorised to use this command.");

    if(sscanf(params, "i", bizID))
        return sendSyntaxMessage(playerid, "/createpump [businessid]");

    if((bizID < 0 || bizID >= MAX_BUSINESSES) || businessData[bizID][businessID] == INVALID_BUSINESS_ID)
        return sendErrorMessage(playerid, "You specified an invalid business ID.");

    if(businessData[bizID][businessType] != 7)
        return sendErrorMessage(playerid, "You specified a business that is not a Gas Station.");

    if(GetPlayerInterior(playerid) > 0 || GetPlayerVirtualWorld(playerid) > 0)
        return sendErrorMessage(playerid, "You cannot place gas pumps inside interiors.");

    localPumpID = createPump(playerid, bizID);

    if(localPumpID == INVALID_PUMP_ID)
        return sendErrorMessage(playerid, "You cannot create any more gas pumps for the specified business, you have reached the limit.");

    sendServerMessage(playerid, "You have successfully created gas pump ID: %i, type '/editpump' to edit.", localPumpID);
    return 1;
}
pawn Код:
createPump(playerID, bizID) {
    static
        Float: playerPos[4],
        szQuery[64],
        localPumpID = INVALID_PUMP_ID;

    if(GetPlayerPos(playerID, playerPos[0], playerPos[1], playerPos[2])) {
        if((localPumpID = getPumpFreeID()) != -1) {
            GetPlayerFacingAngle(playerID, playerPos[3]);
           
            playerPos[0] += 5.0 * floatsin(-playerPos[3], degrees);
            playerPos[1] += 5.0 * floatcos(-playerPos[3], degrees);

            pumpData[localPumpID][pumpBusinessID] = bizID;
            pumpData[localPumpID][pumpPos][0] = playerPos[0];
            pumpData[localPumpID][pumpPos][1] = playerPos[1];
            pumpData[localPumpID][pumpPos][2] = playerPos[2];
            pumpData[localPumpID][pumpPos][3] = playerPos[3];
            pumpData[localPumpID][pumpFuel] = 2000;
            pumpData[localPumpID][pumpObject] = CreateDynamicObject(1676, playerPos[0], playerPos[1], playerPos[2], 0.0, 0.0, playerPos[3]);

            format(szQuery, sizeof szQuery, "INSERT INTO `pumps` (`pumpBusinessID`) VALUES(%i)", businessData[bizID][businessID]);
            mysql_function_query(g_iConnectionHandle[0], szQuery, true, "onCreatePump", "i", localPumpID);
            return localPumpID;
        }
    }
    return INVALID_PUMP_ID;
}
pawn Код:
CMD:editpump(playerid, params[]) {
    new
        localPumpID;

    if(playerData[playerid][pAdmin] < 5)
        return sendErrorMessage(playerid, "You are not authorised to use this command.");

    if(sscanf(params, "i", localPumpID))
        return sendSyntaxMessage(playerid, "/editpump [pumpid]");

    if((localPumpID < 0 || localPumpID >= MAX_GAS_PUMPS) || pumpData[localPumpID][pumpID] == INVALID_PUMP_ID)
        return sendErrorMessage(playerid, "You specified an invalid pump ID.");

    sendServerMessage(playerid, "You are editing pump ID %i.", localPumpID);

    playerData[playerid][pEditPump] = localPumpID;

    EditDynamicObject(playerid, pumpData[localPumpID][pumpObject]);
    return 1;
}
Anyone got any idea why it's happening. Lemme know if you need anything else


Re: ID's mixed up - conor565 - 12.09.2015

Anyone got any ideas?


Re: ID's mixed up - BroZeus - 12.09.2015

Show the code for getPumpFreeID()


Re: ID's mixed up - conor565 - 12.09.2015

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
Show the code for getPumpFreeID()
pawn Код:
getPumpFreeID() {
    for(new i = 0; i < MAX_GAS_PUMPS; i++) if(pumpData[i][pumpID] != INVALID_PUMP_ID) {
        return i;
    }
    return -1;
}



Re: ID's mixed up - BroZeus - 12.09.2015

I am assuming that [pumpID] is for MySQL ID of pump in table so
Have you done like this? :
PHP код:
forward onCreatePump(localPumpID);
public 
onCreatePump(localPumpID)
{
     
pumpData[localPumpID][pumpID] = cache_insert_id();
     return 
1;

Make sure that id is the PRIMARY_KEY in mysql db.
and why aren't you inserting the coords of pump?


Re: ID's mixed up - conor565 - 12.09.2015

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
I am assuming that [pumpID] is for MySQL ID of pump in table so
Have you done like this? :
PHP код:
forward onCreatePump(localPumpID);
public 
onCreatePump(localPumpID)
{
     
pumpData[localPumpID][pumpID] = cache_insert_id();
     return 
1;

Make sure that id is the PRIMARY_KEY in mysql db.
and why aren't you inserting the coords of pump?
The coords of the pumps are inserted after /editpump is finished

Yeah that's how it is. I have noticed I explained it really poorly in the OT so I'll explain it as in depth as possible.

So when you /createpump <BusinessID here> it'll create the pump in front of you with ID 0 then you /editpump and then when finished editing it will create the 3d text. Then if you create a new pump, it re-uses the ID 0 but doesn't delete the original dynamicobject. If you try do /editpump 1 it doesn't work as it doesn't recognise the ID but if you /editpump 0 it allows you to edit the NEW pump which SHOULD be ID 1 instead of 0. Then when finished editing, it'll delete the 3d text from the original gas pump and re-create it at the new one which again should be ID 1 but shows as ID 0. In the database it shows as ID 1 but IG it's recognised as ID 0.

For a bit of help, I've put the whole system in pastebin here: http://pastebin.com/jCk3YzQ9


Re: ID's mixed up - BroZeus - 12.09.2015

The problem is getPumpFreeID it should be
Quote:

pumpData[i][pumpID] == INVALID_PUMP_ID //right

not
Quote:

pumpData[i][pumpID] != INVALID_PUMP_ID //wrong




Re: ID's mixed up - conor565 - 12.09.2015

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
The problem is getPumpFreeID it should be not
Surely if it was = to INVALID_PUMP_ID it'll just return -1 (-1 is defined as the invalid pump ID)


Re: ID's mixed up - BroZeus - 12.09.2015

Quote:
Originally Posted by conor565
Посмотреть сообщение
Surely if it was = to INVALID_PUMP_ID it'll just return -1 (-1 is defined as the invalid pump ID)
Well no, think about it currently is returning the id of pump for which the mysqlid is not invlaid that means there is pump already loaded in there, just try this one :
PHP код:
getPumpFreeID() {
    for(new 
0MAX_GAS_PUMPSi++) if(pumpData[i][pumpID] == 0) { //== 0
        
return i;
    }
    return -
1;




Re: ID's mixed up - conor565 - 12.09.2015

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
Well no, think about it currently is returning the id of pump for which the mysqlid is not invlaid that means there is pump already loaded in there, just try this one :
PHP код:
getPumpFreeID() {
    for(new 
0MAX_GAS_PUMPSi++) if(pumpData[i][pumpID] == 0) { //== 0
        
return i;
    }
    return -
1;

Jesus thanks mate, the amount of time I've spent trying to find something so small wrong. Thanks bro