Jump to content
  • 0
Zephy

countitem loop

Question

So i'm trying to avoid writing out all the 'countitem' stuff and just do it in a loop.

 

-	script	Deleter -1,{		for(.@i = 0; .@i < getarraysize(.item[0]); .@i++)	{		if(countitem(.item[.@i]) >= .amt)		{			delitem .item[.@i], .amt;		}	}	setarray .item[0], 501, 502, 503, 504;	.amt = 1;}

 

But for some reason it doesn't work. Can anyone tell me where my error is exactly? Did I do the logic wrong?

Share this post


Link to post
Share on other sites

15 answers to this question

Recommended Posts

  • 0
-	script	Deleter -1,{		for(set .@i, 0; .@i <= getarraysize(.item); .@i++)	{		if(countitem(.item[.@i]) >= .amt)		{			delitem .item[.@i], .amt;		}	}OnInit:	setarray .item[0], 501, 502, 503, 504;	.amt = 1;end;}

Please try.

Share this post


Link to post
Share on other sites
  • 0

So i'm trying to avoid writing out all the 'countitem' stuff and just do it in a loop.

 

 

-	script	Deleter -1,{		for(.@i = 0; .@i < getarraysize(.item[0]); .@i++)	{		if(countitem(.item[.@i]) >= .amt)		{			delitem .item[.@i], .amt;		}	}	setarray .item[0], 501, 502, 503, 504;	.amt = 1;}
 

But for some reason it doesn't work. Can anyone tell me where my error is exactly? Did I do the logic wrong?

 

 

You should always declare something before trying to use it, otherwise the server won't be able to identify those variables when it reads the script. You could also use this example:
-	script	Deleter -1,{	// Declare your variables before using them	setarray .item[0], 501, 502, 503, 504;	.amt = 1;	// Usually commands that identify _arrays_ use only names to identify them, without '[' ']'	for(.@i = 0; .@i < getarraysize(.item); .@i++)	{		// It's possible not to use brackets '{' '}' when there's only one line after an 'if' or an 'for'		// You could also remove the brackets from this 'for', but it would make the script more difficult to read		if(countitem(.item[.@i]) >= .amt)			delitem .item[.@i], .amt;	}}
Mhalicot's example works because it uses an initialization event ('OnInit') that is executed (parsed) before the rest of the script.

Hope I've helped c:

Share this post


Link to post
Share on other sites
  • 0

Sorry :x. This was just a theoretical "snippet" from that script so you could get an idea of the loop. In the original one all variables are declared under 'OnInit'. However, it still does not work.

Share this post


Link to post
Share on other sites
  • 0

Have you changed your

getarraysize(.item[0])
to
getarraysize(.item)
?

What happens when you run your script?

Share this post


Link to post
Share on other sites
  • 0

before you running  countitem  and delitem  command,  you need to attach  an on-line player

Considering he said it's a snippet I assume we're all assuming this is either inside a fully functioning script that requires a player to click it to start, or talk to it via OnWhisperGlobal

Share this post


Link to post
Share on other sites
  • 0

 

 

before you running  countitem  and delitem  command,  you need to attach  an on-line player

Considering he said it's a snippet I assume we're all assuming this is either inside a fully functioning script that requires a player to click it to start, or talk to it via OnWhisperGlobal

 

 

Maybe it's not possible to use this syntax?
for(.@i = 0; .@i < getarraysize(.item); .@i++)
I'm not sure whether the emulator can or can't parse var = val inside 'for's or 'if's

Share this post


Link to post
Share on other sites
  • 0

 

 

before you running  countitem  and delitem  command,  you need to attach  an on-line player

Considering he said it's a snippet I assume we're all assuming this is either inside a fully functioning script that requires a player to click it to start, or talk to it via OnWhisperGlobal

 

Maybe it's not possible to use this syntax?
for(.@i = 0; .@i < getarraysize(.item); .@i++)
I'm not sure whether the emulator can or can't parse var = val inside 'for's or 'if's

It should be able to, else it would error.

http://upaste.me/8c97c0 ran on upaste without any issues.

 

 

^ Same thought thats why in my sample I revise it to

for(set .@i, 0; .@i <= getarraysize(.item); .@i++)

You missed a set in your second case ^^ ".@i++"

Share this post


Link to post
Share on other sites
  • 0

I don't think that's it because I have the same loop to print out the requirements and its...

 

for(set .@i, 0; .@i <= getarraysize(.item[0]); .@i++)mes ^FF0000"+ .amt +"x -"+ getitemname(item[0]) +"^000000^";

 

...and it prints all the items out correctly.

 

 

EDIT: I think that whoever said a character was not attached was right. When I looked at the emulator (before changing the code) it kept giving me an error that the player was not attached (which is weird since the player has to initiate the script by talking to the NPC). The only real problem I saw with that was that it wasn't displaying the '.@player_name$' variable. So I changed the code to remove the error from the emulator (e.g. take out the '.@player_name$' variable). I'll keep messing around with it, especially since Nameless2you said the syntax was correct and it worked. 

 

Thanks everyone :x.

Edited by Zephy

Share this post


Link to post
Share on other sites
  • 0

Well 2 options, 1 show the entire script so we can debug it or 2, check if the script is attached.

You can do this by placing:

 

mes "Character ID: "+getcharid(0);

Within the for loop, if the ID is 0 the character isn't attached.

Share this post


Link to post
Share on other sites
  • 0

if you are trying to delete everything in player's inventory

prontera,155,180,5	script	Deleter	100,{	getinventorylist;	for ( .@i = 0; .@i < @inventorylist_count; .@i++ )		delitem @inventorylist_id[.@i], @inventorylist_amount[.@i];	end;}

Share this post


Link to post
Share on other sites
  • 0

Damn this is pissing me off. For some reason when I try to use my script it says that my player is not attached. Eeff it, here's the actual script.

 

prontera,155,150,3	script	TEST_NPC1	99,{	// Loop to check if player has the required items	for(.@i = 0; .@i < getarraysize(.qitem[0]); .@i++)	{		if(countitem(.qitem[.@i] >= .qamt)		{			mes .npc_name$;			mes "congrats dialogue for gather items.";						delitem .qitem[.@i],.qamt;			getitem .pitem,.pamt;						close;		}	}	// Dialogue if player does NOT have the required items already	mes .npc_name$;	mes "dialogue hereeeee.";	mes "More hereeeee";	next;		mes .@player_name$;	mes "PC dialogue here";	next;		if(select("Yes, please.:No, thank you.") == 2)	{		mes .npc_name$;		mes "leaving message here";		close;	}	mes .npc_name$;	mes "more dialogue";	next	mes .npc_name$;	mes "Come back with:";		// List items	for(.@i = 0; .@i < getarraysize(.qitem[0]); .@i++)		mes "^FF0000"+ .qamt +"x - "+ getitemname(.qitem[.@i]) +"^000000";			mes "...and I will give you the item.";	close;OnInit:	// Configuration		// General		.npc_name$ = "[^008800NPC_NAME^000000]"; // NPC's dialogue name		.@player_name$ = "[^FF0000"+strcharinfo(0)+"^000000]";		// Items		.pitem = 901; // Prize item for completing the quest		.pamt = 1; // Prize item amount		setarray .qitem[0], 501, 502, 503;		.qamt = 1; // Quest item amount			end;}

 

 

 

 

 

 

 

I guess I should mention that I'm getting this strange error as well, even though I don't have that in my script ._. .

[Error]: npc_read_event_script: detected possible use of wrong case in a script. Found '::OnPcLoginEvent', probably meant to be '::OnPCLoginEvent' (in 'OnPCLoginEvent').
Edited by Zephy

Share this post


Link to post
Share on other sites
  • 0

Players will only be attached to the npc whenever they talk to it or when the script execute certain labels or by using addrid or attachrid. In your case you put this line on OnInit label :

 

.@player_name$ = "[^FF0000"+strcharinfo(0)+"^000000]";

 

Which means that when the script was loaded by the server it will assign the name of a player on the variable .@player_name$ ( in which case by default and by loaded i believe no player is currently talking to that npc )

 

Regarding the wrong case error. Maybe it was included in your other scripts. I am certain that the error does not belong to the script you are talking about in this topic.

 

I just modified your script. Kindly test this one and tell me if it works :

 

prontera,155,150,3	script	TEST_NPC1	99,{	.@player_name$ = "[^FF0000"+strcharinfo(0)+"^000000]";	// No requirements	for( .@i = 0; .@i < getarraysize(.qitem); .@i++ ) {		if ( countitem( .qitem[.@i] ) < .qamt ) {			// Dialogue if player does NOT have the required items already			mes .npc_name$;			mes "dialogue hereeeee.";			mes "More hereeeee";			next;						mes .@player_name$;			mes "PC dialogue here";			next;						if(select("Yes, please.:No, thank you.") == 2)			{				mes .npc_name$;				mes "leaving message here";				close;			}			mes .npc_name$;			mes "more dialogue";			next;			mes .npc_name$;			mes "Come back with:";						// List items			for(.@i = 0; .@i < getarraysize(.qitem); .@i++)				mes "^FF0000"+ .qamt +"x - "+ getitemname(.qitem[.@i]) +"^000000";							mes "...and I will give you the item.";			close;		}	}	// Has requirements	mes .npc_name$;	mes "congrats dialogue for gather items.";	for( .@i = 0; .@i < getarraysize(.qitem); .@i++ )		delitem .qitem[.@i],.qamt;	getitem .pitem,.pamt;	close;OnInit:	// Configuration		// General		.npc_name$ = "[^008800NPC_NAME^000000]"; // NPC's dialogue name		// Items		.pitem = 901; // Prize item for completing the quest		.pamt = 1; // Prize item amount		setarray .qitem[0], 501, 502, 503;		.qamt = 1; // Quest item amount		end;}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.