Understanding foreach.
#1

So, i'm creating entrances system using foreach and djson.
It looks like that
pawn Код:
enum dInfo
{
    Float:dExteriorX,
    Float:dExteriorY,
    Float:dExteriorZ,
    dExteriorInt,
    Float:dInteriorX,
    Float:dInteriorY,
    Float:dInteriorZ,
    dInteriorInt,
    dName[128],
    dPp
}
new DoorInfo[MAX_DOORS][dInfo];
Everybody know what is this for Enum for entrances.
And then
pawn Код:
new Iterator:Doors<MAX_DOORS>, dCount;
public OnGameModeInit()
{
        for(new d=0;d< MAX_DOORS;d++)
    {
        LoadDoor(d);
    }
        return 1;
}


stock LoadDoor(id)
{
    new string[128], string2[128], name[128];
    format(string, sizeof(string), "Door_%d", id);
    if(djIsSet(DOORS_FILE, string))
    {
        format(string2, sizeof(string2),"Door_%d/ExteriorX", id); DoorInfo[id][dExteriorX] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/ExteriorY", id); DoorInfo[id][dExteriorY] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/ExteriorZ", id); DoorInfo[id][dExteriorZ] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/ExteriorInt", id); DoorInfo[id][dExteriorInt] = djInt(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/InteriorX", id); DoorInfo[id][dInteriorX] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/InteriorY", id); DoorInfo[id][dInteriorY] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/InteriorZ", id); DoorInfo[id][dInteriorZ] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/InteriorInt", id); DoorInfo[id][dInteriorInt] = djInt(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/Name", id); format(name, sizeof(name), "%s", dj(DOORS_FILE, string2)); DoorInfo[id][dName] = name;
        Iter_Add(Doors, id);
        DoorInfo[id][dPp] = CreateDynamicPickup(1239, 2, DoorInfo[id][dExteriorX], DoorInfo[id][dExteriorY], DoorInfo[id][dExteriorZ], -1, DoorInfo[id][dExteriorInt], -1, 50.0);
        return 1;
    }
    return 1;
}
stock AddDoor(Float:x, Float:y, Float:z, interior, name[])
{
    new string2[128];
    dCount ++;
    Itter_Add(Doors, dCount);
    format(string2, sizeof(string2), "Door_%d/ExteriorX", dCount); djSetFloat(DOORS_FILE, string2, x);
    format(string2, sizeof(string2), "Door_%d/ExteriorY", dCount); djSetFloat(DOORS_FILE, string2, y);
    format(string2, sizeof(string2), "Door_%d/ExteriorZ", dCount); djSetFloat(DOORS_FILE, string2, z);
    format(string2, sizeof(string2), "Door_%d/ExteriorInt", dCount); djSetInt(DOORS_FILE, string2, interior);
    format(string2, sizeof(string2), "Door_%d/InteriorX", dCount); djSetFloat(DOORS_FILE, string2, 0);
    format(string2, sizeof(string2), "Door_%d/InteriorY", dCount); djSetFloat(DOORS_FILE, string2, 0);
    format(string2, sizeof(string2), "Door_%d/InteriorZ", dCount); djSetFloat(DOORS_FILE, string2, 0);
    format(string2, sizeof(string2), "Door_%d/InteriorInt", dCount); djSetInt(DOORS_FILE, string2, 0);
    format(string2, sizeof(string2), "Door_%d/Name", dCount); djSet(DOORS_FILE, string2, name);
    LoadDoor(dCount);
    return 1;
}
The problem is when i use AddDoor function, it overrides Door_1 informations instead of creating new line in .ini file. and it fails to load informations from file, because pickup isn't created where it should be, but on 0, 0, 0 coords.
Reply
#2

pawn Код:
// Use this under OnGameModeInit() - for loading
stock LoadDoor(id)
{
    new string[128], string2[128], name[128];
    format(string, sizeof(string), "Door_%d", id);
    if(djIsSet(DOORS_FILE, string))
    {
        format(string2, sizeof(string2),"Door_%d/ExteriorX", id); DoorInfo[id][dExteriorX] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/ExteriorY", id); DoorInfo[id][dExteriorY] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/ExteriorZ", id); DoorInfo[id][dExteriorZ] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/ExteriorInt", id); DoorInfo[id][dExteriorInt] = djInt(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/InteriorX", id); DoorInfo[id][dInteriorX] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/InteriorY", id); DoorInfo[id][dInteriorY] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/InteriorZ", id); DoorInfo[id][dInteriorZ] = djFloat(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/InteriorInt", id); DoorInfo[id][dInteriorInt] = djInt(DOORS_FILE, string);
        format(string2, sizeof(string2),"Door_%d/Name", id); format(name, sizeof(name), "%s", dj(DOORS_FILE, string2)); DoorInfo[id][dName] = name;
        Itter_Add(Doors, Iter_Count(Doors) + 1);
        DoorInfo[id][dPp] = CreateDynamicPickup(1239, 2, DoorInfo[id][dExteriorX], DoorInfo[id][dExteriorY], DoorInfo[id][dExteriorZ], -1, DoorInfo[id][dExteriorInt], -1, 50.0);
        return 1;
    }
    return 1;
}
pawn Код:
stock AddDoor(Float:x, Float:y, Float:z, interior, name[])
{
    new string2[128];
    Itter_Add(Doors, Iter_Count(Doors) + 1);
    dCount =  Iter_Count(Doors) ;
    format(string2, sizeof(string2), "Door_%d/ExteriorX", dCount); djSetFloat(DOORS_FILE, string2, x);
    format(string2, sizeof(string2), "Door_%d/ExteriorY", dCount); djSetFloat(DOORS_FILE, string2, y);
    format(string2, sizeof(string2), "Door_%d/ExteriorZ", dCount); djSetFloat(DOORS_FILE, string2, z);
    format(string2, sizeof(string2), "Door_%d/ExteriorInt", dCount); djSetInt(DOORS_FILE, string2, interior);
    format(string2, sizeof(string2), "Door_%d/InteriorX", dCount); djSetFloat(DOORS_FILE, string2, 0);
    format(string2, sizeof(string2), "Door_%d/InteriorY", dCount); djSetFloat(DOORS_FILE, string2, 0);
    format(string2, sizeof(string2), "Door_%d/InteriorZ", dCount); djSetFloat(DOORS_FILE, string2, 0);
    format(string2, sizeof(string2), "Door_%d/InteriorInt", dCount); djSetInt(DOORS_FILE, string2, 0);
    format(string2, sizeof(string2), "Door_%d/Name", dCount); djSet(DOORS_FILE, string2, name);

    DoorInfo[dCount][dExteriorX] = x;
    DoorInfo[dCount][dExteriorY] = y;
    DoorInfo[dCount][dExteriorZ] =  z;
    DoorInfo[dCount][dExteriorInt] = interior;
    DoorInfo[dCount][dInteriorX] = // interior
    DoorInfo[dCount][dInteriorY] =  // interior
    DoorInfo[dCount][dInteriorZ] = // interior
    format(DoorInfo[dCount][dName], sizeof(DoorInfo[dCount][dName]), "%s", name);

    DoorInfo[dCount][dPp] = CreateDynamicPickup(1239, 2, DoorInfo[dCount][dExteriorX], DoorInfo[dCount][dExteriorY], DoorInfo[dCount][dExteriorZ], -1, DoorInfo[dCount][dExteriorInt], -1, 50.0);
    return 1;
}
Try this.
EDIT: You shouldn't use LoadDoor() function into AddDoor() function, because it will add 2 itters.
LoadDoor() should be used under ongamemodeinit , therefore , loading when server starts.
You should assign the right variables under AddDoor() not LoadDoor()
Reply
#3

Yay, thanks. AddDoor works fine now, but LoadDoor(id); function doesn't. By the way my RP from scratch is also called O:RP
Reply
#4

Quote:
Originally Posted by sekol
Посмотреть сообщение
Yay, thanks. AddDoor works fine now, but LoadDoor(id); function doesn't. By the way my RP from scratch is also called O:RP
Alright, can you tell me what exactly doesn't work on LoadDoor function?
Reply
#5

It looks like LoadDoor isn't correctly loading float and integer variables into DoorInfo enum.
I made a command "/omg [id]" which shows exterior coordinates of selected entrance and every id returns me "0.0000, 0.0000, 0.0000 0"(x, y, z, int)
Reply
#6

Little debugging won't hurt.
pawn Код:
stock LoadDoor(id)
{
    new string[128], string2[128], name[128];
    format(string, sizeof(string), "Door_%d", id);
    if(djIsSet(DOORS_FILE, string))
    {
        format(string2, sizeof(string2),"Door_%d/ExteriorX", id);
        printf("ExteriorX: %f", djFloat(DOORS_FILE, string));
        format(string2, sizeof(string2),"Door_%d/ExteriorY", id);
        printf("ExteriorY: %f", djFloat(DOORS_FILE, string));
        format(string2, sizeof(string2),"Door_%d/ExteriorZ", id);
        printf("ExteriorZ: %f", djFloat(DOORS_FILE, string));
        return 1;
    }
    return 1;
}
Can you tell me what does it print?
Reply
#7

It all print "0.000000".
G2g to school. I'm gonna check this thread later.
Reply
#8

WTF! I'm so stupid! I asked ofr help on polish forum and see!
printf("ExteriorX: %f", djFloat(DOORS_FILE, string[color = red]2[/color]));
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)