Put an instance of an object around another instance of the same object?

I feel like this is something I should understand, but I’m not getting the result I want. Here is what I’m trying to do:

I have 4 instances of an object on the scene (RedMoldormPart1). If I give each instance a unique ID object variable (1, 2, 3, 4), I then want to be able to put each instance around the instance of the object with the ID variable that is 1 higher than its ID variable to make a snake-like enemy with individual objects as its body parts, with each body part following the one in front of it. I have this working as I want it when I use 4 different objects (and don’t use an ID variable), but I was wondering if it was possible to do this using different instances of the same object so I only have to create one object (or two, if I use a head or tail as a different object) rather than 4 or 5. Here is the code I have working when I use 4 different objects:

So if I’m using different instances of the RedMoldormPart1 object and give each a unique ID variable, how would I tell it to do what we see in the code above using the “Put around object” function?

I hope I made the question clear enough to understand what I want to do. Thanks in advance for any help you can give!

I’m not sure how you’re calculating the angles, so I used a variable for angle and just added 10 to it each time. You can’t reference 2 instances at the same time. You could store the x,y and use them. I used dragging for testing. I hope this at least points you in the right direction.

This uses a head and tail and an infinite number of body objects.

If this is a snake, there are questions about snakes that might be more useful.

Thanks for your reply and information!

Yes, it will be a snake-like enemy. Something I should have mentioned is that I want to be able to detect collision with each body part of the snake enemy as well. So, when the player’s sword is in collision with a certain body part of the snake, that body part (and ONLY that body part) will blink to indicate that it is damaged. I’m then linking each body part to the head of the enemy, and subtracting the head’s HP on each hit to one of its body parts. Then, when the head’s HP level gets down to a certain number, I want to delete a body part that is at the tail of the enemy and repeat this until only the head is left. I have this all working pretty close to how I want it to using separate objects for each body part, but I was wondering if there was a “shortcut” of using the same object multiple times as each of the “body parts”, and using an ID variable for each one to distinguish it from the others rather than making them all separate objects. If not, it’s no big deal. I’ll just make them separate objects like they are in the code example above. I just thought I would ask either way, since it would be an opportunity to learn something new.

1 Like

Is this a Zelda like game? Like the stone “snake”?

I’ve never played with a snake other than the old snake game where the snake keeps growing. The movement would be the same though. It would just shrink instead of grow.

There are multiple ways of going through the objects. Either a for each object or a repeat using an index or ID variable.

Set Index to 0
Repeat the number of segments times
–If object ID equals index do something
–add one to index

You could also use an array with a list of the IDs and use for each child. You could remove the ID from the list as the object is deleted.

Yes, it’s a Zelda game I’m making for my own learning of game development. So the enemy I’m referencing in this post is known as a Moldorm, if you look it up. They’re a snake like creature made up of multiple body parts that follow each other.

I never even thought of using an array, since I have yet to use an array and I struggle with understanding how to implement them. I know they’re just lists of variables of the same data type with each one having an index, but can I ask a general question since you brought them up?

What would be a couple of simple examples of common ways arrays might be used in game development?

Arrays are useful for storing a list of information instead of using events for each item, you can cycle through the array. It can simplify things so much.

In the plus symbol that has repeat and for each object, there’s a for each child. That will go through an array or structure. It puts the value in a variable and you can use that variable for settings or to display user names or stats.

They can be basic to complex. They can be a list of high scores or a list of cards or player or weapon stats or inventory. They can be a list of locations or questions/answers for a trivia game or quiz.

It can be a simple list of strings like character names or an array of structures that could hold player/character stats like name, health, strengths, inventory, high scores, distance traveled. Anything.

Arrays and structures are very versatile. Not to confuse you but you can mix and match arrays to store any combination of data. Not just numbers, text and booleans but you can have arrays, structures, arrays of structures, arrays of arrays, structures of arrays and so one.

Good information, thank you. I’m using quite a bit of structures so far, especially for global variables which are keeping track of what chests in the game have been opened/looted. They seem to be able to handle all the things I need in my game, so I still haven’t come across a situation where I thought an array would be necessary. I’m sure there are times when I’m using a structure in which an array would be more appropriate.

Do arrays have any clear advantages over structures in the example I gave, of tracking what chests in each dungeon/part of the overworld have been opened? Like, if a chest was opened, could I just add its ChestID variable to an array (global array) and just check that array for that ID rather than using a structure of booleans? I guess I just went the structure route because it was easier for me to understand as a complete novice.

I can’t think of any advantage. Structures and arrays are very similar. Arrays are more organized. You can get the values of specific ones like a list but by location not value.in your case, a structure is probably easier and more efficient.

In your case, adding the ID would work if you’re just running through the array with a for each child event. You couldn’t check the state on a specific chest like you can with a structure. You’d have fun the entire array or search through it. Unless, instead of adding to the array you set the same index in the array as the ID of the chest. By then it would probably be more efficient just to use a structure. If you just add a number to an array then chest 7 might be index 1. You could also have chest 7 as index 14 since arrays don’t care if there are duplicates values in different indexes. You only have one of each index not value where a structure only had 1 child with the same name.

Ok, good to know. I find structures easier to wrap my head around. How you can create multiple parent/child variables within one another is essential for managing a lot of data. For example, in the game I’m working on, I’m keeping global variables for each cave when the player does something that will carry over between scenes (collecting a key item, opening a chest, meeting conditions to unlock a door, etc). It’s nice to be able to keep a structure of global variables for each cave with meaningful names (such as CaveF12Globals.RoomsCleared.Room5, for example). It’s just kind of a pain to declare all of your global variable structures beforehand, which is what GDevelop seems to urge you to do, but it’s become much more tolerable since they improved the speed of the global variables menu in one of the recent updates (Thank you devs! That was key!)

So I guess in the game I’m trying to make, I haven’t come across a situation where I need to/should use an array over a structure YET. Index/position doesn’t seem to matter for me based on what you said. An expression or two to dynamically add to a structure’s list seems to be working fine. I guess let me know if any of the assumptions I’ve made in this post are incorrect. Lol

1 Like

Sounds good to me. Sounds like a fun project.

1 Like