How can I get the position of an instance of an object?


I’ve been trying to find the answer all morning looking on the forum but haven’t found anything.

I have 60 instances of an object and I want to be able to get their positions. I’ve set up an ID variable but can’t find a way to access a particular instance to get its position so that I can move my player there.

At the moment it’s looking like I’ll either have to create an array of all the positions or create 60 distinct objects :frowning:
There must a better way, no?

Any ideas?

It sounds like you need to use the “For each object” event type, although it would be more accurate to call it a “for each instance” type. This cycles through every instance of a particular object, allowing you to use the value of the ID variable as a condition for selecting one of them. The action would then either be to set the player object’s X and Y positions to the same as the instance of the object, or set other variables and use them later to set the X and Y of the player.

I don’t know which version of GDevelop you’re using, but you need to click the icon that looks like a big + in a circle, that’s near to the other ‘add event’ icons.

If each instance (Target) has an unique ID, to move the Player to the instance with ID (e.g.) 5, you can do it:

Conditions: Variable ID of object Target is = 5 Actions: Do = Target.X() ; Target.Y() to the position of Player
Maybe you have problems to get the ID value?

Thanks, yes that probably is the way to go. I’m using version but it’s ok, I know where to find it.

My problem now is calculating the ID. I’ve written an external event to do this but it doesn’t seem to be getting any of the scene variables.
I’ve tried adapting the code from the wiki[]=javascript&s[]=code:

var myVar = runtimeScene.getVariables().get(“MyVar”);

var currentValue = myVar.getAsNumber();

To :
var cDirection = runtimeScene.getVariables().get(“currentDirection”);
var cPpos = runtimeScene.getVariables().get(“currentPosPoint”);
var cInstruction = runtimeScene.get(“currentInstruction”);

var cd = cDirection.getAsNumber();
var cp = cPpos.getAsNumber();
var ci = cInstruction.getAsNumber();

but I’m getting the following error in the console: TypeError: runtimeScene.get is not a function :cry:

Here, you forgot the “getVariables()” part before the “get()” function :slight_smile:
If you want to work with one instance only in a Javascript event, you can select the instance with a common event condition as I did in the previous post and put the Javascript event as a sub-event of this one. If you do it the object list passed to the Javascript event will have length one, containing your selected object :wink:

Duh! (facepalms herself) That will teach me to not put things to one side and come back to them later with a fresh eye. :astonished:

No error now, but also no values in cI, cd, cp :frowning:

Also I’ve tried putting dummy values in and saving the result to a global variable but that’s not working either. :frowning:

I will do as you suggested for the instance.

The external event is to calculate the ID of the instance I need which depends on the current instance (where the player is standing at the moment), current direction and, and this is where it gets complicated, the user instruction. At the moment I’m putting in dummy values for this because if and when I get this part working I want to use speech to text for the user instructions.

Not sure, I would need to check the events, but “runtimeScene.getVariables()” returns a list of the scene variables, to get/save global variables you have to use (checking sources…) “runtimeScene.getGame().getVariables()”, if it isn’t the problem I will need more info :confused:

Thanks for your response.

Unfortunately I’m no closer to getting it to work and I’m confused.

“runtimeScene.getGame().getVariables()” to save a global variable? :confused:

It will return the list of global variables, then you access your variable from this list and finally modify the value:


Yay! Cheers :slight_smile:
I’ve switched the other variables to globals and that part seems to be working now.
Now to figure out why the player isn’t moving to the Centre of the object now.
Have a great day.