[Tutorial] 2-dimensional arrays in enum
#1

Problem


Let's say I want to make a house storage where players can save weapons.

If I want to store 5 weapons (weapon ID and ammo) I would add the 'hStorage' to the house enum like this:

pawn Code:
enum eHouse
{
    hCreated,
    Float:hEnter[3],
    Float:hExit[3],
    hInterior,
    hOwner[MAX_PLAYER_NAME],
    hValue,
    hLock,
    hStorage[5][2],
    hPickup,
    Text3D:hLabel
};
new House[50][eHouse];
This doesn't work (gives error on compiling), but there is another way.


Solution


pawn Code:
const hStorage_size1 = 5;
const hStorage_size2 = 2;
const sizeof_hStorage = hStorage_size1 * hStorage_size2;
enum eHouse
{
    hCreated,
    Float:hEnter[3],
    Float:hExit[3],
    hInterior,
    hOwner[MAX_PLAYER_NAME],
    hValue,
    hLock,
    hStorage[sizeof_hStorage],
    hPickup,
    Text3D:hLabel
};
new House[50][eHouse];

#define hStorage][%1][%2] hStorage][((%1)*hStorage_size2)+(%2)]
This compiles fine and now I can use the 'hStorage' as I intended.

pawn Code:
House[houseid][hStorage][2][0] = WEAPON_MP5;
House[houseid][hStorage][2][1] = 500;
Looping:

pawn Code:
new weapon, ammo;
for(new i=0; i < hStorage_size1; i++)
{
    weapon = House[houseid][hStorage][i][0];
    ammo = House[houseid][hStorage][i][1];
    printf("[%d] weapon: %d   ammo: %d", i, weapon, ammo);
}
This method doesn't actually make 'hStorage' 2-dimensional, but it gives a feeling that it is.
Reply
#2

Very Nice, and useful, going to test this soon, thanks for sharing
I think though,
that
const hStorage_size1 = 5;
const hStorage_size2 = 2;
could better be changed to definitions
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)