What really are instance variables ?

0 favourites
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • It's already been a few weeks since I use the instance variables and I just noticed that they reset when changing scene.

    My question is therefore is it possible to keep them between the scenes?

    Or is reset mandatory? -> if it is the case how to overcome the problem

  • Instance variables store values for each instance. If you have 100 enemies, each enemy instance can have its own 'health' value for example.

    When you switch layouts, new instances of all objects on the new layout are created, with default values in instance variables.

    You can set an object as Global, and then it will be transferred to another layout with all its properties.

  • Thank you for the explanation it allowed me to resolve the problem :)

  • A second question came to me today: can we compare the variables of instances within the same family? (so as to find which object of this family has the third highest value)

  • You can, but you need to define the instance variable on the family level.

  • Okay, but how do I do that? (I haven't seen any action allowing me to do this)

  • I mean remove instance variables from the object and add them to the family. The objects in the family will inherit these variables.

  • In fact it’s already what I was doing ... But I don’t know how to compare the instances of the same family with each other

  • Could you give more details about what you are trying to do?

    You can pick one Family instance and one Object instance (from this family) in the same event, and compare their instance variables. For example - "If Enemies.hp > Zombie.hp"

  • Basically I would like to make a system of "classification" in the mario kart for my racing games. (ie a classification in real time on the side of the screen)

    I have already created an instance variable for the "player" family which is a turn counter.

    But how do you compare the instance variables of each player to know their position in the ranking?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • So you want to display the list of player sorted by an instance variable? You can easily do this with "For each (ordered)" system event.

    For each Players ordered by Players.TurnCount

  • It may sound a bit silly but I don't know where to go with that ...

  • Do you want to display player names? Then it can be something like:

    Every 0.5 seconds
     Set RankingText text to ""
     For each Players order by TurnCount
    	RankingText append Players.name & newline
    

    where RankingText is a large text object that can fit many lines.

  • OK not bad ! Is it also possible to have an image associated with each player next to their name?

  • Of course it's possible, but the solution depends on how your objects are organized. If you have a separate sprite with player's icons, the easiest option is to put it into the same container with Player sprite. Then the icon will be picked automatically inside the loop

    For each Player order by TurnCount
    	PlayerIcon set Y to 500+loopindex*40
    
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)