(newbie question) Trying to set multiple instances of the same actor to a random color

Hi all, I'm just experimenting here and I can't figure out if there's a better way to do something.

At the start of my scene, I want to have multiple instances of a simple square actor, but with each one set to a random color (Red, Blue, Yellow or Green). I brought four instances of the square in to the scene and I set rules in the square's prototype to pick a random value for game.RandomColor, between 1 and 4, which then change square.color to be the corresponding color (set in the rules).

What happens (I thought it might) is all instances of the square share the same color. I guess it's only picking one random number and applying the condition to all instances (I was hoping that each instance we run it's full set of rules, allowing game.RandomColor to change value for each one).

I guess an easy way to fix this is to create something like game.RandomColor1, game.RandomColor2, etc. However, my goal is to eventually build something with lots of actors, each choosing a random color, so this method might have me creating 20 or so variables. It's doable, but I was wondering if there's a more optimal way to go about it.

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    The issue is that you're choosing a random value for a single game attribute that all of the instances are referring to. As soon as the scene loads, game.RandomColor will be set to 1, 2, 3, or 4, and each instance will immediately choose a color based on that single value (e.g. 2).

    What you need are self attributes. Just add self.RandomColor (index/integer) to the actor's prototype and then substitute that for game.RandomColor in the rules. That way, each instance will generate its own unique value and refer to that instead.

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

  • wirelesstkdwirelesstkd Member Posts: 75
    Thanks, tatiang, that makes sense! :)
Sign In or Register to comment.