countitem loop

Zephy

New member
Messages
20
Points
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?

 
- 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.

 
Last edited by a moderator:
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:

 
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.

 
Have you changed your

getarraysize(.item[0])to
Code:
getarraysize(.item)
?What happens when you run your script?

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

 
Last edited by a moderator:
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

 
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?
Code:
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
 
^ Same thought thats why in my sample I revise it to

Code:
for(set .@i, 0; .@i <= getarraysize(.item); .@i++)
 
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++"

 
  • Upvote
Reactions: pan
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.

 
Last edited by a moderator:
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.

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

Code:
prontera,155,180,5	script	Deleter	100,{	getinventorylist;	for ( .@i = 0; .@i < @inventorylist_count; .@i++ )		delitem @inventorylist_id[.@i], @inventorylist_amount[.@i];	end;}
 
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 ._. .

Code:
[Error]: npc_read_event_script: detected possible use of wrong case in a script. Found '::OnPcLoginEvent', probably meant to be '::OnPCLoginEvent' (in 'OnPCLoginEvent').
 
Last edited by a moderator:
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 :

Code:
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;}
 
Back
Top