This question is a little smarter than the last one, I didn't find the answer in the FAQ.
I used the AGS help as a reference.
I can't quite figure out how to replace a background image. I have a lamp
in my room and when the character uses it I want to change the backgound image
to a one that has lightning effects which I added in Photoshop.
I created a new room and in the properties of the lamp's hotspot in the
first room I set up that when the player uses the lamp the character is
placed in the new room with the lightning effects. This is bad as I found
afterwards because I have to create duplicate objects and keep track of
them in both rooms which I don't know how to do.
I read in the scripting help file that SetBackgroundFrame can be used to
lock a certain image in the background animation. It also said that the lock
will be reset when the player exits the room. i don't want it to be reset, I want
the image to stay untill the player turns off the lamp and then the old
background is restored.
Should I make a variable that keeps track of the state of the lamp(on or off)
and changes the background image accordingly.
Something like this:
if light==1
SetBackgroundFrame(1)
else
SetBackgroundFrame(2)
But, then the frame lock would be released if player exits the room.
How can this be done?
Yes, you need a variable to keep track if the player left the lamp turned on or off
when he left the room.
You add the variable
int LightOn = 0;
at the beginning of your global script file. (Change the 0 to a 1 if you want the game
to start with the lamp turned on...)
Then you go to the lamps interaction (If player interacts with lamp) function and there you add:
if (LightOn == 1)
LightOn = 0;
else
LightOn = 1;
And of course then when the player reenters the room, you write in the room enter function:
if (LightOn == 1)
SetBackgroundFrame (NUMBEROFBACKGROUNDFRAMEWITHLIGHTONSPRITE);
else
SetBackgroundFrame (NUMBEROFBACKGROUNDFRAMEWITHLIGHTOFFSPRITE);
Of course you replace the placeholders with the actual sprite number.
There you go. End of little tutorial... :)
Quoteat the beginning of your global script file
To reduce clutter in the global script, you can also put the variable in the room script.
Its value is retained even when you change rooms.
Well, I got it partially working. In the rooms interaction under when player first enters screen
to run a script with just SetBackgroundFrame(0);
Then in the lamps' Interaction I entered a new script
int LightOn = 0;
if (LightOn == 1)
LightOn = 0;
else
LightOn = 1;
if (LightOn == 1)
SetBackgroundFrame (1);
else
SetBackgroundFrame (0);
So, now the character can turn on the lamp but when I try to use it again it stays lit.
How do I make the lamp go on and off?
Also why do use an if statement to reverse the value of LightOn?
if (LightOn == 1)
LightOn = 0;
else
LightOn = 1;
If at the beginning of the script LightOn = 0 and then the if statement converts it
to 1, doesn't than LightOn remain 1 forever?
What am I doing wrong this time?
You have to put the scripts in seperate events:
Declare the variable at the very top of the room script ("{}" button), outside of any functions:
int LightOn=0; // 0 -> light initially turned off
Into the room's "Player enter screen (before fadein)" function:
if (LightOn == 1) // if light is turned on
SetBackgroundFrame(1); // display lit room background
else // if light is turned off
SetBackgroundFrame(0); // display unlit room background
In the lamp object's interaction:
if (LightOn == 1) { // if light is turned on
LightOn = 0; // turn light off
SetBackgroundFrame(0); // display unlit room background
}
else { // if light is turned off
LightOn = 1; // turn light on
SetBackgroundFrame(1); // display lit room background
}
Open the room interaction script and move the line "int LightOn = 0;" from inside the lamp interaction function to the top of the script.
This will make it
- "global": available to all room and object functions
- "static": keeps its value even when you leave the room
And so you should now be able to switch off the light again
[EDIT: As strazer has just said, but with some explanation]
I am much obliged, I will try this as soon as I come back home from work.
It's working. yay :D