Can I change an actors image with an off-screen script?

JCFordJCFord Member Posts: 785
edited November -1 in Working with GS (Mac)
I have 20 buttons on-screen and when each button is clicked the button spawns a blank actor called 'MASTER SPAWNED' and depending on which of the 20 buttons was clicked I have a long script inside the 'MASTER SPAWNED' actor that will change the image to one of 20 possible images.

On each of the 20 buttons when clicked I change an attribute called 'Game.WhichImage' so if button 1 is pressed then Game.WhichImage = 1 etc and then this is then passed onto the 'SPAWNED MASTER' and this changes it to the relevant image. i.e.

CHANGE ATTRIBUTE
change: Self.WhichImage to Game.WhichImage

IF attribute Self.Which.Image = 1
Change image to Image1.png

IF attribute Self.Which.Image = 2
Change image to Image2.png

etc etc etc right the way through to:

IF attribute Self.Which.Image = 20
Change image to Image 20.png

Although this all works. I can have a large number of these 'MASTER SPAWNED' actors on screen, is there a way that I could optimise this long script by just having it once in a hidden actor off-screen rather than repeating it in every SPAWNED ACTORS. So the hidden actor would change the 'MASTER SPAWNED' image?

I have tried but cant seem to get it to work, as I can only see a way of having the changing image action in the relevant actor itself?

Hope this makes sense!
JCFord

Comments

  • Rob2Rob2 Member Posts: 2,402
    button loads correct filename into text attribute

    then use 'change attribute' behaviour to change image rather than 'change image' behaviour, seems to work well.
  • JCFordJCFord Member Posts: 785
    @ rob sorry - can you clarify, would this work with the long script offscreen so it would not need to be repeated in every 'MASTER SPAWNED' actor?
  • scitunesscitunes Member, Sous Chef Posts: 4,047
    here's a cool trick. Name all of your images exactly the same other than a number (pic1.png, pic2.png, etc.)

    then have a game attribute called game.PicNumber and set it to one.

    Then you can have a rule that says when touch is pressed (or some other attribute changes etc.) change attribute self.image to "pic"..game.PicNumber..".png"

    This way whenever you change game.PicNumber it will cause the image to change to that file with the corresponding number.
  • Rob2Rob2 Member Posts: 2,402
    you don't need the script at all

    button loads filename into text attribute

    spawned actor loads self.image according to text attribute
  • JCFordJCFord Member Posts: 785
    @ Rob - it was going round and round in my head and I finally realised what you meant. Wasn't sure it would work, but yes it did!

    I created a attribute called game.TextHolder (text) and for each of the 20 buttons when they are clicked write the PNG file name to game.TextHolder. Then in my 'MASTER SPAWNER' I now just have 2 behaviours

    Change: self.TextHolder to game.TextHolder
    Change: self.Image to self.TextHolder

    Works a treat, so thanks Rob2 & Scitunes too.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    JCFord: You can also use the concatenation operator: [..] to combine things, like this:

    In the spawned Actor:

    Change Attribute
    self.Image To: "image"..game.currentImageNumber..".png"

    You just have to name your images in a particular way, like this:

    image1.png
    image2.png
    image3.png
    image4.png

    What you're now doing with the text attribute works as well.
    Basically, just use Change Attribute: self.Image instead of the Change Image behavior.

    I asked for similar abilities in the Change Scene behavior which would be particularly helpful in games with many levels:

    Change Scene To: "Scene"..(game.currentScene+1)

    or:

    Change Scene To: "Scene"..random(1,75)

    This method could also work with Play Sound as well.
  • JCFordJCFord Member Posts: 785
    I just want to say thanks guys... as always there are many ways to do things, but I love the way some of you really know how to think outside the box!

    JCFord
Sign In or Register to comment.