Zephy 4 Posted January 3, 2014 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? Quote Share this post Link to post Share on other sites
0 Mhalicot 392 Posted January 3, 2014 - 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. Quote Share this post Link to post Share on other sites
0 pan 87 Posted January 4, 2014 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: Quote Share this post Link to post Share on other sites
0 Zephy 4 Posted January 4, 2014 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. Quote Share this post Link to post Share on other sites
0 pan 87 Posted January 4, 2014 Have you changed your getarraysize(.item[0])togetarraysize(.item)?What happens when you run your script? 1 Mumbles reacted to this Quote Share this post Link to post Share on other sites
0 Nameless2you 97 Posted January 4, 2014 You also might want to add an 'end;' before the 'OnInit:'. 1 Mumbles reacted to this Quote Share this post Link to post Share on other sites
0 Angelmelody 221 Posted January 4, 2014 (edited) before you running countitem and delitem command, you need to attach an on-line player Edited January 4, 2014 by Angelmelody 1 Mumbles reacted to this Quote Share this post Link to post Share on other sites
0 Nameless2you 97 Posted January 4, 2014 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 Quote Share this post Link to post Share on other sites
0 pan 87 Posted January 4, 2014 before you running countitem and delitem command, you need to attach an on-line playerConsidering 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 Quote Share this post Link to post Share on other sites
0 Mhalicot 392 Posted January 4, 2014 ^ Same thought thats why in my sample I revise it to for(set .@i, 0; .@i <= getarraysize(.item); .@i++) Quote Share this post Link to post Share on other sites
0 Nameless2you 97 Posted January 4, 2014 before you running countitem and delitem command, you need to attach an on-line playerConsidering 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++" 1 pan reacted to this Quote Share this post Link to post Share on other sites
0 Zephy 4 Posted January 4, 2014 (edited) 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 January 4, 2014 by Zephy Quote Share this post Link to post Share on other sites
0 Nameless2you 97 Posted January 4, 2014 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. Quote Share this post Link to post Share on other sites
0 AnnieRuru 957 Posted January 4, 2014 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;} Quote Share this post Link to post Share on other sites
0 Zephy 4 Posted January 4, 2014 (edited) 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 January 4, 2014 by Zephy Quote Share this post Link to post Share on other sites
0 Patskie 88 Posted January 4, 2014 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;} 1 Mumbles reacted to this Quote Share this post Link to post Share on other sites
So i'm trying to avoid writing out all the 'countitem' stuff and just do it in a loop.
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