array: assigning tokens

0 favourites
  • 8 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • my first post, I love construct already but have got to find my way :P

    My first problem as well ;)

    ================= situation

    global text shape_all = "shape_red:shape_blue:shape_purple:shape_yellow:shape_green:shape_orange"

    global text shape_1 = "empty"

    global text shape_2 = "empty"

    global text shape_3 = "empty"

    global text shape_select = "empty"

    set shape_select to (tokenat(shape_all, int( random( tokencount(shape_all, ":"))), ":"))

    ================= problem

    I can't find a solution for

    a. randomly set shape_1 _2 or _3 to shape_select

    b. set the remaining two shape_x to something different than shape_select and different from each other (using the shape_all list)

    I was used to be working with Director Shockwave Lingo

    Have a look at chato.nl/ipad and hit the blue balloon (second screen) to see the progress

    Hope someone can help me on this one! (arrays/lists are not easy in C2 for me yet) TNx!

  • You can either,

    a:set a global to"red,blue,purple,yellow,orange", then pick a token in that string with getToken(int(random(5)),",").... comma is the delimiter.

    B: Use the system expression choose, like choose(red,blue,purple,yellow,orange)

  • Dont use shape_1, shape_2 and shape_3 but an array

    Else you will have to rewrite the whole thing for each

    global text shape_all = "shape_red:shape_blue:shape_purple:shape_yellow:shape_green:shape_orange"
    global shapeCount = 3  // number of shape you want
    +System: on start of layout
      -> Array: set size to shapeCount,1,1
      + Array: foreach X elements
        -> Array: set value at (self.CurX) to "empty"
    +On What you want
      Local text tmp = ""  // copy to keep the original intact
      +repeat shapeCount times
        -> System: set tmp to shape_all
        -> Array: set value at loopindex to (tokenat(tmp,floor(random(tokencount(tmp,":"))),":"))
        -> System: set tmp to replace(tmp,Array.At(loopindex),"")
        -> System: set tmp to replace(tmp,"::",":")
        -> System: set tmp to (left(tmp,0,1) = ",") ? right(tmp,len(tmp)-1) : tmp
        -> System: set tmp to (right(tmp,len(tmp-1),1) = ",") ? left(tmp,len(tmp)-1) : tmp

    The four last actions are used to delete the token from the tmp string. This way you don't pick it again.

        -> System: set tmp to replace(tmp,Array.At(loopindex),"")

    Delete the token leaving the colons

    1:2:3 -> ":2:3" or "1::3" or "1:2:"

        -> System: set tmp to replace(tmp,"::",":")

    Check if there's two colons side to side and delete one

    1:2:3 -> ":2:3" or "1:3" or "1:2:"

        -> System: set tmp to (left(tmp,0,1) = ",") ? right(tmp,len(tmp)-1) : tmp

    Check if the first character is a colon and delete it

    1:2:3 -> "2:3" or "1:3" or "1:2:"

        -> System: set tmp to (right(tmp,len(tmp-1),1) = ",") ? left(tmp,len(tmp)-1) : tmp

    Check if there's a colon on the last character is a colon and delete it

    1:2:3 -> "2:3" or "1:3" or "1:2"

  • Tnx, I've managed to make a workaround. Just one thing to fix:

    I put the names of the three globals in one global text, to randomly get one out of it.

    But because this is a string, it won't give its value when using this name.

    =================== begin

    global text shape_nr = "shape1:shape2:shape3"

    set shape_select to (tokenat(shape_nr, int( random( tokencount(shape_nr, ":"))), ":")) -> "shape1" "shape2" "shape3"

    Set animation to shape_select.AnimationName -> "shape1" instead of the value inside global shape1

    ========================

    probably something simple, but I'm unable to find anything that I can use; global(tokenat(......)) -> syntax error

    tnx for any hints and tips!

  • object X -> set animation to sprite(shape_select_tmp).AnimationName

    ps. shape_select_tmp = "shape1"

    No errors, but nothing happens as well :P

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Read and apply Yann's solution.

    For the problem evoked in the very first post, this is the best solution.

  • I did use Yann's solution partly, but I'm getting errors on

    System: set tmp to (right(tmp,len(tmp-1),1) = ",") ? left(tmp,len(tmp)-1) : tmp

    --> C2 indicates there can only be 2 conditions instead of 3? But 2 doesn't work

    Also the missing part in Yan's solution is that I have to randomly set shape4.animationName to shape1, shape2 or shape3's animationName.

    And for this last part Im looking for a solution, cause when I randomly get the name "shape1", "shape2" or "shape3", I'm unable to get the value of a global with this string?

    shape4 -> set animation to "AnimationName of sprite_randomly_picked" ?

    Tnx for any suggestions to solve this last issue.

  • Fixed! :D

    Yann: tnx for pointing out the directions :P

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