Finding right variables and randomly choosing one of them
#1

I realised my last thread wasnt informative enough so I decided to create a new one

I have 5 item names. I need to find which of these item values aren't equal zero and then randomly choose one of them (of those that aren't equal 0) and then give that item to player.

Code:
new a[5];
a[0]=GetItemValue(playerid, "chair");
a[1]=GetItemValue(playerid, "box");
a[2]=GetItemValue(playerid, "tv");
a[3]=GetItemValue(playerid, "couch");
a[4]=GetItemValue(playerid, "bed");
maybe I shouldn't use array, I don't know..

example:
chair is 2, box is 0, tv is 0, couch is 1, bed is 0. Since bed, tv and box is 0, I have to randomly choose from chair and couch. Let's say it's chair. Now I have to give item "chair" to player
Code:
GiveItem(playerid, "chair");
Reply
#2

If you are indexing arrays with numbers you are doing something wrong arrays are not really meant to be used like that unless you have good reason which would be very rare. Furthermore why are you using strings to identify items? That is a poor design choice these should all be integer values you would have something like this.

#define ITEM_TYPE_CHAIR 0
#define ITEM_TYPE_BOX 1

From my point of view the problem is all in the design it's flawed and those flaws manifest themselves later with either limitations, undefined behaviour, unneeded loops etc.
Reply
#3

Thank you for your suggestions, but is there a way to do it with what is given right now?
Reply
#4

pawn Code:
new indexes[5], count;
static const item_names[][] =
{
    {"chair"},
    {"box"},
    {"tv"},
    {"couch"},
    {"bed"}
};

for(new i = 0, j = sizeof(item_names); i < j; i ++)
{
    if(!GetItemValue(playerid, item_names[i][0]))
    {
        continue;
    }

    indexes[count] = i;
    count ++;
}

GiveItem(playerid, item_names[indexes[random(count)]][0]);
Reply
#5

Quote:
Originally Posted by SickAttack
View Post
pawn Code:
new indexes[5], count;
static const item_names[][] =
{
    {"chair"},
    {"box"},
    {"tv"},
    {"couch"},
    {"bed"}
};

for(new i = 0, j = sizeof(item_names); i < j; i ++)
{
    if(!GetItemValue(playerid, item_names[i][0]))
    {
        continue;
    }

    indexes[count] = i;
    count ++;
}

GiveItem(playerid, item_names[indexes[random(count)]][0]);
thank you!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)