Turn the lights off/on in a room [SOLVED]

Started by Narulita, Fri 29/04/2011 17:11:25

Previous topic - Next topic

Narulita

So I want my character to be able to turn the lights off in a room after interacting with a light switch button that is on the wall. I created a hotspot for the button named hKnapp if that is of any importance. I'm not quite sure how to put this into the scripting. To make it clear, I want the lights to be on when the character first enters the room and off after he presses the button, not the other way around :)

Khris

It depends on how you implemented the two states of the room.

In general you need a room variable to store the room's state, i.e. you declare it at the top of the room script, outside any function:

Code: ags
bool light_is_on = true;


light_is_on is now initially true and will retain its value even after you left the room.

In hKnapp's interaction function you switch the variable and update the room

Code: ags
  light_is_on = !light_is_on;       //  ! means "not"

  // switch room background, etc. depending on light_is_on

Sephiroth

#2
If you're using 2 different background frames, with and w/o lights, you'll need to add a quick check to know which one to apply:

Code: ags


bool LightOn = false;

function hKnapp_Interact()
{
  if(LightOn)
    SetBackgroundFrame(1);
  else
    SetBackgroundFrame(2);

  LightOn = !LightOn;
}


For example.

Calin Leafshade

remember that the background images will cycle from one to the other. So it will look like its flashing on and off.

You need to continually set the background image in rep_ex every loop

Sephiroth

#4
void SetBackGroundFrame(int frame);
Locks the current room to the specified background.

I must have misunderstood, sorry.

Edit: thanks Khris, thought so but couldn't test right now.

Khris

Your way is fine, setting it to -1 will resume cycling.

Calin Leafshade

Just embarrass me in front of the whole internet why don't you.

Narulita

Uh, so i tried this:

Code: ags
 
function room_AfterFadeIn()
{
  SetBackgroundFrame(0);
}

function hKnapp_Interact()
{
  SetBackgroundFrame(1);
}


Which works fine except for that the room is switching between the two backgrounds while loading, is there any way to avoid this?

Narulita

Boy am I a slow newbie, lol. I'll look through your tips now, sorry ^^

monkey0506

AGS will, by default, already be cycling the background frames until you manually lock it with SetBackgroundFrame.

If you don't want it flashing between the background frames, then why would you be locking the frame in the room's after fade-in function? Consider using the before fade-in function instead, and paired with the code Sephiroth gave above, it should work fine.

Narulita

It works flawlessly now. Thank you all very much!

SMF spam blocked by CleanTalk