[SOLVED] How to call variables from another rooms

Started by nightmarer, Wed 18/03/2020 19:13:10

Previous topic - Next topic

nightmarer

Hello.

I have my room1 with an invisible object(oFlower), and I want to make it visible once the character arrives to room4.
oFlower.Visible = true;
It is possible to change an object property from another room?
I have been reviewing the manual but I don't know if objects are part of a higher object room.
I don't know if I am explaining right.
How I can refer to variables from outside of one room? Is that possible?

Regards.

Cassiebsg

You have at least two options.

The one you asked about, is going to Global variables (the round icon blue globe in the project tree) and create a bool variable and call it "isFlowerVisible" (I like to make my bool variables a question, that way I always know what the answer is). Then you can just use the variable when ever you like.

The second way, is before fade in, to check if the player has visited room 4, and if so turn the flower on.

Useing one or the other will work, but depends if you also have to know in other rooms/scripts if the flower is on or off. Either way, you can always check if the player had visited the room or not.

Hope it helps. if not ask again.  (nod)
There are those who believe that life here began out there...

JackPutter

#2
I am just a beginner myself so someone else may have a better answer, but you could use Global Variables to achieve what you're looking for. Global Variables can be called from any room.

In the Global Variables section of the project tree, right click and add a new Global Variable. A new window will pop up. Add a name for your variable, for example let's name it "VisitedRoom4" for now. Next, from the drop-down menu pick the "bool" option. Finally make the default value "false" and click ok.

Now, in whatever script you use to change the room to Room4, add the line:

Code: ags
 VisitedRoom4 = true; 


This will set the Global Variable to true, which you can use to make your flower object become invisible.

Open Room1 in your editor. First off, open the oFlower object. In the bottom right, make sure the Visible property is set to "False" so the object will not show up at first. Next, select Room1 and go to the room events. (This is the little lightening symbol in the bottom right.) Click on the option "Enters room before fade-in" and then click on the button with three dots in it. This should bring you to the script for that room. Then enter in the following code:

Code: ags
 function room_Load()
{

if (VisitedRoom4 == true)
{
oFlower.Visible = true
}

}


That means that when you go to Room1, the game will check the Global Variable value first. If the player has not visited Room4 yet, the Global Variable value will be false, and the object will remain invisible. If the player has visited Room4, the Global Variable will be true, and your Flower should appear!

As I said, I am only a beginner, so someone else may have a more simple way to achieve the same thing. That's just the method I would use. I hope that helps you!

EDIT: I just saw Cassiebsg's reply moments after I sent my own, and learned some things myself! Good tip about making variables a question, and about doing checks before room load on where the player has been!

TheManInBoots

So just to clarify the suggestions above:

In the room load function of room1 you write

Code: ags

function room_Load() 
{
if(HasPlayerBeenInRoom(4)==true)
{oFlower.Visible=true;}
}


There is a way to address objects from Global Script for example by writing object[0].
But that would not help you here, and you have to be careful because if you run the function in Global Script with object[0] while you are in a room where there is no object with ID 0 the game crashes.

nightmarer

Thank you very much for all the replays!

I'm going to try it, if I don't say anything it will be because it works.

:)

SMF spam blocked by CleanTalk