Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: theoneelectronic on Tue 17/09/2013 15:14:08

Title: Problem with SetBackgroundFrame()
Post by: theoneelectronic on Tue 17/09/2013 15:14:08
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:

Code (ags) Select

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.
Title: Re: Problem with SetBackgroundFrame()
Post by: Crimson Wizard on Tue 17/09/2013 15:21:23
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:

Code (ags) Select

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.
Title: Re: Problem with SetBackgroundFrame()
Post by: theoneelectronic on Tue 17/09/2013 15:41:23
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?
Title: Re: Problem with SetBackgroundFrame()
Post by: Crimson Wizard on Tue 17/09/2013 17:54:39
Add a global variable.
Somewhere at the top of GlobalScript.asc (outside any functions):
Code (ags) Select

bool IsGeneratorWorking;   // new boolean variable
export IsGeneratorWorking; // export our variable from the global script so that others could use it


Somewhere in GlobalScript.ash:
Code (ags) Select

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:
Code (ags) Select

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:
Code (ags) Select

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
Title: Re: Problem with SetBackgroundFrame()
Post by: Khris on Tue 17/09/2013 20:03:32
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.
Title: Re: Problem with SetBackgroundFrame()
Post by: theoneelectronic on Tue 17/09/2013 20:06:08
Thank you very much to both of you. Your suggestions are highly appreciated!
Cheers