Save-Point
Activate Switch with a button - Printable Version

+- Save-Point (https://www.save-point.org)
+-- Forum: Games Development (https://www.save-point.org/forum-4.html)
+--- Forum: Code Support (https://www.save-point.org/forum-20.html)
+--- Thread: Activate Switch with a button (/thread-4688.html)



Activate Switch with a button - Iqus - 06-18-2013

Hi! I'm using a mini-map that it's triggered by a switch, so I'm trying to turn the switch on and off when pressing a button, however, I don't know much about Ruby and I'm probably missing something (maybe some kind of break to prevent the conditional from looping?)
The code I wrote is as follows:


Code:
if Input.trigger?(Input::L)
if $game_switches[2] = false    
$game_switches[2] = true    
end
if $game_switches[2] = true  
$game_switches[2] = false
end
end


I would appreciate if someone has the time to tell me what I'm doing wrong or missing! Thanks before hand =)


RE: Activate Switch with a button - MechanicalPen - 06-18-2013

Try:
Code:
if Input.trigger?(Input::L) && $game_switches[2] = false
  $game_switches[2] = true
end

if Input.trigger?(Input::L) && $game_switches[2] = true
  $game_switches[2] = false
end



RE: Activate Switch with a button - Iqus - 06-19-2013

Hmmm I think that should work but somehow the switch is not being activated (I checked it with F9...)


RE: Activate Switch with a button - MechanicalPen - 06-19-2013

oh whoops! (this is why you don't try to script after working for 8 hours) Try this:

Code:
if Input.trigger?(Input::L) && $game_switches[2] = false
  $game_switches[2] = true
  p "switch on!"
elsif Input.trigger?(Input::L) && $game_switches[2] = true
  $game_switches[2] = false
  p "switch off!"
end

remove the print statements once you know it's working. What is probably happening is the first 'if' is satisfied so the switch is set to true, and then since the switch is true the second if statement is run, setting the switch back to false.

That is, if you put your code somewhere where it can run every frame. You did that, right?


RE: Activate Switch with a button - Iqus - 06-19-2013

Hmmmm when I press the button it shows 'switch off' every single time.


RE: Activate Switch with a button - MechanicalPen - 06-22-2013

I HATE toggles, I always do them wrong. My last advice is to switch the order of the statements, otherwise I'll have to leave you in the hands of someone else.


RE: Activate Switch with a button - MetalRenard - 06-22-2013

You can do this REALLY easily with a single event... just "wait for button" then "condition > if on, turn off, if off, turn on" !


RE: Activate Switch with a button - DerVVulfman - 06-22-2013

You need to first check if the button is pressed (regardless of the switch).
THEN... you toggle based on the current switch condition:
Code:
if Input.trigger?(Input::L)
  if $game_switches[2] = false
    p "on"
    $game_switches[2] = true
  else
    p "off"
    $game_switches[2] = false
  end
end
And as MechanicalPen stated, remove the 'p'/print statement lines when you're satified.