Respawn only destroyed boxes if checkpoint not activated

Hello, so I have a game where you destroy boxes in a level and you need to destroy all of them to get a reward at the end of the level, I’m trying to make it so if you destroy the boxes and die before reaching a checkpoint, they would respawn back in their location.

Hey welcome!

You need to use variables to store the location of each destroyed box, so when the player respawns you recreate the boxes at these locations.

In my example I used mouse events to destroy and create the boxes, so you’ll need to adapt it to your events.

1st declare BoxesArray (as an array)

After reaching a checkpoint, clear children from variable BoxesArray

Hi! Sorry for the late response, I got some questions as I have never dealt with arrays…
1 - How to do/get the “Declare” sub events?
2 - Would this work for different box objects? They do different things when broken, but they still get broken the same way
3 - I actually forgot to mention a point about the counter, I see you have an add counter but I would also like to know how to subtract it with the amount of boxes that got respawned
Thanks for your time!
4- Would it be better to have these variables as global or scene? The boxes are in every level

No worries! Variables (all types) are essential in game dev and you’ll be using them a lot. So if you’re not very familiar with them, I recommend checking out this tutorial.

Right-click (on an event) > add > local variable.
You’ll see it in the tutorial video.

Yes. Add all the boxes to an object group (let’s say the group’s name is Boxes), so you’ll need to use the group Boxes instead of the object Box in all the actions/conditions.

Also you’ll need to store the name of the object in a string variable along with the X & Y positions. Use Boxes.ObjectName() to get the name, and use the “create object from it’s name” action to create the boxes:

I don’t understand what you mean by this.

A global variable will not reset when changing scenes. So you either have to declare the BoxesArray as a scene array in every scene, or you can use a global variable but make sure to clear it at the start of each scene.

You can add these events to an external event and add a link in all the scenes, instead of copying the events to each scene. This way any change in the events will be automatically updated in all the scenes.

Example for point 3: I’m in a level, the counter starts at 0/Total boxes, I break let’s say 4 boxes and so the counter now shows 4/Total boxes, however I die before the checkpoint that saves them, when I respawn and the 4 boxes respawn, I want the counter to subtract the exact same 4 boxes that respawned

The counter I used is to loop through the children of the array. But in your case you’ll need a different variable to be used to count the number of destroyed boxes.

Declare a scene number variable (call it something like DestroyedBoxes). And along with “add 1 to Counter” add an action to subtract 1 from DestroyedBoxes in the same event.

Understood, may I have a full screenshot of all the events?

Thanks, I’ll do a quick test with it

I did a quick test with this and the result is perfectly what I want, will try it with my own boxes and such, although a question, I put text to display “Counter” and “DestroyedBoxes”, is it normal that “Counter” is not changing at all? it’s always at 0, while “DestroyedBoxes” is the one that changes according to destroying and respawning and again it works as I want it to, so is “Counter” necessary to have and it just has a hidden role? Or could I keep it in the events and use just “DestroyedBoxes” for the HUD

I used the Counter as a “local variable”, which means that it will only be declared and used in this particular event, and will be discarded once the event is done. So you won’t see it’s value changing. I used it to loop through the children of the array.

I’ll try to explain briefly how I used the counter in my example. But it’s best to learn the different types of variables and understand how each type work.

Let’s say the player destroyed 3 boxes. The array would be like this:

As you can see, each array child has an index (number on the left), starting from 0,1,2 and the total number of the children are 3.

The Counter is used to loop through the children of the array.

So I used a repeat event (repeating with the number of children, in this case 3) by using the expression VariableChildCount(BoxesArray).

At the first repeat, Counter = 0.
So BoxesArray[Counter].PosX would be the same as BoxesArray[0].PosX. This would get the the PosX from the 1st child of the array (index = 0).

When 1 is added to the Counter, the next repeat would get the 2nd child (index = 1)

Etc…

On the other hand, 1 is subtracted from DestroyedBoxes on each repeat.
DestroyedBoxes is a scene variable so it won’t be discarded until the scene is changed or restarted.

.

Hope this explanation helps you understand these events a bit more. Creating logics like these can be complicated if you don’t know how variables work, so as I said it’s best to take some time and learn more about variables.

Ohhhh, now that makes sense, I do know about the variables except Array, I never went through a situation where I needed it, well until now xD
Last thing for real, I’m sorry I forgot to mention it but would this work with multiple checkpoints? My checkpoint has a “off” animation when it’s not in collision with player, and “on” when player is in collision, if checkpoint 1 (if it’s by object or instance ID) is on, would that mean the next crates would stop respawning all together even without activating their respective checkpoints? Or does it only save the one time the checkpoint is activated and you would need to activate another checkpoint to save the destroyed objects?

Yes this will work with multiple checkpoints. Any box that is destroyed before reaching a checkpoint will be added to the array (recreated if player dies). And once a checkpoint is reached, the array will be cleared (look at the last event in the screenshot).

Thank you so much, I tested it with my own level and objects and it’s working perfectly!

1 Like

Okay so there’s a little setback, particularly with “Reached Checkpoint”, it works as intended when it’s a “X key is pressed” but when I try to make it a “Player is in collision with checkpoint” it either doesn’t save at all or always saves, I tried other conditions like “Checked” of checkpoint is true or the animation of Checkpoint is off/on, trigger once or no trigger once, opacity etc, nothing works

Can you share a screenshot of the events so I can take a look?

Here you go


Destroying one of the boxes

I don’t see anything wrong with the reached checkpoint event other than it needs a trigger once. But I can’t really tell what might be the issue. You’ll need to do some testing to isolate the problem.

Though, I think that the events in the 2nd screenshot need some cleanup. I don’t see why you need a for each object event since the collision detection condition is enough to pick the correct box. Also since 90% of both events are the same, you can merge them into 1 event and use conditions for the small differences. If you have multiple boxes then use a group. You can do the animations separately from the saving part if you can’t merge everything in 1 event.

I use “for each object” because boxes are close to each other most of the time and if I destroy like three at once it only counts as one, and yeah I do know it needs optimization, I started the project recently so I’m just making sure stuff works first, I don’t think there is anything wrong with the second event, because again it works as intended when I use “key pressed” as a condition for reaching the checkpoint, so the root cause must be the condition for when the checkpoint is reached, I guess all I could do is try other conditions

I figured it out! Turns out the issue was the checkpoint immediately changes to “On” when in collision, I added an action for it to wait 0.5 seconds before it changes to “On” and now it works as intended, it’s all done now, thanks for your patience with me through all this!