Basic Switch Toggle ON/OFF? [VXA]

Status
Not open for further replies.
Joined
Jul 17, 2017
Messages
41
Reaction score
3
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 ^^
 

Xane

Veteran
Veteran
Joined
Jun 5, 2020
Messages
72
Reaction score
374
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
3
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
287
Reaction score
71
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:

Xane

Veteran
Veteran
Joined
Jun 5, 2020
Messages
72
Reaction score
374
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
287
Reaction score
71
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,729
Reaction score
4,682
First Language
English
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.

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

Latest Threads

Latest Posts

Latest Profile Posts

People3_5 and People3_8 added!

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.

Forum statistics

Threads
105,868
Messages
1,017,090
Members
137,586
Latest member
Usagiis
Top