Can you do switch case using MV event commands? <RESOLVED>

Status
Not open for further replies.

marbeltoast

Your future fish emporer
Veteran
Joined
Nov 11, 2018
Messages
80
Reaction score
28
First Language
English
Primarily Uses
RMMV
I do not know much or really any javascript, and as such do most of my code work with event commands. For the sake of efficient code, it would be so, so useful if I could use switch case.

However, I see no options for doing so using MV's event commands. The closest thing I could think of is some kind of label-based list, as labels are, to my limited knowledge, the only way to jump forwards and backwards in the code. I'm not sure how, or even if, this could actually be implemented though.

Is there a way to write a switch case script in a common event, or must I choose between sub-optimal code, or a java for dummies book?

Also sorry if this is the wrong place to post this. It's kind of a multifaceted problem so I wasn't sure whether it was more of a script issue or an engine issue.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,311
Reaction score
1,479
First Language
English
Primarily Uses
RMMV
You can use switch cases in a "script" command, though unless you assign any values therein to globally-scoped variables or an element of $gameVariables, they'll only be scoped to that one script box.
 

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
453
Reaction score
193
First Language
English
Primarily Uses
RMMV
You might be interested in my plugin that turns a Show Choices command into a switch statement.
 

marbeltoast

Your future fish emporer
Veteran
Joined
Nov 11, 2018
Messages
80
Reaction score
28
First Language
English
Primarily Uses
RMMV
You might be interested in my plugin that turns a Show Choices command into a switch statement.
I've downloaded this and will be testing later. Looks promising!
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,441
Reaction score
385
First Language
German
Primarily Uses
RMMV
Can you explain what a "switch case" is and does? I dont understand, thats why i cant tell you how to do that in rpg maker.
 

Solar_Flare

Veteran
Veteran
Joined
Jun 6, 2020
Messages
453
Reaction score
193
First Language
English
Primarily Uses
RMMV
It's a type of branching structure where you choose a path to follow based on the contents of a variable, like below (you can copy-paste that code, press F12 in your browser or an RMMV playtest, paste it in, and try it).

JavaScript:
var myNumber = prompt('Pick a number:')
switch(Number(myNumber)) {
case 1: alert('You picked 1? How boring!'); break;
case 3: alert('3 is a fine number!'); break;
default: alert("I don't really care for your choice!"); break;
}
While I'm here I want to note that the association of switch statements with labels is rather unfortunate. It would make more sense in my opinion for each case to be a separate block... which is how SFG_SwitchCommand ends up working, I guess.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,311
Reaction score
1,479
First Language
English
Primarily Uses
RMMV
Can you explain what a "switch case" is and does? I dont understand, thats why i cant tell you how to do that in rpg maker.
Put simply, a switch case is a shorter if statement that checks a variable for multiple values. Let's say you wanted to show a different message depending on the ID of an item the player selected:

1594388469462.png
 

ATT_Turan

Veteran
Veteran
Joined
Jul 2, 2014
Messages
67
Reaction score
25
First Language
English
Can you explain what a "switch case" is and does? I dont understand, thats why i cant tell you how to do that in rpg maker.
It's basically a more efficient method than using a ton of if/else statements.

Instead of doing:
Code:
if (your_variable==1)
    thing;
else if (your_variable==2)
    other thing;
else if (your_variable==3)
    yet more things;
you'd put
Code:
switch (your_variable)
    case 1:
        thing;
    case 2:
        other thing;
    case 3:
    case 4:
        easily make some values do the same stuff without having to retype code;
 

Bex

Veteran
Veteran
Joined
Aug 2, 2013
Messages
1,441
Reaction score
385
First Language
German
Primarily Uses
RMMV
A Switch can have only 2 Values, 0 or 1 , false or true, thats why i struggle to understand
where case 3 and 4 come from? They should not be possible.

I guess you want a Variable Pointer to a Switch, i mean a variable value that determines the switch id?
There is this Scrip Call List, that helped alot with these things.
Put this in Eventcommand Conditional Branch in the Scriptline, the Value of Variable1 will determine the SwitchId, i did not test if the Game crashes if variable 1 is equal to 0, than we would need to adjust the Codeline.
Code:
$gameSwitches.value($gameVariables.value(1)) == true
Code:
$gameSwitches.value($gameVariables.value(1)) == false
Edit: This also works with variables. Value of Variable1 determines the VariableID which is used in this if statement.
Code:
$gameVariables.value($gameVariables.value(1)) == 12
This is interesting, please correct me if i miss understood. Iam not a Scripter.
 
Last edited:

standardplayer

Keeper of Kitties
Veteran
Joined
Apr 6, 2016
Messages
654
Reaction score
3,010
First Language
English
Primarily Uses
N/A
@Bex they mean JavaScript Switch statement, not RPG Maker. The conditional structure with the Switch/Case format allows you to check one variable or property, and then instead of writing an if statement for each value you want to check, you check each one within the switch statement.
You can handle each 'case' inside of the switch statement.

It's actually got kind of limited uses, but it's nice for the programmer when it is viable.

It's called a Switch statement in JavaScript, but similar structures are in other non C like languages. For example, VB has it, but it's called a Select statement. Same principal.
 

marbeltoast

Your future fish emporer
Veteran
Joined
Nov 11, 2018
Messages
80
Reaction score
28
First Language
English
Primarily Uses
RMMV
I have tested this plugin, following the instructions as best I could, and found an error I do not understand. Here is my code and my error:

Error Report.png

The error reads: TypeError: Utils.isJson is not a function.

This is probably something fairly easy to fix but I'm not entirely sure how.

EDIT: Nevermind, I figured it out. Silly me, not installing the utilities plugin first! Well, that solves my problems. I'm going to call for this thread to be closed now. Thanks, buds!
 
Last edited:

slimmmeiske2

Little Red Riding Hood
Global Mod
Joined
Sep 6, 2012
Messages
6,973
Reaction score
4,728
First Language
Dutch
Primarily Uses
RMXP

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Don't mind me, just installing a fog machine. :LZSwink:


I can't stop looping this BGM while at work since I began to play PSO2. It just too good for mood booster.
How to post a picture there in the status feed?
So I was at the grocery store trying to reach for some small garbage bags on the top shelf way in the back. Instead of going all the way downstairs to find someone to help me I grabbed a broom and pulled it down. Putting the broom back I said, "Thank you for your service".
S.O.S.--10:16pm......two toddlers still awake....send help...

Forum statistics

Threads
100,423
Messages
975,701
Members
132,080
Latest member
Garycomehome
Top