[SOLVED]Trying to write the right expresion

This forum is currently in read-only mode.
From the Asset Store
A cool way for kids to write and practice English Alphabets
  • I have a tiled BG. But i want the texture to be randomly loaded on the start of layout.

    i puropusly named the tiledBG pngs 1.png, 2.png, 3.png... so i can use "Random(#)" expresion on them instead of using variable switches.

    The problem is i cant write a working expresion for this.

    i tried

    AppPath & Random(10)"png"

    but this doent work.

    AppPath & Random(10)&"png"

    AppPath & "Random(10)png"

    returned negative too.

    Anybody have idea how to write this correctly?

  • The second one is corrent, but you forgot the '.' in ".png"

  • AppPath & random(10) & ".png" or

    AppPath & "" & random(10) & ".png" - sometimes i have to add empty "" for that to work and sometimes works without it :/

    if your files start at 1.png add random(10)+1

  • ahh thank you guys. i am a clumsy GameDev ^^ i always lost this small little things that makes the whole structure calapse

  • Random(10) +1 unless you have a 0.png

    Like shinkan said

  • ...

    sometimes i have to add empty "" for that to work and sometimes works without it :/

    ...

    Although Construct tries its best to convert between numbers and text, you are free to explicitly convert to text with the expression 'str':

    AppPath & str(random(10) + 1) & ".png"

    This should work without exception.

  • oww that's a great tip, thanks tulamide!

  • > ...

    > sometimes i have to add empty "" for that to work and sometimes works without it :/

    > ...

    Although Construct tries its best to convert between numbers and text, you are free to explicitly convert to text with the expression 'str':

    AppPath & str(random(10) + 1) & ".png"

    This should work without exception.

    Ampersand concatenates to string, so it should automatically be converted.

    I've seen others use the plus sign, and it worked, but I can't think of a situation where that method would be better.

  • okey. so to not make another thread:

    do you know if theres a way to write an expresion so the value would be picked randomly but from a pattern?

    example.

    i want the angle of sprite X, to be randomly set from 0, 90, 180 or 270.

    Is there a way to do this without the use of switches?

  • Do it with math:

    random(3)* 90

    Where is the problem? <img src="smileys/smiley2.gif" border="0" align="middle" /> Don't think too complicated...

  • this is one of those moments i want to smack myself in the face.

    Ahhh... why i am always blind for the obvious solutions?

    Thank you GF.

    another thing. this:

    AppPath & "" & random(10)+1 & ".png"

    gives me error saying:

    '+' does not work on 'integer' and 'string'

    the heck?

  • Try AppPath & "" & str(random(10) + 1) & ".png"

    Although, I don't understand why you have the "" in there, as that doesn't add anything to the string.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • >

    > > ...

    > > sometimes i have to add empty "" for that to work and sometimes works without it :/

    > > ...

    >

    > Although Construct tries its best to convert between numbers and text, you are free to explicitly convert to text with the expression 'str':

    >

    > AppPath & str(random(10) + 1) & ".png"

    >

    > This should work without exception.

    Ampersand concatenates to string, so it should automatically be converted.

    I've seen others use the plus sign, and it worked, but I can't think of a situation where that method would be better.

    • That's what I said, Construct tries its best to convert.
    • Never use the plus sign, the only sign you can rely on is ampersand.
    • there are situations where Construct will be confused with the information given. Just create a text box and do:

          + System: Start of layout

          -> Text: Set text to AppPath & random(10) + 1 & ".png"

    Do you now see, where that str method would be better? <img src="smileys/smiley2.gif" border="0" align="middle" />

    EDIT: Sorry, after posting I realized that Jayjay already gave that example.

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