Background selection.

As_Of_LatteAs_Of_Latte Member, BASIC Posts: 343

I have an endless runner I am working on. I am trying to set a rule that will randomize the background image between a set of 3 images, every time the user presses "Replay".

I made a game integer attribute called GameplayBG and set it to 0.

I have a main background actor with the following rules inside;

Rule:
When Numerical Expression, random (0,2) = 0

Change game.attribute GameplayBG to 0.

Set image to Background 1


Rule:
When Numerical Expression, random (0,2) = 1

Change game.attribute GameplayBG to 1.

Set image to Background 2


Rule:
When Numerical Expression, random (0,2) = 2

Change game.attribute GameplayBG to 2.

Set image to Background 3


The problem I'm having, sometimes the game.attribute Gameplay BG will be 0,1 or 2, but the corresponding background image I've set to it seems to disappear...Any advice?

Hope this makes sense and thank you in advance :)

Comments

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

    You need to choose a random value and THEN evaluate using rules. The way you have it set up, you're choosing a random value inside of each of those rules. So it should look like this:

    Change Attribute game.random to random(0,2)

    When attribute game.random=0
    ...
    When attribute game.random=1
    ...
    When attribute game.random=2
    ...

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

  • As_Of_LatteAs_Of_Latte Member, BASIC Posts: 343

    @tatiang Awesome!! Don't know why I didn't see that... :s But, I'm glad you did and I appreciate your quick reply/knowledge. Thank you so much! Everything works great.

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2015

    [for the sake of clarity/sanity :tongue: I've momentarily abandoned your offsetting of values to images by -1, instead I will stick to a value range of 1-3 = 1-3 rather than 0-2 = 1-3).

    You are saying . . . .

    If a random number between 1 and 3 (unrelated to the below random number) = 1 then do A
    If a random number between 1 and 3 (unrelated to the above random number) = 2 then do B
    If a random number between 1 and 3 (unrelated to the above random number) = 3 then do C

    But of course all three conditions could be true at the same time, you could run your rules and get a positive from all three rules. I think you are conflating generating a random selection with processing a random selection.

    You should first generate the random value and then (after it has been generated) process it, so it should more like this:

    Generate a random number - 'X'
    If X = 1 then do A
    If X = 2 then do B
    If X = 3 then do C

    Personally I'd just change the names of the BG images to 1, 2 and 3 and dump all the rules and attributes and just use: Change self.image to random (1,3)

    EDIT - what tatiang posted while I was typing !

  • As_Of_LatteAs_Of_Latte Member, BASIC Posts: 343

    Thanks @Socks ! I thought I'd offset my values just to brighten everyones day, haha ;) I appreciate your response.

  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2015

    @As Of Latte said:
    I thought I'd offset my values just to brighten everyones day, haha ;)

    :smiley:

    For the sake of the children use random (1,3) !

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

    @Socks said:
    Change self.image to random (1,3)

    Yep, that's simplest. Just a single behavior.

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

Sign In or Register to comment.