Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Narulita on Fri 29/04/2011 17:11:25

Title: Turn the lights off/on in a room [SOLVED]
Post by: Narulita on Fri 29/04/2011 17:11:25
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 :)
Title: Re: Turn the lights off/on in a room
Post by: Khris on Fri 29/04/2011 17:25:28
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:

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

  light_is_on = !light_is_on;       //  ! means "not"

  // switch room background, etc. depending on light_is_on
Title: Re: Turn the lights off/on in a room
Post by: Sephiroth on Fri 29/04/2011 17:35:21
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:



bool LightOn = false;

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

  LightOn = !LightOn;
}


For example.
Title: Re: Turn the lights off/on in a room
Post by: Calin Leafshade on Fri 29/04/2011 17:42:19
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
Title: Re: Turn the lights off/on in a room
Post by: Sephiroth on Fri 29/04/2011 17:44:46
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.
Title: Re: Turn the lights off/on in a room
Post by: Khris on Fri 29/04/2011 18:07:32
Your way is fine, setting it to -1 will resume cycling.
Title: Re: Turn the lights off/on in a room
Post by: Calin Leafshade on Fri 29/04/2011 18:11:11
Just embarrass me in front of the whole internet why don't you.
Title: Re: Turn the lights off/on in a room
Post by: Narulita on Fri 29/04/2011 18:13:58
Uh, so i tried this:


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?
Title: Re: Turn the lights off/on in a room
Post by: Narulita on Fri 29/04/2011 18:16:14
Boy am I a slow newbie, lol. I'll look through your tips now, sorry ^^
Title: Re: Turn the lights off/on in a room
Post by: monkey0506 on Sat 30/04/2011 00:00:59
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.
Title: Re: Turn the lights off/on in a room
Post by: Narulita on Sat 30/04/2011 17:37:02
It works flawlessly now. Thank you all very much!