Pages: 1 2
Author Topic: Very hard time with ENIGMA (while, for, until, etc.)  (85,296 Views)
Offline (Unknown gender) Darkstar2

Member
Joined: Jan 2014
Posts: 1,238
View profile
Reply #15 Posted on: May 08, 2014, 01:50:48 AM
Yes now that I see it visually it makes sense that the FOR would execute completely each step as opposed to incrementally.  Understood, so in this case I should increment the value of variables myself outside of a loop.  So in which cases then would a for loop be properly used in a step event ?  Also I can't get while, do until to work at all.  Even without for / loops. and most of the code in games are done in step events. :D
So it's hard for me.  I'd have to create tons of control objects to do individual tasks instead of being able to put every thing inside a step event in my main controller.

Here is an example experiment, let's say I want to check if key up is PRESSED, if it is then object's x
and y positions would change to a random value 10 times and then wait.  Whilst this is happening, having  the feature of ignoring the key check since it is being processed ......

This would obviously not work and I know why:

if keyboard_check_pressed(vk_up)
   {
   key = 1;
   }
   
if (key = 1)
   {
        repeat (10); {
   x=random(room_width);
   y=random(room_height); }
   }
key=0


So how would I be able to make this work ?

Refer to post below this one, I figured it out.

An alternative would be to set a controller object
and increment a variable manually with an if
and create instance of other object and destroying itself.  and calling this controller through instance create.   that's why I was wondering I can do all this in a single step event of the instance object.

Offline (Unknown gender) Darkstar2

Member
Joined: Jan 2014
Posts: 1,238
View profile
Reply #16 Posted on: May 08, 2014, 02:04:24 AM
Ok, experimented a bit and done it.  For something that needs to be done incrementally step by step, I need to do it this way, for something that needs complete execution of a loop inside 1 step, I would use for loops.

So here, this would work instead of what I wrote above this post:


if (key = 0)
{
if keyboard_check_pressed(vk_up)
{
key = 1;
}
}

if (key = 1)
{
i++
if (i < 11)
{
x=random(room_width);
y=random(room_height);
}
else
{
i = 0;
key = 0;
}
}


and inside draw event


draw_text(10,10,"Key:  "+string(key));
draw_text(10,20,"i:  "+string(i));
draw_self();



This works !

I even added a debug for variable i and variable key,
it works as it should.  When key up is pressed key = 1, whenever key =1 it ignores vk_up, until execution of key =1 code is done, then it resets i and key.

I used a draw event and I can see the counter incrementing every step.

Is it correct what I did above  ? Is the coding clean
or is there a better way to do the above?

Is there any use of while / do / until in code above as I tried it and it did not work at all.

Now, I applied what I learned in how for loop works in other languages and done something with arrays.

Now here is some new experiment I've done with for loops successfully.

in Create code


word = 0;
firstrun = 1;
i = 0;


In Step code


if (firstrun = 1)
{
for (i=0; i < 11; i++)
{
word[i]="Count: "+string(i);
}
firstrun = 0;
i = 0;
}


So array creation will only occur once when this object is created.

In draw event


for (i = 0; i < 11; i++)
{
draw_text(10,(10*i),word[i]);
}



Result:

Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Count: 6
Count: 7
Count: 8
Count: 9
Count: 10

-------------

Success!   Now the while / do / until for another day.
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #17 Posted on: May 08, 2014, 08:16:15 AM
QuoteOn an expert level yes I used BASIC and I can swear on my life that it worked differently.
The difference is that in Basic you probably didn't have an engine. I think you didn't even draw anything to a rendering context. All you did was output to console. That is a lot different then rendering to screen. That is where your misunderstanding came from. If you had used some graphics engine with BASIC you would have noticed the same thing you noticed here.

QuoteUnderstood.  So, in this example, how can I make an object move 100 pixels (1 pixel at a time) using a for / loop then ? 
If you actually want to see the object move, then you shouldn't use a for loop (unless you want to use the screen_redraw() "hack"). If you want 1 pixel at a time, then that means you need to increment the position 1 pixel per step. That means adding x+=1 to a step event. That's it.

QuoteSo in which cases then would a for loop be properly used in a step event ?
Well, whenever you have to loop something. Like looping over objects or looping over arrays. Previously you showed a code used for a collision that used the while loop. That is a very popular place to use it. Basically you use it the same places you would in C++ or BASIC or anything else. If you wanted to move a box 1 pixel at a time and render that movement on the screen, then you wouldn't use a for(){} in BASIC either.

QuoteNow, I applied what I learned in how for loop works in other languages and done something with arrays.
You could just put the loop in create. No need to have it in step if you only run it once.

QuoteIs it correct what I did above  ? Is the coding clean
or is there a better way to do the above?
It's ok. I would probably just use an alarm.

So to recap. This is what your code looks like in ENIGMA (or any game engine):
while (true){
    //start logic, like your step event
    for (i=0; i < 99; i++)
    {
draw_text(10,10,"Count: "+string(i));
    }
    //end logic

    screen_redraw();
}
So it's a loop inside a loop. The first loop is the main one and executes once per step. The inner one is your step event that gets executed. And screen_redraw() is the function that actually draws something on the screen. You should notice from this example why your previous codes didn't work as you expected. When you understand you are already in an infinite loop, then it should be easier for you to make things. Like if you wanted your object to move to x = 100 with 1 pixel per step, then you just do this:
//Create event
x = 0;

//Step event
if (x<100) ++x;
So this is actually easier and shorter than what you wanted to do with the for loop.

edit:
Also, you could technically do what you want with threading. You could create a script and place something like this in it (haven't tested it):
for (var i=0; i<100; ++i){
   obj_someobject.x = i;
   sleep(1/room_speed);
}
And then call that with script_thread() (probably in Create event). What this would do is change the object's x position, the go to sleep for 1 step during which time the screen should redraw in the main thread. Then you modify the position again and sleep again. Until the loop is finished. But this isn't a good solution, as it can cause race conditions, consume more resources and create jerky movement (like it's possible during a slowdown that the object moves twice in one step instead of once).
Offline (Unknown gender) Darkstar2

Member
Joined: Jan 2014
Posts: 1,238
View profile
Reply #18 Posted on: May 08, 2014, 08:50:42 AM
Very interesting thanks :D

Yes indeed placing it in create worked.

BTW, what function do I use to find out the length
of an array?

example


{
i=0;
word=0;
for (i=0; i < 10; i++)
{
word[i]="Count: "+string(i);
}
}


This is in create event.

Let's say I want to find length of the array word,
in this example it should return me a 9.

array_length_1d compiles with error function not found, even though the command is highlighted and documented.

Offline (Unknown gender) onpon

Member
Joined: Feb 2010
Posts: 102
View profile WWW
Reply #19 Posted on: May 08, 2014, 09:13:54 AM
Quote
BTW, what function do I use to find out the length
of an array?

I don't think EDL supports that. C and C++ don't, either. You just have to remember the length by putting it in a variable.
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #20 Posted on: May 08, 2014, 09:47:12 AM
QuoteBTW, what function do I use to find out the length of an array?
As onpon pointed out, the C++ array doesn't support that. You should either save the length in a variable on your own or use a datastructure (ds_ functions).
Though it is possible we will support getting length for GM arrays. We could make it work because the arrays in GM are dynamic and they are wrapped in some other class even now. So it should be possible to make it work. But if you use C++ style arrays, then you won't have that possibility. For example in ENIGMA you can write (or at least "should write", as I am pretty sure this is broken now):
int array[10];
array[0] = 5;
This is a pure C++ array and as they are static size at compile time, you cannot get length directly. You could write something like sizeof(array) and then figure it out from that, but usually it's meant that the user already knows the size.
Pages: 1 2