hakuren Posted September 15, 2016 Group: Members Topic Count: 120 Topics Per Day: 0.02 Content Count: 295 Reputation: 6 Joined: 12/02/11 Last Seen: November 6, 2023 Share Posted September 15, 2016 Hi rAthena, Question : - How can a npc script give 5 different items randomly in a array at the same-time. Example : - setarray .@item_id, 501,502,503,504,505,506,507,508,509,510; can someone help me? Thank you in advance Quote Link to comment Share on other sites More sharing options...
0 Tokei Posted September 15, 2016 Group: Members Topic Count: 16 Topics Per Day: 0.00 Content Count: 695 Reputation: 721 Joined: 11/12/12 Last Seen: 23 hours ago Share Posted September 15, 2016 Just wanted to point out that .@index = rand(0, getarraysize(.@item_ids) - 1); is the equivalent of .@index = rand(getarraysize(.@item_ids)); Except it's easier to read xD. Also, you should avoid using temporary char-bound variables in scripts (@item_id versus .@item_id), those are kept until the player logs out. The usage of "set" is also deprecated, you should be using 'variable = value;". prontera,137,203,4 script TEST::KST01 834,{ setarray .@item_ids, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510; for (.@i = 0; .@i < 5; .@i++) { .@index = rand(getarraysize(.@item_ids)); getitem .@item_ids[.@index], 1; deletearray .@item_ids[.@index], 1; } end; } 1 Quote Link to comment Share on other sites More sharing options...
0 Kamii Posted September 15, 2016 Group: Members Topic Count: 5 Topics Per Day: 0.00 Content Count: 21 Reputation: 1 Joined: 06/10/14 Last Seen: September 26, 2016 Share Posted September 15, 2016 You need to use for loop.Pseudocode: for (i=0; i< 5; i++) { getitem @item_id[rand(0,10)], 1; } Quote Link to comment Share on other sites More sharing options...
0 hakuren Posted September 15, 2016 Group: Members Topic Count: 120 Topics Per Day: 0.02 Content Count: 295 Reputation: 6 Joined: 12/02/11 Last Seen: November 6, 2023 Author Share Posted September 15, 2016 (edited) You need to use for loop. Pseudocode: for (i=0; i< 5; i++) { getitem @item_id[rand(0,10)], 1; } Hi @Kamii thank you for your reply i use this like this setarray .@item_id, 501,502,503,504,505,506,507,508,509,510; for (i=0; i< 5; i++) { getitem .@item_id[rand(0,10)], 1; } yes it gives 5 items but... some on it give the same item when the random value is the same as the preview value i need that 5 random items differently when receive by the player. Edited September 15, 2016 by hakuren Quote Link to comment Share on other sites More sharing options...
0 Kamii Posted September 15, 2016 Group: Members Topic Count: 5 Topics Per Day: 0.00 Content Count: 21 Reputation: 1 Joined: 06/10/14 Last Seen: September 26, 2016 Share Posted September 15, 2016 (edited) Then you have to try this one. NOTE: can't check if code is correct right now... setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;for (@i=0; @i< 5; @i++){ set @ind, rand(0,getarraysize(@item_id)); getitem .@item_id[@ind], 1; deletearray @item_id[@ind],1;} Corrected Script: prontera,137,203,4 script TEST 834,{ setarray @item_id, 501,502,503,504,505,506,507,508,509,510; for (@i=0; @i< 5; @i++) { set @ind, rand(0, getarraysize(@item_id) - 1); getitem @item_id[@ind], 1; deletearray @item_id[@ind],1; } } Edited September 15, 2016 by Kamii Quote Link to comment Share on other sites More sharing options...
0 a91323 Posted September 15, 2016 Group: Members Topic Count: 15 Topics Per Day: 0.00 Content Count: 99 Reputation: 10 Joined: 01/13/12 Last Seen: December 6, 2020 Share Posted September 15, 2016 Hi rAthena, Question : - How can a npc script give 5 different items randomly in a array at the same-time. Example : - setarray .@item_id, 501,502,503,504,505,506,507,508,509,510; can someone help me? Thank you in advance prontera,155,125,4 script test 4_m_genie,{ setarray .@item_id[0],501,502,503,504,505,506,507,508,509,510; for(.@i=0; .@i<5;.@i++){ .@pass = 0; while(1){ // random item .@item = .@item_id[rand(0,getarraysize(.@item_id)-1)]; if(getarraysize(.@del) != 0){ for(.@j=0; .@j<getarraysize(.@del);.@j++){ if(.@del[.@j]==.@item) // repeat break; if(.@j == (getarraysize(.@del)-1)) .@pass = 1; } } else { break; } if(.@pass == 1) break; } getitem .@item,1; // record gived .@del[.@i] = .@item; } end; } Quote Link to comment Share on other sites More sharing options...
0 hakuren Posted September 15, 2016 Group: Members Topic Count: 120 Topics Per Day: 0.02 Content Count: 295 Reputation: 6 Joined: 12/02/11 Last Seen: November 6, 2023 Author Share Posted September 15, 2016 (edited) Hi rAthena, Question : - How can a npc script give 5 different items randomly in a array at the same-time. Example : - setarray .@item_id, 501,502,503,504,505,506,507,508,509,510; can someone help me? Thank you in advance prontera,155,125,4 script test 4_m_genie,{ setarray .@item_id[0],501,502,503,504,505,506,507,508,509,510; for(.@i=0; .@i<5;.@i++){ .@pass = 0; while(1){ // random item .@item = .@item_id[rand(0,getarraysize(.@item_id)-1)]; if(getarraysize(.@del) != 0){ for(.@j=0; .@j<getarraysize(.@del);.@j++){ if(.@del[.@j]==.@item) // repeat break; if(.@j == (getarraysize(.@del)-1)) .@pass = 1; } } else { break; } if(.@pass == 1) break; } getitem .@item,1; // record gived .@del[.@i] = .@item; } end; } woooow! thank you @a91323 your script perfectly works! to my condition thankyou so much!!! and thankyou @Kamii Edited September 15, 2016 by hakuren Quote Link to comment Share on other sites More sharing options...
0 Kamii Posted September 15, 2016 Group: Members Topic Count: 5 Topics Per Day: 0.00 Content Count: 21 Reputation: 1 Joined: 06/10/14 Last Seen: September 26, 2016 Share Posted September 15, 2016 Keep in mind, that this script is really bad performance wise.It's not wrong, but it's really not optimized. Quote Link to comment Share on other sites More sharing options...
0 hakuren Posted September 15, 2016 Group: Members Topic Count: 120 Topics Per Day: 0.02 Content Count: 295 Reputation: 6 Joined: 12/02/11 Last Seen: November 6, 2023 Author Share Posted September 15, 2016 Keep in mind, that this script is really bad performance wise. It's not wrong, but it's really not optimized. yeah i think it will give unlimited loop when the rand value return the same correct me if im wrong or can you give me some idea to optimize this? Quote Link to comment Share on other sites More sharing options...
0 Kamii Posted September 15, 2016 Group: Members Topic Count: 5 Topics Per Day: 0.00 Content Count: 21 Reputation: 1 Joined: 06/10/14 Last Seen: September 26, 2016 Share Posted September 15, 2016 Maybe a91323 can optimizie his code by himself.I'll be home in about 1 1/2 hour.You could also try out my second code... I'll test it when i'm home. Quote Link to comment Share on other sites More sharing options...
0 hakuren Posted September 15, 2016 Group: Members Topic Count: 120 Topics Per Day: 0.02 Content Count: 295 Reputation: 6 Joined: 12/02/11 Last Seen: November 6, 2023 Author Share Posted September 15, 2016 setarray .@item_id, 501,502,503,504,505,506,507,508,509,510; for (@i=0; @i< 5; @i++) { set .@ind, rand(0,getarraysize(.@item_id)); getitem .@item_id[.@ind], 1; deletearray .@item_id[.@ind],1; } i tried it but when the rand value return to previews deletearray value it return 0 and give error Quote Link to comment Share on other sites More sharing options...
0 a91323 Posted September 15, 2016 Group: Members Topic Count: 15 Topics Per Day: 0.00 Content Count: 99 Reputation: 10 Joined: 01/13/12 Last Seen: December 6, 2020 Share Posted September 15, 2016 (edited) setarray .@item_id, 501,502,503,504,505,506,507,508,509,510; for (@i=0; @i< 5; @i++) { set .@ind, rand(0,getarraysize(.@item_id)); getitem .@item_id[.@ind], 1; deletearray .@item_id[.@ind],1; } i tried it but when the rand value return to previews deletearray value it return 0 and give error because .@item_id≠@item_id and set @ind, rand(0,getarraysize(.@item_id)-1); deletearray <array name>[<first value>],<how much to delete>; prontera,155,125,4 script test 4_m_genie,{ setarray .@item_id[0],501,502,503,504,505,506,507,508,509,510; for(.@i=0; .@i<5;.@i++){ .@pass = 0; // random item .@item = .@item_id[rand(0,getarraysize(.@item_id)-1)]; if(getarraysize(.@del) != 0){ for(.@j=0; .@j<getarraysize(.@del);.@j++){ if(.@del[.@j]==.@item) // repeat break; if(.@j == (getarraysize(.@del)-1)) .@pass = 1; } } else { .@pass = 1; } if(.@pass == 1){ getitem .@item,1; // record gived .@del[.@i] = .@item; } else { .@i -= 1; } } end; } Edited September 15, 2016 by a91323 Quote Link to comment Share on other sites More sharing options...
0 hakuren Posted September 15, 2016 Group: Members Topic Count: 120 Topics Per Day: 0.02 Content Count: 295 Reputation: 6 Joined: 12/02/11 Last Seen: November 6, 2023 Author Share Posted September 15, 2016 setarray .@item_id, 501,502,503,504,505,506,507,508,509,510; for (@i=0; @i< 5; @i++) { set .@ind, rand(0,getarraysize(.@item_id)); getitem .@item_id[.@ind], 1; deletearray .@item_id[.@ind],1; } i tried it but when the rand value return to previews deletearray value it return 0 and give error because .@item_id≠@item_id and set @ind, rand(0,getarraysize(.@item_id)-1); deletearray <array name>[<first value>],<how much to delete>; prontera,155,125,4 script test 4_m_genie,{ setarray .@item_id[0],501,502,503,504,505,506,507,508,509,510; for(.@i=0; .@i<5;.@i++){ .@pass = 0; // random item .@item = .@item_id[rand(0,getarraysize(.@item_id)-1)]; if(getarraysize(.@del) != 0){ for(.@j=0; .@j<getarraysize(.@del);.@j++){ if(.@del[.@j]==.@item) // repeat break; if(.@j == (getarraysize(.@del)-1)) .@pass = 1; } } else { .@pass = 1; } if(.@pass == 1){ getitem .@item,1; // record gived .@del[.@i] = .@item; } else { .@i -= 1; } } end; } wow another knowledge acquired thankyou for the information thank you again... Quote Link to comment Share on other sites More sharing options...
0 Kamii Posted September 15, 2016 Group: Members Topic Count: 5 Topics Per Day: 0.00 Content Count: 21 Reputation: 1 Joined: 06/10/14 Last Seen: September 26, 2016 Share Posted September 15, 2016 Alright i corrected my script. prontera,137,203,4 script TEST::KST01 834,{ setarray @item_id, 501,502,503,504,505,506,507,508,509,510; for (@i=0; @i< 5; @i++) { set @ind, rand(0, getarraysize(@item_id) - 1); getitem @item_id[@ind], 1; deletearray @item_id[@ind],1; } } It works now as intented. (If someone else needs that kind of script.) Quote Link to comment Share on other sites More sharing options...
0 Kamii Posted September 15, 2016 Group: Members Topic Count: 5 Topics Per Day: 0.00 Content Count: 21 Reputation: 1 Joined: 06/10/14 Last Seen: September 26, 2016 Share Posted September 15, 2016 Just wanted to point out that .@index = rand(0, getarraysize(.@item_ids) - 1); is the equivalent of .@index = rand(getarraysize(.@item_ids)); Except it's easier to read xD. Also, you should avoid using temporary char-bound variables in scripts (@item_id versus .@item_id), those are kept until the player logs out. The usage of "set" is also deprecated, you should be using 'variable = value;". prontera,137,203,4 script TEST::KST01 834,{ setarray .@item_ids, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510; for (.@i = 0; .@i < 5; .@i++) { .@index = rand(getarraysize(.@item_ids)); getitem .@item_ids[.@index], 1; deletearray .@item_ids[.@index], 1; } end; } That Sir, are some useful informations. I did my last script on 2007 or 2008... So my knowledge is pretty old on this one. I'm gonna have to redo my new scripts... Quote Link to comment Share on other sites More sharing options...
Question
hakuren
Hi rAthena,
Question :
- How can a npc script give 5 different items randomly in a array at the same-time.
Example :
- setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;
can someone help me?
Thank you in advance
Link to comment
Share on other sites
14 answers to this question
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.