Use value of variable to select an object?

Before proceeding I know this question has been asked before in February 2018 (“Selecting an Object based on the Value of a Scene Variable?”), but with no solution (at least not for my problem) and I was hoping that in 5+ years some workaround has been found.

I’ve been loving GDevelop so far. This is the first time I’m really trying to finish a game, but this game engine lacks some super basic features.

As the title states I’m trying to select an object based on a variable. I’ll attempt to explain myself better with a screenshot and an example:
Let’s say I have 100 types of projectile objects (basic arrow, steel arrow, fire arrow, etc.) and I have a variable that stores the last projectile equipped (projName = basic arrow); how can I select the value of the variable as an object?

If I can’t select an object this way, my game would need thousands of conditions just to determine the projectile type or who’s projectile is (enemy? ally?, neutral?). The same goes for weapons.

Like the guy who asked in 2018 I also thought to change the group of the object in events, but that’s also not possible in GDevelop. So is there anything I can do to fix this issue?

Thank you in advance.

Variables can only contain strings or number values. Object names in conditions (and most actions) are objects/references and not strings nor numbers. You will not be able to dynamically select an object in a condition that way, no.

However, what you’re building doesn’t make sense to me. You shouldn’t be selecting an object instance to shoot it in the first place, because you shouldn’t be keeping one object instance for your projectiles.

Unless you have more context on why you’re designing it that way, my recommendation would be:

  1. Have all projectiles in an object group.
  2. Set up logic to select your projectile (this is up to you, but just make sure you’re targeting the Object group)
  3. Add logic for players and enemies somewhat as follows:

For your player:

  1. When selecting the projectile to fire (or having done so previously), set the instance variable on your player. Make sure to be targeting the ProjectileObjectGroup, and store the variable as ProjectileObjectGroup.ObjectName()
  2. Have an event set up for when you’re going to fire.
  3. As an action in that above event, use the “Create Object from group by name” event action, target the Projectile object group. For the name, use YourPlayer.VariableString(WhateverVariableYoucreatedIn#1Above). Now that instance is created, any actions following this action will target that specific instance.

For enemies:

  1. Have all enemies in an object group.
  2. Create a “For each” event targeting the enemy object group.
  3. Follow the same logic as the player above. Make sure you’re not trying to use Trigger Once, as that will make the event only occur for the first instance of the enemies. If you need trigger once, you’ll need to use a boolean variable and reset it after the monsters have fired.

I hope this helps, but if the above doesn’t solve this for you, you will need to provide more context on what you’re trying to accomplish and why it doesn’t work (also be sure to include your events if you change them).

Sorry for the late reply and thank you so much for your detailed response. I’m a beginner so I get why it doesn’t make sense, I’m sure there’s a better way to do this, as you’ve tried to teach me.

I want a “realistic” bow shot: place the arrow in the string of the bow, charge the shot moving the arrow with the string and releasing the arrow. Once the arrow is released, obviously I want to stop THAT specific projectile to teleport again to the string of the bow, so I thought on giving every projectile the boolean variable “shot”. If the projectile IS “shot”, it will remain in the ground, where you can pick it up or just wait for it to despawn. I don’t really know how to differentiate a projectile from another so I was testing if I could store the name of the projectile and call it as an object. I hope I have explained myself better this time, english is not my native language, but I’m trying my best.

Here is a gif of what I’ve achieved so far (forgive me for the horrible pixel art):
BowShot

The problem here is that if I wanted to charge another arrow while maintaining the previously shot arrow on the ground, I would teleport both to the bow. Or if my character unlocked the “triple shot” skill I would be charging 3 arrows on the same spot, same angle. And this last part is an extra issue I have btw, I don’t know how to change the initial angle of an object while still looking at the mouse. I had to redraw some sprites because my sprites are always pointing from the east (if that makes sense), so to point the angle at the mouse position I couldn’t draw an arrow diagonally, for example.

I know there’s an extension called FireBullet, which has been really useful for guns, but I don’t get how to use it for bows or if I should even use it for bows.

Thank you for reading.

  1. I love your pixel art style and how the simple animations look.

  2. I’m not completely sure you should use the FireBullet extension for your arrows, I think what you want to make requires something different.

  3. I think you need to handle each instance of the arrows as separate objects, using a For Each loop. It’s basically the same logic @Silver-Streak suggested to you, but for the arrows.

I’ll try to make a simple example for you.