mawjustin Posted February 17, 2023 Group: Members Topic Count: 48 Topics Per Day: 0.01 Content Count: 124 Reputation: 6 Joined: 09/26/14 Last Seen: August 20 Share Posted February 17, 2023 Hi Team, May I ask if this is possible? I have a sample here that requires user to select which choice a player will choose. Instead of selecting from switch expression, can we randomize this without changing the switch statement? The script will randomly choose from Case 1 to Case 4 without changing the switch statement. Is this possible? switch(expression) { case x: // code block break; case y: // code block break; default: // code block } Thank you. Quote Link to comment Share on other sites More sharing options...
0 Emistry Posted April 25, 2023 Group: Forum Moderator Topic Count: 93 Topics Per Day: 0.02 Content Count: 10015 Reputation: 2357 Joined: 10/28/11 Last Seen: Saturday at 02:34 PM Share Posted April 25, 2023 On 2/17/2023 at 1:17 PM, Virtue said: have you tried switch(select(rand(1,x))? it should be switch (rand(1, 4)) { ... ... } Quote Link to comment Share on other sites More sharing options...
1 Virtue Posted February 17, 2023 Group: Members Topic Count: 92 Topics Per Day: 0.02 Content Count: 354 Reputation: 22 Joined: 11/17/11 Last Seen: May 12 Share Posted February 17, 2023 have you tried switch(select(rand(1,x))? 1 Quote Link to comment Share on other sites More sharing options...
1 pajodex Posted February 17, 2023 Group: Members Topic Count: 79 Topics Per Day: 0.03 Content Count: 439 Reputation: 172 Joined: 12/12/17 Last Seen: 10 minutes ago Share Posted February 17, 2023 (edited) Yes, should be possible. try this : // This script just shuffles the menu.. not related to the original post function script F_ShuffleNumbers { deletearray getarg(2); .@static = getarg(0); .@range = getarg(1) +1 - .@static; .@count = getarg(3, .@range); if (.@range <= 0 || .@count <= 0) return 0; if (.@count > .@range) .@count = .@range; for (.@i = 0; .@i < .@range; ++.@i) .@temparray[.@i] = .@i; for (.@i = 0; .@i < .@count; ++.@i) { .@rand = rand(.@range); set getelementofarray( getarg(2), .@i ), .@temparray[.@rand] + .@static; .@temparray[.@rand] = .@temparray[--.@range]; } return .@count; } prontera,150,180,0 script Test 100,{ setarray .@s$, "Menu 1", "Menu 2", "Menu 3", "Menu 4"; callfunc "F_ShuffleNumbers", 0, getarraysize(.@s$)-1, .@r; for ( .@i = 0; .@i < getarraysize(.@s$); .@i++ ) { .@sel$[.@i] = .@s$[.@r]; .@menu$ += .@s$[.@r] + ":"; } .@i = select(.@menu$)-1; if ( .@sel$[.@i] == "Menu 1" ) { mes "this happens 1"; } else if ( .@sel$[.@i] == "Menu 2" ) { mes "this happens 2"; } else if ( .@sel$[.@i] == "Menu3" ) { mes "this happens 3"; } else if ( .@sel$[.@i] == "Menu 4" ) { mes "this happens 4"; } close; } This is untested.. didnt have time to think thoroughly because im about to head to work but your question is quite interesting so I had to answer EDIT: I noticed some mistakes but will correct it later after work.. [fixed] EDIT 2: upon re-reading your question, it made more sense.. this is the correct one.. I will leave my initial answer here if someone needs it.. 12 hours ago, Virtue said: have you tried switch(select(rand(1,x))? Edited February 17, 2023 by pajodex Quote Link to comment Share on other sites More sharing options...
0 MarieDigby Posted April 20, 2023 Group: Members Topic Count: 0 Topics Per Day: 0 Content Count: 1 Reputation: 0 Joined: 04/20/23 Last Seen: April 25, 2023 Share Posted April 20, 2023 If you haven't tried switch(select(rand(1,x)) then I recommend you to use.or you can follow Pooring Scripter's way On 2/17/2023 at 12:33 PM, pajodex said: Yes, should be possible. try this : // This script just shuffles the menu.. not related to the original post function script F_ShuffleNumbers { deletearray getarg(2); .@static = getarg(0); .@range = getarg(1) +1 - .@static; .@count = getarg(3, .@range); if (.@range <= 0 || .@count <= 0) return 0; if (.@count > .@range) .@count = .@range; for (.@i = 0; .@i < .@range; ++.@i) .@temparray[.@i] = .@i; for (.@i = 0; .@i < .@count; ++.@i) { .@rand = rand(.@range); set getelementofarray( getarg(2), .@i ), .@temparray[.@rand] + .@static; .@temparray[.@rand] = .@temparray[--.@range]; } return .@count; } prontera,150,180,0 script Test 100,{ setarray .@s$, "Menu 1", "Menu 2", "Menu 3", "Menu 4"; callfunc "F_ShuffleNumbers", 0, getarraysize(.@s$)-1, .@r; for ( .@i = 0; .@i < getarraysize(.@s$); .@i++ ) { .@sel$[.@i] = .@s$[.@r]; .@menu$ += .@s$[.@r] + ":"; } .@i = select(.@menu$)-1; if ( .@sel$[.@i] == "Menu 1" ) { mes "this happens 1"; } else if ( .@sel$[.@i] == "Menu 2" ) { mes "this happens 2"; } else if ( .@sel$[.@i] == "Menu3" ) { mes "this happens 3"; } else if ( .@sel$[.@i] == "Menu 4" ) { mes "this happens 4"; } close; } This is untested.. didnt have time to think thoroughly because im about to head to work but your question is quite interesting so I had to answer EDIT: I noticed some mistakes but will correct it later after work.. [fixed] EDIT 2: upon re-reading your question, it made more sense.. this is the correct one.. I will leave my initial answer here if someone needs it..cuphead Quote Link to comment Share on other sites More sharing options...
Question
mawjustin
Hi Team,
May I ask if this is possible?
I have a sample here that requires user to select which choice a player will choose.
Instead of selecting from switch expression, can we randomize this without changing the switch statement?
The script will randomly choose from Case 1 to Case 4 without changing the switch statement.
Is this possible?
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Thank you.
Link to comment
Share on other sites
4 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.