How to refer to Instance varibale?(similar to self.variable)

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hello.

    Each of instance has an instance variable called "animation_number".

    I need to get acces to this variable through expression, similar to:

    sprite.animation_number (certainly not with a dot...)

    Is there any way to acces this instance variable that way?

    Like... sprite.variable(animation_number) or anyway??

    Thanks a lot for any advise.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Why would you want to access it another way?

    If you explain the problem and why this is an issue, then we can help more.

  • Sure thanks

    I am building a beat'em up game.

    I want to put all "enemy" characters into one sprite, where number represents an enemy type animation (1-monster, 2-zombie, 3-soldier), like this:

    1_moving

    2_moving

    3_moving

    1_standing

    2_standing

    3_standing

    1_was_killed

    2_was_killed .... etc.

    When "on start of layout" I want to put random sprites on the stage, I make a random number to each instance of an enemy (called "character_type"), and then based on that, I want to use animation based triggers, to do something with the sprite, for example:

    "enemy" On animation "was_killed" finished => do some explosion, or whatever...

    Now, I should be able to have it rather like that:

    "enemy" On animation self.type & "_was_killed" finished => do some explosion, or whatever...

    Why do I do that in a such a twisted way: because that way I can define all behaviours for one sprite. Otherwise, if I create 20 different enemy types I will have to have 20 different actions/interactions defined. And if I have to alter something in their code, I will have to update 20 sprites, not just one.

    I hope it makes sense what I am typing

    Thanks a lot again!

  • Make a separate sprite for each enemy type, but put them all in the same family.

    You can then use variables/actions at the family level, so that you only need to write your code once, rather than 20 times for each one.

    Another way would be to define a function, and then call that function from each enemy. Again, that way you're writing code only once but reusing it in several places.

    Hope that helps.

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