How do I rotate an object when condition met.

Get help using Construct 2

Post » Sat Mar 15, 2014 12:56 am

nimos100 wrote:Loops works fine inside events, but you have to understand how C2 execute code.

When C2 reads it start from the top and go through each event and execute whatever it should.

When it reaches a loop it will repeat the loop X amount of times, before moving on.

The wait statement is a bit weird and think that might be what confuse you. The wait statement, doesn't store X amount of waits even if you repeat it 90 times. It will do it once when the wait timer is up.

There are nothing wrong with using loops in conditions as long as you know how C2 handles them. Hope that makes it a bit clearer.


Well, loops inside events aren't truly executed within an event if it is as you say. I will post another topic on NESTED LOOPS, which may be what I was getting at. Other programming languages handle loops entirely within their nested position in code, without the use of global variables...it seems C2 does not.

In most programming languages I've worked with, an example like this:

On Mouse.click
{
Repeat(90)
{
System.Wait(1)
Sprite.Rotate(-1)
}
}

here you can see that the loop, Repeat(90), is nested inside an event, Mouse.Click. Here, when the mouse is clicked, it starts the Repeat loop which will: tell the system to wait 1 increment of time, then rotate the sprite counterclockwise 1 degree, then tell the system to wait 1 increment of time, then rotate the sprite counterclockwise 1 degree, then tell the system to wait 1 increment of time, then rotate the sprite counterclockwise 1 degree, then tell the system to wait 1 increment of time, so on and so forth until that was done 90 times and THEN exit the loop. Inside the loop code, Repeat, it executes whatever commands it finds there from top to bottom until it is done and then it exits the loop.

C2 seems to run a nested loop only once and then exits. Of course, if the loop isn't nested in C2, it seems to work fine. At least, that is how I see it so far.
B
19
S
7
G
7
Posts: 88
Reputation: 4,117

Post » Sat Mar 15, 2014 1:01 am

RamPackWobble wrote:my attempt...

wait loopindex*delay


Thank you, still, unfortunately, not what I was looking for, but very interesting to see how people tackle same problem.
B
19
S
7
G
7
Posts: 88
Reputation: 4,117

Post » Sat Mar 15, 2014 1:03 am

Yann, thank you.

I'll consider this thread closed now and I'm going to look more into Nested Loops to see if C2 really has that functionality...or start a new discussion on it.
B
19
S
7
G
7
Posts: 88
Reputation: 4,117

Post » Sat Mar 15, 2014 9:08 am

TheDoctor wrote:
RamPackWobble wrote:my attempt...

wait loopindex*delay


Thank you, still, unfortunately, not what I was looking for, but very interesting to see how people tackle same problem.


you asked for a loop this is what mine does (I used a for/next rather than a repeat) inside a condition - what more do you want ?

On Mouse.click

--for count = 1 to 90 -- System.Wait(1)
-- Sprite.Rotate(-1)

as you have this (wait 1) it will run through its loop 90 times (almost instantly) but each loop will be delayed by 1 second so ALL 90 loop will run instantly be delayed by 1 second so ALL of the 90 loop will finish 1 second after they have started.
now...
do the loop again but with wait (loopindex) ALL 90 will loop instantly and then carry on with the rest of the program BUT the first will delay by 1 second(and therefore rotate the sprite 1 degree 1 second after the loop starts), the second loop will delay by 2 seconds(and therefore rotate the sprite 1 degree 2 second after the loop starts), the third by 3 seconds, etc until loop 90 will finish 90 seconds later and your sprite will have travelled 90 degrees.

Run (my) Capx (again ?) but replace the for/next with repeat 90 - works as expected ?
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,882

Post » Sat Mar 15, 2014 1:22 pm

@TheDoctor
I understand your confusion now.
In unity and java, python, etc, you can use some kind of wait or sleep to tell the thread to just stop execution for a (more or less) specific amount of time.
But you don't really have threads in javascript (or rather you only have one), so the wait can't work the same.

What the wait does is registering a set of actions to be called after a certain amount of time.
But doing this registration happens in one go. The repeat 90 will register 90 set of action to be executed after a given time (hence the wait loopindex * 0.3)

If the wait was really blocking the thread, all the game will stop receiving inputs (:
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Mon Mar 17, 2014 1:25 am

RamPackWobble wrote:
wait loopindex*delay


Thank you so much! That little bit of code was what I was looking for. You threw me for a loop :lol: when you had all that other code in there.
B
19
S
7
G
7
Posts: 88
Reputation: 4,117

Post » Mon Mar 17, 2014 1:56 am

Yann wrote:@TheDoctor
I understand your confusion now.
In unity and java, python, etc, you can use some kind of wait or sleep to tell the thread to just stop execution for a (more or less) specific amount of time.
But you don't really have threads in javascript (or rather you only have one), so the wait can't work the same.

What the wait does is registering a set of actions to be called after a certain amount of time.
But doing this registration happens in one go. The repeat 90 will register 90 set of action to be executed after a given time (hence the wait loopindex * 0.3)

If the wait was really blocking the thread, all the game will stop receiving inputs (:


Thanks! I get it now. Never programmed in one thread before. Now I understand why the loopindex works!

Thinking about it though, wouldn't it be easier and simpler to have Construct just automatically multiply your delay by the loop index? I mean, generally when a programmer puts a delay inside of a loop the programmer is looking to have whatever is in that block of code repeated from top to bottom as many times as the loop executes. If the Wait command was outside of a loop it could default to running once. To me that would make the coding a lot clearer. I'm betting this is a headache with a lot of people new to Construct. -at least those that want to understand it well.
B
19
S
7
G
7
Posts: 88
Reputation: 4,117

Post » Mon Mar 17, 2014 2:08 am

TheDoctor wrote:
Yann wrote:@TheDoctor
I understand your confusion now.
In unity and java, python, etc, you can use some kind of wait or sleep to tell the thread to just stop execution for a (more or less) specific amount of time.
But you don't really have threads in javascript (or rather you only have one), so the wait can't work the same.

What the wait does is registering a set of actions to be called after a certain amount of time.
But doing this registration happens in one go. The repeat 90 will register 90 set of action to be executed after a given time (hence the wait loopindex * 0.3)

If the wait was really blocking the thread, all the game will stop receiving inputs (:


Thanks! I get it now. Never programmed in one thread before. Now I understand why the loopindex works!

Thinking about it though, wouldn't it be easier and simpler to have Construct just automatically multiply your delay by the loop index? I mean, generally when a programmer puts a delay inside of a loop the programmer is looking to have whatever is in that block of code repeated from top to bottom as many times as the loop executes. If the Wait command was outside of a loop it could default to running once. To me that would make the coding a lot clearer. I'm betting this is a headache with a lot of people new to Construct. -at least those that want to understand it well.


I don't think it would be a good idea, It is logic right now, it is like having the same set of event (not only actions) placed one after another:

"Repeat
Simply repeat the event a given number of times. This tests any conditions following it on every repeat, and if those conditions are met also runs the actions and any sub-events on every repeat."

it works exactly the same way, it might not be straightforward to you, but it is coherent with the rest of the actual engine, and also to the way it is defined in the manual.

Also this tutorial by ashley explains the wait, and how to use it in loops too : https://www.scirra.com/tutorials/56/how-to-use-the-system-wait-action
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Mon Mar 17, 2014 2:40 am

The headache usually comes more from not understanding loops than misunderstanding wait.

And building something on assumption about why programmers do what they do is a bit dangerous. There's already a lot of programming style out their and I'm not sure they would all agree on that one (:

For example, in Java you would probably do something like
Code: Select all
class MyObject {
    private static final float ANGLE_STEP = Math.PI/180;
    private static final float AMOUNT_TO_ROTATE = Math.PI/2;
    private static final int ROTATION_RATE = 300;
    private float angle = 0.0;
    void onClick() {
        new Thread(new Runnable() {
                void run() {
                    float deltaAngle = 0.0; // auto reset
                    float startAngle = angle;
                    while(deltaAngle < AMOUNT_TO_ROTATE) {
                        deltaAngle += ANGLE_STEP;
                        angle = startAngle + deltaAngle;
                        sleep(ROTATION_RATE);
                    }
                    angle = startangle + AMOUNT_TO_ROTATE; // to be sure to avoid any imprecision
                }
            }).start(); // I always forget that one
    }
}


In javascript you would do the more or less equivalent:
Code: Select all
var MyObject = (function () {
    var ANGLE_STEP = Math.PI/180;
    var AMOUNT_TO_ROTATE = Math.PI/2;
    var ROTATION_RATE = 300;
    function MyObject() {
        this.angle = 0.0;
    }
    MyObject.prototype.onClick = function() {
        var deltaAngle = 0;
        var startAngle = this.angle;
        var self = this;
        function rotate () {
            deltaAngle += ANGLE_STEP;
            self.angle = startAngle + deltaAngle;
            if (deltaAngle < AMOUNT_TO_ROTATE) {
                setTimeout(rotate,ROTATION_RATE);
            } else {
                self.angle = startAngle + AMOUNT_TO_ROTATE;
            }
        }
        rotate();
    };

    return MyObject;
})();

In javascript you don't have any kind of loop, you just give a callback to the timer system and call the function again until you're done (or use setInterval). That's more or less what I do with the capx I provided using the timer behavior.
B
66
S
22
G
14
Posts: 1,484
Reputation: 16,511

Post » Mon Mar 17, 2014 3:15 am

Aphrodite wrote:
I don't think it would be a good idea, It is logic right now, it is like having the same set of event (not only actions) placed one after another:
https://www.scirra.com/tutorials/56/how ... ait-action[/url]


Consider this, the entire purpose of a loop is to execute a set of events over and over and over and over until it has done it enough times or stopped by some event. Logic, and the standard way of thinking in most programming languages, I believe, would dictate that loops, intuitively, are simply repeated actions.

If I want to rotate a wheel 90 degrees clockwise, then I would simply rotate a wheel 90 degrees clockwise. You don't need a loop for that. If I want to rotate a wheel 90 times, one degree at a time clockwise, then I'd use a loop. I'd only put a command in the loop one time at one degree and then the loop would execute that 90 times. So, whatever I put in the loop code block, intuitively and logically, I want that to run for as many times as the loop runs. Whatever happens behind the scenes is all up to the creators of Construct. They can choose to program the Wait command any way they want, but I believe that the more intuitive you make something, the better the product is and the happier the users are!

Right now, I believe most new users of Construct, especially those who program in other languages, would find it a headache to wonder why their instructions in a loop execute "all at once". Certainly they can look into the matter spending a few hours searching the manual, looking through many tutorials which aren't cross referenced and even wait for someone to answer "that same question again" on the forums, but that means the user interface isn't as intuitive as it could be. There is room for improvement.

For me, I read the manual many times looking for the answer but couldn't spot it. I was looking through the tutorials, but I didn't think the problem was the Wait command, I thought I was structuring my loops the wrong way! I was doing all kinds of searches on LOOP STRUCTURE! I thought I had the conditions and sub events with my actions in the wrong place!
B
19
S
7
G
7
Posts: 88
Reputation: 4,117

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 39 guests