String vs loop
#1

Hi.
I'm trying to put the 'name' into the nearest empty cell of 'Item' array. The thing is, I can't logically script it in the working way that wouldn't be messing with all cells.
Any idea? Thanks! :- )

Код:
	for(new i = 0; i < 10; i++)
	{
		if(strlen(Statistics[playerid][Item][i]) == 0)
		{
		    format(Statistics[playerid][Item][i], 32, "%s", name);
		    break;
		}
	}
Reply
#2

The logic is correct, however to make it work the length of empty slots must always be zero (first slot (0) must be 0) - obviously.
If that's the case I don't see anything wrong there.
No matter what slot is the selected, because of break it's always just one that gets writen to.

Btw you can also replace the "%s" with name directly, no need to use a format specifier if you want to set it to the whole string without any actual formatting. Or use strcat since it's empty anyways.

Or did I get you wrong somehow?
Reply
#3

Well, take a look.
We got two example items as the Bun and Sprunk. It is supposed to show itself at place no. 0 and 1, somehow it is kind of differently as we can see below.

Reply
#4

Using isnull is way better than strlen as well as strcpy (a macro of strcat that resets the string before copying to destination) for copying strings instead of format.

You cannot have a 2D array inside an enumerator (I believe in Zeex's version of the compiler that is possible but not sure). As of now, you got 10 items with no size for the text.

EDIT: To be more precise: Consider Statistics[playerid][Item] as string
If you print string[0] will give all the characters, if you do string[1] will give all the characters except the first one that why the first letter disappears in every iterator.
Reply
#5

A - why don't you use isnull?
B - don't use format
C - Why don't you use IDs for this matter? Assign a unique ID to each item, save them in array, use them then if you want to know which item there is, just match the id to the array to get the name.
Reply
#6

Okay then. I've done it kind of better way than previously, but I'm getting stuck at bringing certain action by using certain type of our 'item'. Let's Have a look.
Код:
//somewhere there under dialog response callback
        if(response)
		{
			for(new i= 0; i < sizeof(items); i++)
			{
				if(strcmp(items[i], inputtext)) //not sure about that inputtext
				{
				    if(i == 0)
				    {

				    }
				    else if(i == 1)
				    {
				        SendClientMessage(playerid, -1, "Bun");
				    }
				    else if(i == 2)
				    {
				        SendClientMessage(playerid, -1, "Pizza");
				    }

					return 1;
				}
			}
It will always show the 'Bun' on the chat, no matter what type of item we use.
Reply
#7

strcmp returns 0 when both strings are equal (same) so modify that line. When using list with dialogs, "inputtext" holds the text of the line (row) you selected.
Reply
#8

Код:
new Items = 0;
for(new i = 0; i < 10; i++)
{
     if(!isnull(Statistics[playerid][Item][i]))
     {
           Items++;
           continue;
     }
     else
     {
            strmid(Statistics[playerid][Item][i], inputtext, 0, strlen(inputtext));
	    Items = 0;
	    break;
     }
}
That's how your method should be done.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)