How to reference a specific instance variable?

Get help using Construct 2

Post » Thu Oct 18, 2012 4:12 am

First of all, I'm trying to create a snake-like game. I'm trying to design the body part of the snake AKA make the body grow and follow the head of the snake. Any tips on that would be greatly appreciated.

Second, I ran into a problem that would be great help to any other game.

I want to be able to target a specific instance variable of a object, at any time.

For example, inside a "For each" loop of a object, I want to change the variables inside the current instance of the object, based on the variables of the NEXT instance of the object on the loop. I also want to change the variables of the PREVIOUS object on the loop.

Is there a way to do that?

Much appreciated.
B
3
Posts: 4
Reputation: 384

Post » Thu Oct 18, 2012 5:30 am

Try giving the object a variable, and set it to 1,2,3,etc for each object. Then you can reference them like this:

Repeat 10 times
Higher if you need to, then set these as sub-events:

> Object variable = LoopIndex-1
This will select the object before

> Object variable = LoopIndex
This will select the object

> Object variable = LoopIndex+1
This will select the object after
B
83
S
21
G
15
Posts: 1,039
Reputation: 15,134

Post » Thu Oct 18, 2012 9:08 pm

Ok, what if I want to do something like this:

CurrentObject.Variable = NextObject.Variable + 2?
B
3
Posts: 4
Reputation: 384

Post » Thu Oct 18, 2012 10:19 pm

I think you could have an array, where the X position is the variable I told you before, and at Y you save the value.
First you create the array object with an width of whatever you want to, and Y 1, Z 1

In this example, "ID" is the variable I told you to set before, and "Var" is the variable you are talking about.
The "+" is the action

Repeat 10 times
> Object.ID = LoopIndex #this picks the current object
+ Set Array at (LoopIndex, 1) to Object.Var #this saves the object's value to the array

Then you can have this:
Repeat 10 times (this is the same event as before)
> Object.ID = LoopIndex #this picks the current object
+ CurrentObject.Variable = Array.At(LoopIndex+1, 1) + 2

This will get all objects one after the other, by selecting the object that has an ID equal to the current LoopIndex. Then it will set its variable to the next object value, from all the values were saved to the array.
B
83
S
21
G
15
Posts: 1,039
Reputation: 15,134

Post » Fri Oct 19, 2012 12:22 am

That's what I did.

Thanks!!
B
3
Posts: 4
Reputation: 384


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 24 guests