Getting name of array
#1

Let's say for an example I have a array with the objective of holding strings, such as "cow", "Potato", etc.
The problem here is not getting the name of this as I obviously understand that, but the main issue is getting the name of an integer. Let me explain with code.
Код:
new itemnames[MAX_ITEMS][] = {
"potato",
"potato2"
};
Код:
for(new i; i!= MAX_INVENTORY; i++)
{
format(str, sizeof(str), "ItemName: %s",itemnames[invslot[playerid][i]);
}
What I mean by this is getting the itemname dependent on what [i] is, so if the inventoryslot 0 would be 0 it would be potato 1 potato2. I honestly got no clue at this point, as doing it simply as above "itemname[invslot[playerid][i]]", gives me a clear Array index out bounds error.

If someone could help me, I'd appreciate it massively!
Thanks!
Reply
#2

Use:
pawn Код:
itemnames[index_here]
instead.
If you use the second dimension, it will take only 1 character of the string. For example:
pawn Код:
itemnames[0]
will return "potato" and with index [1] instead of 0, it will return "potato2". I'm not sure if you mean the array index out of bounds as a run time error (and not while compiling) but if that's the case, then you should loop through the size of the itemnames.
pawn Код:
for(new i; i != MAX_ITEMS; i++)
// OR:
for(new i; i != sizeof (itemnames); i++)
if MAX_INVENTORY is a number greater than the size above, it will cause the run time error.
Reply
#3

I meant a run time error(my fault for not being clearer), I understand the first things you mention, and the solution to this must be looping through itemnames then, I assume? (If I understood correctly)

Which I did and gave me the following run-time error:

Accessing element at index 15 past array upper bound 14

The thing is also, that MAX_INVENTORY is defined 15, it's not bigger than MAX_ITEMS, actually a lot smaller
Код:
for(new p; p != sizeof (itemname); p++)
{
format(str, sizeof(str), "itemname %s",
invslots[playerid][p]);
// feels kinda off doing that xD must be smth wrong
Reply
#4

There's lack of information so please post how MAX_INVENTORY and MAX_ITEMS are defined. Also how you declared invslots because that's what you use in the loop with the size of itemname (they're different arrays).
Reply
#5

Код:
#define MAX_INVENTORY 15
#define MAX_ITEMS 51

new invslots[MAX_PLAYERS][MAX_INVENTORY];
Been stuck at this for over a week now, so thanks for actually helping me
Reply
#6

One more question. Is invslots[playerid][some_index] supposed to hold the item? So you want to use that item in itemname and retrieve its name? If so an example would be:
pawn Код:
// let's say invslots[playerid][0] holds an item

if (0 <= invslots[playerid][0] < MAX_INVENTORY) // making sure that it's in bounds
{
    new
        str[32] = "ItemName: ";

    strcat(str, itemnames[invslots[playerid][0]], sizeof (str)); // faster than using format
}
0 was just an example. It could be a value as variable. If I understood incorrect, then please explain what you want to do.
Reply
#7

Yes, you're completely right, I want the MAX_INVENTORY index, in a loop, name. However I just tried the code and still returns the same error(no idea why tbh) // Accessing element at index 15 past array upper bound 14


One thing I did not mention was that I'm formatting a string like "Itemname: %s, ID(%d)",blabla,balbal);
But all of these things register in the dialog I want to get(ID, durability of item etc), except for the itemname, when I tried before to get the itemname of invslots[playerid][MAX_INVENTORY] it did not show with similar run time errors.
What I mean by did not show, the dialog just doesn't show with similar errors of Array Index Out Of Bounds,



I was able to get itemnames in SCM and dialogs when not in a loop with the function below, but directly when looped, it did not work. I used this function for that btw. So what appears with this function is that it doesn't work in loops at all. And same thing with the code you sent above, gives me the same errors as well. // upper 15 blabla


Stock:
Код:
stock GetItemName(id, name[], len) return format(name, len, "%s", itemname[id]);

Error:
[/code]
Accessing element at index 15 past array upper bound 14
[code]

I'll send you the entire code in a minute in PM so you can look at it.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)