Jump to content
  • 0

Automatically select switch case


mawjustin

Question


  • Group:  Members
  • Topic Count:  48
  • Topics Per Day:  0.01
  • Content Count:  124
  • Reputation:   6
  • Joined:  09/26/14
  • Last Seen:  

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.

image.png.927f2266baa392c02ccf8548aa5d45d6.png

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

  • 0

  • Group:  Forum Moderator
  • Topic Count:  93
  • Topics Per Day:  0.02
  • Content Count:  10015
  • Reputation:   2357
  • Joined:  10/28/11
  • Last Seen:  

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)) {
   ...
   ...
}

 

Link to comment
Share on other sites

  • 1

  • Group:  Members
  • Topic Count:  92
  • Topics Per Day:  0.02
  • Content Count:  354
  • Reputation:   22
  • Joined:  11/17/11
  • Last Seen:  

have you tried switch(select(rand(1,x))?

  • Upvote 1
Link to comment
Share on other sites

  • 1

  • Group:  Members
  • Topic Count:  79
  • Topics Per Day:  0.03
  • Content Count:  439
  • Reputation:   172
  • Joined:  12/12/17
  • Last Seen:  

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 /swt

 

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 by pajodex
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  0
  • Topics Per Day:  0
  • Content Count:  1
  • Reputation:   0
  • Joined:  04/20/23
  • Last Seen:  

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 /swt

 

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

 

Link to comment
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.   Paste as plain text instead

  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.

×
×
  • Create New...