how to increase id number per create?
#1

as the title said the id doesnt increase it stay always in 0
here is my code
pawn Код:
CMD:cdoor(playerid,params[])
{
    new string[200];
    for(new idx=0; idx<MAX_DOOR; idx++)
    {
        //if(!DoorInfo[idx][dId])
        //{
            GetPlayerPos(playerid, DoorInfo[idx][dXe], DoorInfo[idx][dYe], DoorInfo[idx][dZe]);
            DoorInfo[idx][dIe] = GetPlayerInterior(playerid);
            DoorInfo[idx][dVe] = GetPlayerVirtualWorld(playerid);
            GetPlayerFacingAngle(playerid, DoorInfo[idx][dAi]);
            // Creating the door
            CreateDynamicPickup(19198, 1, DoorInfo[idx][dXe], DoorInfo[idx][dYe], DoorInfo[idx][dZe]+0.3, DoorInfo[idx][dVe], DoorInfo[idx][dIe]);
            format(string, sizeof(string), "ID: %d",idx);
            CreateDynamic3DTextLabel(string,-1, DoorInfo[idx][dXe]-0.1, DoorInfo[idx][dYe], DoorInfo[idx][dZe]+0.5, 15);
            idx = MAX_DOOR;
        //}
    }
    return 1;
}
Reply
#2

That is because you are setting it to 0 each time.

pawn Код:
idx = MAX_DOOR; //this line
Reply
#3

Just remove idx = MAX_DOOR
Reply
#4

this is what happen

Reply
#5

Ah I see what you are trying to do there,

Код:
new idx; // Somewhere on top of your script
CMD:cdoor(playerid,params[])
{
    new string[200];
    GetPlayerPos(playerid, DoorInfo[idx][dXe], DoorInfo[idx][dYe], DoorInfo[idx][dZe]);
    DoorInfo[idx][dIe] = GetPlayerInterior(playerid);
    DoorInfo[idx][dVe] = GetPlayerVirtualWorld(playerid);
    GetPlayerFacingAngle(playerid, DoorInfo[idx][dAi]);
    // Creating the door
    CreateDynamicPickup(19198, 1, DoorInfo[idx][dXe], DoorInfo[idx][dYe], DoorInfo[idx][dZe]+0.3, DoorInfo[idx][dVe], DoorInfo[idx][dIe]);
    format(string, sizeof(string), "ID: %d",idx);
    CreateDynamic3DTextLabel(string,-1, DoorInfo[idx][dXe]-0.1, DoorInfo[idx][dYe], DoorInfo[idx][dZe]+0.5, 15);
    idx ++;
    return 1;
}
You need to make the 'idx' variable global, basically what you were doing there that you made a loop, to create x amount of doors at the same position. I don't see why you needed the loop there
Reply
#6

im making a dynamic door where will save using y_ini and that code is rip from Zgaming GM and im converting it to y_ini coz it uses dini then ive encountered this

this is my array in that code
pawn Код:
#define MAX_DOOR 100

enum dInfo
{
    dId,
    dVe,
    dIe,
    Float:dXe,
    Float:dYe,
    Float:dZe,
    Float:dXi,
    Float:dYi,
    Float:dZi,
    Float:dAi,
    dIi,
    dVi,
    dExit
}
new DoorInfo[MAX_DOOR][dInfo];
Reply
#7

Quote:
Originally Posted by WooTFTW
Посмотреть сообщение
You need to make the 'idx' variable global
What a great way to mess things up. Other loops will affect the count if using idx variable as iterator (which he probably does)
Reply
#8

Quote:
Originally Posted by Macluawn
Посмотреть сообщение
What a great way to mess things up. Other loops will affect the count if using idx variable as iterator (which he probably does)
Then just use any other word, is it so hard? Rename the idx, for example, createdDoors, or any other name which links to the variable's purpose
Reply
#9

My suggestion would be create a function which returns the next unused ID. In this case you need to add a new boolean in your enum.
pawn Код:
bool: dUsed
This is the function which would return the next unused ID. You will need no global arrays which store the ID anymore using this method. This method is also a better way for this situation, since you MIGHT make a mistake somewhere in your script with these
pawn Код:
idx--;
or
pawn Код:
idx++;
So basically heres the function:
pawn Код:
GetUnusedDoorID()
{
     for (new i = 0; i < MAX_DOOR; i++) {
          if (DoorInfo[i][dUsed]) continue;
          return i;
     }
     return MAX_DOOR;
}
And make sure when you create a new door:
pawn Код:
DoorInfo[id][dUsed] = true;
And do the opposite when removing the door.

Hope this helps.

EDIT: Theres also one improvement for making it more "automatic". Instead of creating a new boolean in the enum and switching its value to true/false and checking it to return the next ID, you simply can check the doors position. If the X, Y, Z are equal to 0.0 then its probably not created which means the ID is unused. Heres an example:
pawn Код:
GetUnusedDoorID()
{
     for (new i = 0; i < MAX_DOOR; i++) {
          if (DoorInfo[i][dX] == 0.0 && DoorInfo[i][dY] == 0.0 && DoorInfo[i][dZ] == 0.0) {
                return i;
          }
     }
     return MAX_DOOR;
}
Reply
#10

Quote:
Originally Posted by WooTFTW
Посмотреть сообщение
Then just use any other word, is it so hard? Rename the idx, for example, createdDoors, or any other name which links to the variable's purpose
If he couldnt see such a simple mistake, do you really think he would think of renaming the variable?
Reply


Forum Jump:


Users browsing this thread: 7 Guest(s)