Ask here your questions to get help using GDevelop.
By annemb
#65071 Hi,

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 :(
There must a better way, no?

Any ideas?
By MattLB
#65076 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.
User avatar
By Lizard-13
#65081 If each instance (Target) has an unique ID, to move the Player to the instance with ID (e.g.) 5, you can do it:
Code: Select allConditions: 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?
By annemb
#65082 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 [url][]=javascript&s[]=code[/url]:

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:
User avatar
By Lizard-13
var cInstruction = runtimeScene.get("currentInstruction");

Here, you forgot the "getVariables()" part before the "get()" function :)
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 ;)
By annemb
#65117 Duh! (facepalms herself) That will teach me to not put things to one side and come back to them later with a fresh eye. :o

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

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

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.
User avatar
By Lizard-13
#65121 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 :?