How do I not have variables shared across multiple instances of the same sprite?

0 favourites
  • 9 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi Everyone,

    I have several instances of the same sprite with a variable. Each of those sprites creates a text object when the sprite is created. I am wanting to have the text object pull the variable out of the instance of the sprite that created it.

    But what I'm seeing is it looks like the variable is shared across all instances of the sprite.

    How do I have the text object only pull the variable out of the sprite that created it?

    Thanks in advance.

  • Try adding a "for each" sprite object condition and see if that resolves your issue.

  • Ya I have a couple of those. Here is my code...

  • On event 13, you aren't picking any text objects in the conditions, so the actions apply to all text objects instead.

    You'll need to either have some way to identify the correct text object to set, or set the text at the same time the text object is spawned, which automatically picks it.

    To associate a text object with a sprite object, you can put them both in a container, or upon spawning set an instance variable in the sprite to text.uid (or the other way around). Then you can use the pick by uid condition (Or pick by compare instance variable, if you did it the other way).

    It looks like setting the text when spawning isn't going to work in this case since you want it to update every tick.

  • Thank you for the reply, I think understand what you are saying I'm doing wrong, but I understand the solution.

    I'm coming GameMaker and so my brain is wrapped around how it handles instances/variables, which seems to be very different here.

    Would you be willing to provide a code picture?

  • Ok, I think I was able to piece it together. Feedback is welcome...

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Looks fine? Does it work?

  • It does. Just wasn't sure if it was the 'right' way or if there is smarter way to do it.

    Thanks for your help.

  • From my own experiences, I've found updating text objects every tick can cause major slowdowns, especially when more text objects are involved. I would say best practice would be to update the text only when it changes, usually by putting the set text action in the same event that causes the change in the first place.

    If you want to save and simplify the events, containers are perfect for this situation where sets of certain objects are always associated with each other. When picking any of the objects in a container, the associated instances of the other objects in the container are picked at the same time too.

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