How to reference a specific instance variable?

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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.

  • 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

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

    CurrentObject.Variable = NextObject.Variable + 2?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

  • That's what I did.

    Thanks!!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)