Hello,
I'm trying to make the background change when the player has an object in the inventory but without success.
The code is as follows:
function room_AfterFadeIn()
{
if (player.ActiveInventory == iSpanner)
{
SetBackgroundFrame(1);
}
else
{
SetBackgroundFrame(0);
}
}
The idea is:
Room 1 has a streetlight shut off.
Player walks in Room 2, remove a spanner from a generator
Generator starts running
Player goes back to Room 1 and the streetlight is on
To do this I thought to make the program check the presence of the spanner inside the inventory.
Obviously I'm doing something the wrong way.
Thank you in advance.
Quote from: theoneelectronic on Tue 17/09/2013 15:14:08
I'm trying to make the background change when the player has an object in the inventory but without success.
player.ActiveInventory checks
selected item.
To check
existing items, use player.HasInventory:
function room_AfterFadeIn()
{
if (player.HasInventory(iSpanner))
{
SetBackgroundFrame(1);
}
else
{
SetBackgroundFrame(0);
}
}
But in your case I'd suggest to use global variable instead to keep "generator state". Presence of inventory item is generally irrelevant, from the game design perspective. For example, later in game player may loose the spanner or give it away - this will turn the lights off again, although generator is still working.
Thank you for your help Crimson Wizard. It was indeed something trivial yet deeply wrong in concept I was doing.
Thank you also for your suggestion about the design. I was also thinking about the same problem but I still have to look how to do that.
Would you mind proposing me a method?
Add a global variable.
Somewhere at the top of GlobalScript.asc (outside any functions):
bool IsGeneratorWorking; // new boolean variable
export IsGeneratorWorking; // export our variable from the global script so that others could use it
Somewhere in GlobalScript.ash:
import bool IsGeneratorWorking; // tell every other script there's such variable
Now, in the Room 2, just where player removes spanner from generator and new item is added:
player.AddInventory(iSpanner); // you might have something like this there now
IsGeneratorWorking = true; // <--- add this line, this will set variable to 'true'
In the Room 1:
function room_AfterFadeIn()
{
if (IsGeneratorWorking) // <-- check variable's value
{
SetBackgroundFrame(1);
}
else
{
SetBackgroundFrame(0);
}
}
Also, some general information on variables: http://www.adventuregamestudio.co.uk/wiki/Scripting_tutorial_part_1#Variables
And just for the sake of completeness: you can add a global bool in the Global variables pane. That way you can skip the first two code snippets.
Thank you very much to both of you. Your suggestions are highly appreciated!
Cheers