Status
Not open for further replies.
Joined
Jul 17, 2017
Messages
41
Reaction score
4
First Language
Spanish
Primarily Uses
RMVXA
Hello again!
Lately I have been testing RPG Maker 2003, and there is an option in the section of switches that gave me something interesting and that I wanted to test in RPG Maker VX Ace if it was possible to do it.
the option is called: "Toggle ON / OFF"
its function is to change the parameter from True to False and biseverse without the need to check the switch with conditional branch.

I've been trying to experiment with the VX Ace code and I think the closest thing to do so to speak is this:

Ruby:
$game_switches[1] == false ? true : false

I have tried it with local switches:

Ruby:
$game_self_switches[[1, 1, 'A']] == false ?  true : false

and other ways:

Ruby:
$game_switches[1] = false ? true : false
$game_switches[1] = true : false
$game_switches[1] ? true : false

but none have worked in the program and I don't understand why.

I understand that in other scripts they use a button to change the parameter like the following:

Ruby:
value = direction == :left ? false : true
$game_switches[1] = value

I would like to know if it is possible for the switch to change its value from ON to OFF as it does in RPG Maker 2003.

(I know there are many ways to make this mechanism, but I would like to know if this is functional, for me it would be more practical)

Thanks in advance for the help and sorry for the inconvenience for this simple code ^^
 

SpunkyDuck

Ruby Programming Enthusiast
Veteran
Joined
Jun 5, 2020
Messages
97
Reaction score
411
First Language
(US) English
Primarily Uses
RMVXA
For something like this to get it working I'd plug it in directly to the Game_Interpreter class so it can be called on the Map, Battles, etc.

Ruby:
# Use the Game_Interpreter to use this effectively
class Game_Interpreter

  # Toggle Switch w/ ID (Use toggle_switch(id) inside a script command)
  def toggle_switch(switch_id)
    # Toggles the switch if it's on and vise versa
    $game_switches[switch_id] = $game_switches[switch_id] ? false : true
    # Print's Result to Console
    p $game_switches[switch_id] ? "ON" : "OFF"
  end
end
 
Joined
Jul 17, 2017
Messages
41
Reaction score
4
First Language
Spanish
Primarily Uses
RMVXA
Code:
$game_switches[1] = !$game_switches[1]
For something like this to get it working I'd plug it in directly to the Game_Interpreter class so it can be called on the Map, Battles, etc.

Ruby:
# Use the Game_Interpreter to use this effectively
class Game_Interpreter

  # Toggle Switch w/ ID (Use toggle_switch(id) inside a script command)
  def toggle_switch(switch_id)
    # Toggles the switch if it's on and vise versa
    $game_switches[switch_id] = $game_switches[switch_id] ? false : true
    # Print's Result to Console
    p $game_switches[switch_id] ? "ON" : "OFF"
  end
end


wow, I didn't think there were more ways to do it. I have tried both right now and they work perfectly. Thank you very much and sorry for the inconvenience c:
 

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
356
Reaction score
101
First Language
English
Primarily Uses
RMXP
You could replace Xane's code, the part enclosed by def toggle_switch(switch_id) end with my line of code. What my code does is to quickly invert its boolean value. It's faster than a ternary ? condition.

EDIT
Code:
class Game_Interpreter
  def toggle_switch(switch_id)
    $game_switches[switch_id] = !game_switches[switch_id]
  end
end
 
Last edited:

SpunkyDuck

Ruby Programming Enthusiast
Veteran
Joined
Jun 5, 2020
Messages
97
Reaction score
411
First Language
(US) English
Primarily Uses
RMVXA
You could replace Xane's code, the part enclosed by def toggle_switch(switch_id) end with my line of code. What my code does is to quickly invert its boolean value. It's faster than a ternary ? condition.
You questioning my code?! :kaomad3: Joking.

It's 1am where I am. Had I seen your post before hand I'd probably would have wrapped it in there and sent it that way too. To be fair though we're talking a fraction of a ms here for a switch. Unless he's calling this constantly many times over in a single thread the performance is probably a negligible difference.

@Christopher Riel Both of these work well for what you're trying to do. But @kyonides does make more sense in terms of efficiency. Where mine is more for breaking down and understanding the process yourself.
 

kyonides

Reforged is laughable
Veteran
Joined
Nov 17, 2019
Messages
356
Reaction score
101
First Language
English
Primarily Uses
RMXP
Well, there's also another advantage in using the ! operator.
If for any weird reason he assigned a non boolean value to the switch, it will be transformed into a false value and no extra code is needed.
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
3,941
Reaction score
4,853
First Language
EN, FR
Primarily Uses
RMMZ

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.

Latest Threads

Latest Profile Posts


1,Randomly generate events with planted spawner events(great flexibility for customization and auto-match player's level)
2,Randomly enter roguelike dungeon
3,Player can exit dungeon or back to town or choose to challenge BOSS at the end of each dungeon
4,Dungeon maps can be randomly generated by a third-party tool
Got locked out of the house yesterday... best friends who had the spare ghosted me. Locksmith had to literally drill thru the lock bc they couldn't pick it. Whole process took 4 hours and far too much money. And they have to come back next week bc they don't have the right cylinder, cause it's an old interlocking lock. I'm livid lol
 I’m probably going to regret this, but I need kobolds for my project and I’ve decided to sprite them myself. This is Attempt #1, front view only.

KoboldFront.gif
It's a shame that Selection Control will not be ported to MZ. I found a neat way of using it to enable ways for tanks to protect squishies. lol
Lately I have been taking a break from game and resource making so I won't get burnt out. But I am also interested in the Game Jams this site periodically holds. It'd be great if I knew when the next one, if any, is planned.

Forum statistics

Threads
115,354
Messages
1,089,223
Members
150,031
Latest member
Slashqian
Top