Use Variable to select a sprite object

I found a movement routine to move a sprite named Glleft, but i would like to use the same routine to move a sprite named Ted, can i use a variable to pass the name of the sprite i would like to move
in the pic below I need to change any references to Glleft to Ted via a Variable.

Hi, based on this thread Using a text variable to replace an object's name it seems that this is not really possible, but there are other options.

One suggestion is to put your Glleft and Ted in an object group and instead of referring to a specific object in your events you just need to put in the group.

Another possibility could be to make your own custom behavior. After adding it to your project you can just choose which object should have the behavior (e.g. as in your case the movement). Custom behaviors: create your own behaviors with events [GDevelop wiki]

Hey!

I think I have an idea that may work. Create a group called something like Movable Objects. Then in your events, you can have a for each object event using the group. Attached is an example:

A few notes:

  • It is supposed to say Moveable_Objects where it says {ObjectName} (probs a bug)
  • if you were to use this method, you must make these into object variables rather than scene variables, otherwise all of the objects will be affected at once (object variables can be used / accessed by naming the object first: Object.Variable(VariableName)
  • I have not been able to test this, but it should work :crossed_fingers:
  • Yes I spelt moveable wrong, just noticed

If you need any help, have any questions, or don’t know how to use object variables, please let me know! :grin:

OB

To be clear, @Drona already suggested object groups, I just thought an example may prove more useful!

1 Like

Thanks both, i will give it a go