Some help writing an expression to set a global variable to value of an object variable?

I’m working on making chests in my game. I want the chest to only be able to be opened once, meaning once the player opens it, it is opened forever and its contents never respawn. I plan on doing this using global variables to track what chests have been opened, and if that global boolean variable is set to true, the chest will remain open for good.

What I’ve done so far is set up two objects: the chest object and a chest hitbox object. The chest hitbox object is created at the location of every chest and hidden at all times (no condition). I have it set up so that all the chest and the chest hitbox share the same variables (ChestID, Open) so that the hitbox will “inherit” the variables from the chest once the chest is created, then the hitbox created on top of it (with a collision check). I’m not sure if this is necessary or not, but I’ll leave that to you guys to decide.

What I want to make happen is when the player opens a chest, it will have a unique ID (ChestID object variable). I then want to take that ChestID variable and set a global variable to true based on the ChestID, so that chest will remain open forever. I know I could do this with a lot of code (one event for every unique chest in the game), but I know this can probably be accomplished easily with an expression.

Here are the appropriate screenshots of what I have so far:

This shot shows the global variable structure I’ve set up to try to make this happen.

Here are the object variables for the Chest object.

Here is the code I’ve made so far to have the ChestHitbox object “inherit” its object variables from the chest object.

In summary, for example: A chest is created with the ChestID “F13”. The ChestHitbox object in collision with that chest then also gets the ChestID variable of “F13”. Then, when the player opens chest F13, I want to be able to use an expression to have it automatically set the global variable Chests.Overworld.ChestID.F13.Open to true so that the chest stays open forever, without having to write an individual event for every chest in the game. How can I pretty much take the ChestID variable of the Chest object, and insert that string into an expression which will target the global variable for the ChestID of the same string?

Thank you in advance. You folks have always been very helpful!

You can use the boolean toggle event on: Chests.Overworld.ChestID[chests.VariableString(ChestID)].Open to toggle if its true or not, I’m pretty sure you don’t need the Open variable in that case.

With this expression you’ll need a global object group called chests.

I’m planning on using the Open object variable to control the animation of the object (chest open or chest closed), but you’re right…why would I need that if I have the global variable structure in the expression? Good point.

I’m about to sit down and get back to work on this, so I’ll check back with you if I run into any problems. Thank you for your input!

Ok, so I’m having a bit of a problem. I’ve created the global object group ‘chests’ and wrote the expression which you have provided. I put three chests on the screen, each with a different ‘ChestID’ instance variable. One is F13, one is F14, and one if F2. Here is the current code screenshot (I apologize for the messiness of it since I’ve been playing around with it quite a bit):

When I go and open the chest with ChestID F13, it changes the animation of all three chests to ‘NormalOpen’. When I try to open ChestID F2 first, none of the chests animations change. Same with ChestID F14.

The way I understand it is that the expression should be targeting the ChestID string variable of the Chest object (through the chests global object group). It should then set the appropriate global variable with that ChestID to true. I guess now I need to figure out what conditions/actions I need to make this work the way I intend.

Another question while I’m thinking of it: Do I need to pre-create all the global variables in the global variable structure? Or should I just leave everything blank after the ChestID structure and let the engine create the global variables on the fly as they’re triggered in the code? Thanks again, I truly appreciate you helping me out here as I try to learn!

I assume chests is an object group (maybe consider renaming object groups and variables so it’s easier to differentiate between them)

Your issue is with the second [non-disabled] event. Which chests is it referring to? You need use a For each chests object event (and maybe include a condition that Link is not in collision with it), to iterate over each chest and setting the NormalOpen animation if it’s flagged as open in the global variable.

Mr. Men,

Thank you so much for your input once again. After changing the event you referenced to a For Each event for the object group ‘chests’, it appears to be working as I intended.

You are absolutely right about my naming of variables. I’m beginning to have trouble differentiating between them, and I need to do better with that. I since decided to remove the ChestHitbox object altogether, since it was only causing confusion by sharing all the variable names with the Chest object. I named the object group ‘chests’ at the suggestion of weadsy1 above, just to quickly see if I could get it working. So I’ll change that to something different and update the name in the expression.

In case anyone else ends up with a similar question and is reading through this sometime later, I found that I needed to create another boolean variable ‘SpawnLoot’ to get the Chest objects loot to spawn once the chest was opened. I then gave each Chest object an instance variable ‘LootID’, which is a number variable (it wasn’t working with a string data type for reasons I do not understand). So that’s how I got the chest’s loot to spawn once it was opened. Here is the code I have working right now:

Thanks to both of you who helped with this! Hopefully one day I won’t need to post these kinds of questions and can be someone who is helping to answer others’ questions!

1 Like