How to make or simulate a do while like in program

Get help using Construct 2

Post » Sun Apr 14, 2013 4:17 am

I am trying of create a game when I need a DO While, can this is simulate?
B
14
S
4
G
2
Posts: 109
Reputation: 2,679

Post » Sun Apr 14, 2013 5:15 am

Do While is just an extra syntax depending on language. You need to just use while with a condtion

While
count > 0
--- count - 1

so yes you can.

Manual https://www.scirra.com/manual/124/system-conditions
it's in there somewhere :D
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Sun Apr 14, 2013 11:41 pm

ok, thank you!
B
14
S
4
G
2
Posts: 109
Reputation: 2,679

Post » Mon Apr 15, 2013 12:40 am

the do while is a special case of a while function in a programming language, where the predicate is evaluated at the end of the code block it executes.

so you usually have

Always do the first iteration, and then if the predicate is true reiterate.

So to simulate something like[code]set i to 0
do {
append i to text
i = i + 1
} while (i < 10)[/code]you would do[code]+ system: every tick
    -> system: set i to 0
    -> text: append i to text
    -> system: add 1 to i
+ system: while
+ system: i < 10
    -> text: append i to text
    -> system: add 1 to i[/code]

as you see a do while is merely a syntaxic sugar to avoid repeating some of the code you write.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Apr 15, 2013 12:51 am

I am trying to make something like this

int x=10;
int random=0;
do{
random =((int) Math.random() * 100);
if(random==4){
x=10/random;
}
}while(x==2.5);
(this is java code)
I know this can seem useless, but I want know if to make this is possible in construct 2
B
14
S
4
G
2
Posts: 109
Reputation: 2,679

Post » Mon Apr 15, 2013 2:19 am

@Yann you can also write it like this to eliminate repetitive events:
[code]+ system: every tick
    -> system: set i to 0
+ system: while
    -> text: append i to text
    -> system: add 1 to i
    + [inverted] system: i < 10
        ->system: stop loop[/code]

@shimo you could do it like this:
[code]+ system: every tick
    -> system: set x to 10
    -> system: set random_var to 0
+ system: while
    -> system: set random_var to int(random(100))
    + system: random_var==4
        -> system: set x to 10/random_var
    + [inverted] system: x == 2.5
        ->system: stop loop[/code]
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Mon Apr 15, 2013 1:57 pm

@R0J0hound
indeed thanks (:
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Mon Apr 15, 2013 10:32 pm

Thank you so much! Yann and RojoHound
B
14
S
4
G
2
Posts: 109
Reputation: 2,679


Return to How do I....?

Who is online

Users browsing this forum: Matthew de and 24 guests