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 Posts

Latest Profile Posts

Another comic with Backstage Malak...
If you want to ask him anything, you can ask on my DeviantArt page! I'm not sure if you're allowed to do it here.
Forum tip of the day ...
If you get a temporary suspension for being a jerk to people who are trying to help you, and arguing with mods and admins, don't go making new accounts. Chances are, if you p**s us off enough to suspend you for a week, we're not going to hesitate giving you a permanent ban when you give us a reason.
E_6fAErVkAQILXu.png
I'm so close to releasing my game on steam! :blush::blush::blush:
Us:
"This game is awesome! I wish there are more games like this in the future."
Also us:
"I hate how games stopped being original and start copying other successful games."
Should've done this a few days ago but...
PRI_200136808.jpg
Rest in Peace, Sir Clive Sinclair
1940 - 2021
Entrepreneur, Technologist, Father of the Modern British Computing Industry, and protagonist of Legend of ZUN.

Without you, England's games and tech industry wouldn't be where it is.

Forum statistics

Threads
115,292
Messages
1,088,692
Members
149,927
Latest member
antodi
Top