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.

[SOLVED]Checking if a set of switches are ON

I tried to use a cond. branch to check if a certain group of switches are on but got an error. I used $game_switches[10...14].id = true. but got an error msg. any ideas?so
 
darkcieran":307wi7pf said:
I tried to use a cond. branch to check if a certain group of switches are on but got an error. I used $game_switches[10...14].id = true. but got an error msg. any ideas?

Would you care to elaborate? What was the error message? What were you trying to do if the switches were on, and what would the event have done if they were off?
 
this is the error i got.
Untitled2.png
the event would display a message if any of those switch were on.
 

Star

Sponsor

Couldn't you just do ?

If $game_switches[10].id == true or $game_switches[11].id == true or $game_switches[12].id == true or $game_switches[13].id == true or $game_switches[14].id == true

Do something
end
 
Yeah, Star that would work. Sadly it's too long for the cond. branch script.

Try this code in a script command. It'll turn switch num x ON if one of the switches is ON.
Code:
switch_ids = (10..14).to_a

switch_ids.each { |id| 

              $game_switches[x] = true if 

              $game_switches[id] == true}
Just change x to whatever number you want, and use that switch in a cod. branch.
 
Cool, I like one-liners :p
It'll require a fix to work, as $game_switches isn't a real array. However, editing the class would make life easier so.. I wrote a small fix.

My fix :
Code:
 

class Game_Switches

 

  def [](switch_id)

    if switch_id.is_a?(Range) 

      return ( legal_range(switch_id) ? @data[switch_id] : [] )

    end

    if switch_id <= 5000 and @data[switch_id] != nil

      return @data[switch_id]

    else

      return false

    end

  end

 

  def legal_range(r)

    return ( (r.first > 0) and (r.last <= 5000) )

  end

 

end
 
Oh yeh, game_switches is a class, the @data property is an array.

I was being lazy, and typed the statement from memory rather than actually testing it :scruff:

I suppose we could just attr_accessor the @data property too. then use

$game_switches.data[11-14].include?
 
I think its...

$game_switches.data[11..14].include? true

If it's not, why wouldn't it work?

Anyway, I have my own solution.

[rgss]class Array
  def which_is?(bool=true)
    return 'No boolean argument provided' unless bool or !bool or bool.nil?
    results = []
    self.each {|v| results << self.index(v) if v == bool}
    return results.empty? ? nil : results
  end
end
 
class Game_Switches
  def [](switch_id)
    if switch_id.is_a? Integer and switch_id <= 5000 and @data[switch_id] != nil
      return @data[switch_id]
    elsif switch_id.is_a? Range and switch_id.first.between?(1, 5000) and
      switch_id.last.between?(1, 5000)
      ary = []
      switch_id.each {|id| ary << (@data[id].nil? ? false : @data[id]) }
      return ary
    end
    return false
  end
end
[/rgss]

To test it, use...

$game_switches[2] = true
p $game_switches[1..5]
p $game_switches[1..5].include? true
p $game_switches[1..5].which_is? true

You should see 3 popup windows with the results to those commands.
 

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