Problem with SetBackgroundFrame()

Started by theoneelectronic, Tue 17/09/2013 15:14:08

Previous topic - Next topic

theoneelectronic

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

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.

Crimson Wizard

#1
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

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.

theoneelectronic

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?

Crimson Wizard

Add a global variable.
Somewhere at the top of GlobalScript.asc (outside any functions):
Code: ags

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

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

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

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

Khris

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.

theoneelectronic

Thank you very much to both of you. Your suggestions are highly appreciated!
Cheers

SMF spam blocked by CleanTalk