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,602
Reaction score
1,933
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
525
Reaction score
230
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,492
Reaction score
408
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
525
Reaction score
230
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,602
Reaction score
1,933
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
89
Reaction score
39
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,492
Reaction score
408
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
683
Reaction score
3,319
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
7,418
Reaction score
5,057
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 Profile Posts

Highly opinionated world-building mapping
Working on an audio engine plugin for MZ! Check this out with your sound on (preferably stereo sound too). Multiple BGM and BGS channels and spatial audio (volume and panning)!
Got the menu problem sorted out, now most of the systems are in place and I can focus on the plot and the maps!
Im really Happy that the MZ Hero looks like a Boy than a Man.
What the community thinks hackers do:
"Ok, now let's just type in the /win command, and... done!"

What hackers actually do:
"Alright, so I have a high jump mod installed, now I can just jump over everyone and be sneaky until I'm the last one standing."

Forum statistics

Threads
103,147
Messages
997,637
Members
134,625
Latest member
sality
Top