Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Switches

I just need to know two things:

How do I use switches in an IF statement?
How do I control a switch using a script?

For example, if I wanna do this:

if switch[1] is on
do cool stuff
end
 
Code:
if $game_switches[switch_id]            (On)
if $game_switches[switch_id] == false (Off)

@altorn : It is suppose to be $game, and if blah = true will just make blah true, instead of testing it as a boolean, then, since the interpretter will just evaluate blah = true as true, it will always be considered a true boolean.
 
I have this in my scene_map script:

Code:
  def update_stuff
    # Update Hud Windows
    @hud1.update
    @hud2.update
    @hud3.update
    @hud4.update
    # Update quest log
    if $game_switches[2]
      @quest_window.update
      @quest_rewards.update
    end
  end

It says this error:
Line 304, NoMethodError occured, undefined method '[]' for true:TrueClass

Line 304: if $game_switches[2]

Edit:
Basically, if switch 2 is on, the quest log and quest rewards windows are shown. I have that in the def main_draw part and it seems to work fine no errors. All this bit does is updates those windows if switch 2 is on.
 

Jared

Member

The Code is correkt, but seemingly $game_switches is not an object of Game_Switches Class, but an object of True-Class.
You can check it with
p $game_switches.class

Maybe you have written anywhere
$game_switches = true
instead of
$game_switches[1] = true

Nevertheless I wouldn`t use switches for your script. Use ruby-variables.

Code:
class Quest
attr_accessor(:need_update)

  def initialize
    @need_update = false
  end
end

Now you can write
if @need_update
@quest_window.update
@quest_rewards.update
end
You can change the variable @need_update in the object or in a higher instance with @quest_object.need_update = true



@Altorn:
a = b #means: create a reference between a and b (or by booleans and integers: a get the value of b)

a == b #means: return true if the condition is correct. Return false if the condition is false

if a then do_something end #means: do_something if a is not false or nil
unless a then do_somethin end #means: do_something if a is not true
 
I need to use switches as there are events and stuff involved too.

But anyway, I have in scene_map this:

Code:
def main_draw
    # Make sprite set
    @spriteset = Spriteset_Map.new
    # Make message window
    @message_window = Window_Message.new
    @hud1 = Window_HudEquip.new
    @hud2 = Window_HudPortrait.new
    @hud3 = Window_HudInfo.new
    @hud4 = Window_HudSkills.new
    # Transition run
    Graphics.transition
    if $game_switches[2]
      @window1 = Window_Quests.new
      @window2 = Window_QuestRewards.new
    end
end

and this:

Code:
  def update_graphics
    # Update sprite set
    @spriteset.update
    # Update message window
    @message_window.update
  end
  def update_stuff
    # Update Hud Windows
    @hud1.update
    @hud2.update
    @hud3.update
    @hud4.update
    # Update quest log
    if $game_switches[2]
      @window1.update
      @window2.update
    end
  end

The problem is the lines with @window1.update and @window2.update: undefined method "update".
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top