array: assigning tokens

Get help using Construct 2

Post » Tue Feb 28, 2012 10:45 pm

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 www.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!

B
4
Posts: 9
Reputation: 868

Post » Tue Feb 28, 2012 10:54 pm

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)
Image Image
B
161
S
48
G
89
Posts: 7,347
Reputation: 66,249

Post » Wed Feb 29, 2012 12:38 am

Dont use shape_1, shape_2 and shape_3 but an array
Else you will have to rewrite the whole thing for each
[code]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[/code]

The four last actions are used to delete the token from the tmp string. This way you don't pick it again.
[code]    -> System: set tmp to replace(tmp,Array.At(loopindex),"")[/code]Delete the token leaving the colons
1:2:3 -> ":2:3" or "1::3" or "1:2:"

[code]    -> System: set tmp to replace(tmp,"::",":")[/code] Check if there's two colons side to side and delete one
1:2:3 -> ":2:3" or "1:3" or "1:2:"

[code]    -> System: set tmp to (left(tmp,0,1) = ",") ? right(tmp,len(tmp)-1) : tmp[/code] Check if the first character is a colon and delete it
1:2:3 -> "2:3" or "1:3" or "1:2:"

[code]    -> System: set tmp to (right(tmp,len(tmp-1),1) = ",") ? left(tmp,len(tmp)-1) : tmp[/code] 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"Yann2012-02-29 00:43:38
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Thu Mar 01, 2012 7:39 pm

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!
B
4
Posts: 9
Reputation: 868

Post » Thu Mar 01, 2012 9:48 pm

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

ps. shape_select_tmp = "shape1"

No errors, but nothing happens as well :P
B
4
Posts: 9
Reputation: 868

Post » Thu Mar 01, 2012 10:05 pm

Read and apply Yann's solution.
For the problem evoked in the very first post, this is the best solution.

New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Fri Mar 02, 2012 8:34 am

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.
B
4
Posts: 9
Reputation: 868

Post » Fri Mar 02, 2012 9:24 pm

Fixed! :D

@Yann: tnx for pointing out the directions :P
B
4
Posts: 9
Reputation: 868


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 12 guests