Dynamically changing the image of an INSTANCE of an actor

Hi,

I have a actor prototype for a fruit. I am spawning fruits randomly, and would like to also randomly change the image of the fruit. However, every time I change the image of an instance, it changes all the images for all the actors on the scene?

my logic is as follows:

spawner: 1) sets a global attribute to a random number between 1 - 5 2) spawns the fruit actor
fruit: 1) check a local attribute (fruit.isImageInitialized), if false, set the image based on the random global variable and then set the local attribute fruit.isImageInitialized to true

with this logic, the fruits are generated as follows: the first one is spawned with one if the corresponding images. then, when the next friut is spawned, another image is set, but it also changes the first fruit to the same image...when the next one is spawned, the frust two images are then changed to the new random image.

anyway to change the image so that it only changes the INSTANCE of the actor, not all of them?

Best Answer

  • natzuurnatzuur Posts: 304
    Accepted Answer

    @saulspam18 said:
    the global random variable is only used once for each instance of an actor; the first time that an actor is created. I have a local self. variable called is isImageInitialized (default to false)...after the first initialization, this boolean flag is set to true after the actor image is set, then the change-image code does not run any subsequent loop...it only runs once...

    e.g.
    if (self.isInitialized == false)
    {
    change image based on game.randomValue;
    isInitialize = true;
    }

    I don't think its a problem in the logic.

    the question is: when an instance actor changes their own image, why does it change the image of all instances? Is that normal? is there a way around it?

    Actually looking at that, your order of logic is probably off or it's conflicting logic. Have you tried displaying text of the self.isinitialized? It's probably not changing. Either way it would be easier to randomize upon spawn locally.

Answers

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    Don't use a game attribute. Use an actor (self) attribute instead. Each spawned instance will then generate it's own random value.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • saulspam18saulspam18 Member Posts: 5

    I use the actor self attribute when setting the image. yet all instances of the actor change. the game attribute is just a random number that is shared between the spawner actor and the fruit actor.

  • SingleSparqSingleSparq Member Posts: 1,339
    edited June 2014

    But your logic states "if false, set the image based on the random global variable " if the global random number is, say 3, then all your fruit actors would use 3 - its one attribute setting all actors to that number is it not?

    Set a randomizer in your self actor so when it spawns each actor chooses its own random number to change the image as @tatiang said

  • SocksSocks London, UK.Member Posts: 12,822

    @saulspam18 said:
    the game attribute is just a random number that is shared between the spawner actor and the fruit actor.

    Like the others have said this needs to be a self.attribute rather than a game.attribute, you don't want the actors to 'share' whatever value is generated, you want them to have their own unique / individual value.

    Ask yourself this question, if you had a load of instances being spawned - and you wanted all of them to change to the same (randomly chosen) image at the same time, how would you go about it ?

  • saulspam18saulspam18 Member Posts: 5

    the global random variable is only used once for each instance of an actor; the first time that an actor is created. I have a local self. variable called is isImageInitialized (default to false)...after the first initialization, this boolean flag is set to true after the actor image is set, then the change-image code does not run any subsequent loop...it only runs once...

    e.g.
    if (self.isInitialized == false)
    {
    change image based on game.randomValue;
    isInitialize = true;
    }

    I don't think its a problem in the logic.

    the question is: when an instance actor changes their own image, why does it change the image of all instances? Is that normal? is there a way around it?

  • saulspam18saulspam18 Member Posts: 5

    I should add that the new instances of the actors are spawned every 20 seconds or so.

  • natzuurnatzuur Member Posts: 304

    Why cant you randomize locally though? I can almost guarantee it's a logic issue.

  • saulspam18saulspam18 Member Posts: 5

    Thanks...I actually just tried dong it with a local random variable and that worked. It is strange behaviour. the logic is correct, but it seems like gamesalad is overriding it? looking at the debugger, the initialization flag is set correctly (false, then true)...its either a bug in gamesalad compiler or some weird thing with events where when an attribute value changes, my change-image section is automatically re-executed behind the scenes, even thought the initialization flag is set to true?

  • saulspam18saulspam18 Member Posts: 5

    The main problem is that I need this random variable to be shared between actors for other reasons I will not get into now (e.g. depending on what fruit is spawned, another actor does something different).

    Just for fun, I tired the local random attribute, and it worked...but the game attribute logic is sound and it not clear why its not working with a global random attribute...i think gamesalad automagically does things behind the scene when attribute values change...(even though i did not use and 'constrain attribute' behaviours)....really peculiar...

    I need to figure this out so that I can share this random variable between actors.

    @natzuur said:
    Why cant you randomize locally though? I can almost guarantee it's a logic issue.

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949

    If you need to use a game attribute for the random value then you need to change it on a timer or loop. Something like this:

    Timer every 0.1 seconds Change attribute game.random to random(1,10)      Spawn actor

    Then in the spawned actor:
    Change attribute self.myValue to game.random Change attribute self.Image to self.myValue

    You might need a rule for that last behavior depending on how you've named your images (e.g. if you named them 1.png, 2.png., 3.png, etc. then you won't need a rule). The advantage of this method is that each newly spawned actor 'grabs' its value from the game attribute but then keeps it locally stored in self.myValue for later use.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • TosanuTosanu Member, PRO Posts: 388

    @tatiang‌ Yeah, that's exactly what I was going to recommend while reading this thread, but you explained it first and probably better than i would. I see it sort of as the actor taking away a snapshot as its feature.

Sign In or Register to comment.