Defining a shorter value for an Enum
#1

Um..Well, I use this variable to know which Character a User is Playing in. (MultiCharacter)
pawn Код:
new CurrentCharacter[MAX_PLAYERS];
So, I've to do like this. Example:-
pawn Код:
if(CharacterInfo[playerid][CurrentCharacter[playerid]][Admin] >= 5));
The original 'new' for the above equation is:
pawn Код:
new Character[MAX_PLAYERS][MAX_SLOT][CharacterInfo];
But, you see. This becomes very long. Maybe Defining helps, but I need in this regard. How to Make this shorter?
What I want it to work it like is.
pawn Код:
CharacterInfo[playerid][Admin]
i.e Remove the Character slot, 'SLOT', variable from this eq. Anybody?
Reply
#2

Not sure if this works but
pawn Код:
#define CC CurrentCharacter[playerid];
So it would be
pawn Код:
CharacterInfo[playerid][CC][Admin]
Doesn't remove it but makes it quite a bit shorter.
Reply
#3

Quote:
Originally Posted by Facerafter
Посмотреть сообщение
Not sure if this works but
pawn Код:
#define CC CurrentCharacter[playerid];
So it would be
pawn Код:
CharacterInfo[playerid][CC][Admin]
Doesn't remove it but makes it quite a bit shorter.
Didn't.
Reply
#4

Quote:
Originally Posted by ******
Посмотреть сообщение
This is what functions are for - abstracting repetitive and large code:

pawn Код:
CharacterAdminLevel(playerid)
{
    return CharacterInfo[playerid][CurrentCharacter[playerid]][Admin];
}

if (CharacterAdminLevel(playerid) >= 5)
{
}
This is a fundamental part of programming!
I don't object you Sir. But making such functions would be sorta tiring when I have like a 100 variables. A function for each and every variable in enum?
Reply
#5

why not advance that function so that we can tell it which character info we want
pawn Код:
#include    "a_samp"

#define     MAX_SLOTS   (4)

enum c_info
{
    bool: admin,
    cash,
    level
};

new CharacterInfo[MAX_PLAYERS][MAX_SLOTS][c_info];
new CurrentCharacter[MAX_PLAYERS];

public OnFilterScriptInit()
{
    //imaging a player with the id 1
    CurrentCharacter[1] = 3;
    //lets set some stuff
    CharacterInfo[1][CurrentCharacter[1]][admin] = true;
    CharacterInfo[1][CurrentCharacter[1]][cash] = 500;
    CharacterInfo[1][CurrentCharacter[1]][level] = 2;
   
    //now we're using our smart "getInfo" to
    //get the stuff we just set & print it
   
    printf("is ID 1 admin? %s",(getInfo(1,admin) ? ("YES") : ("NO")));
    printf("ID 1 - cash: %d",getInfo(1,cash));
    printf("ID 1 - level: %d",getInfo(1,level));
   
    return 1;
}

/**
* gets the info of the currect character
*
* @param playerid   -   simply the players ID
* @param type       -   the info we want
* @return The info, depending on type
* -------------------------------------
* Usage: new var = getInfo(playerid,info);
*/

getInfo(playerid,type)
{
    return CharacterInfo[playerid][CurrentCharacter[playerid]][c_info:type];
}
prints
Код:
----- Blank - Loaded! -----

Number of vehicle models: 0
loadfs smart
is ID 1 admin? YES
ID 1 - cash: 500
ID 1 - level: 2
  Filterscript 'smart.amx' loaded.
now we just
getInfo(playerid,WhatTypeOfInfo)
whenever we need to and it'll always be the correct info
you dont really have to write 100 functions, just make one smarter and its done

youcould also set info that way:
pawn Код:
/**
* sets the info of the currect character
*
* @param playerid   -   simply the players ID
* @param type       -   the info we want to set
* @param value      -   the value assigned to type
* @return true on success
* -------------------------------------
* Usage: setInfo(playerid,info,40);
*/

setInfo(playerid,type,value)
{
    //of course you have to think about if its a string or something... u know what i mean ;)
    CharacterInfo[playerid][CurrentCharacter[playerid]][c_info:type] = value;
    return true;//acknowledgement
}
simple, thats the cool ascpect of programming, we can make stuff smarter.
heck even create artificial intelligence if needed.
that way, we can let programs do stuff for us so that we have less to do ourselves
Reply
#6

Thanks ******.
Thanks CutX. That worked!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)