Author Topic: Is there any way to do a "for" loop in AGI?  (Read 2168 times)

0 Members and 1 Guest are viewing this topic.

Offline AGKorson

Is there any way to do a "for" loop in AGI?
« on: January 28, 2024, 06:07:40 PM »
I was recently asked this question in a separate conversation. AGI is a very primitive language that does not include any flow control statements except 'if-else' and 'goto'.

But you can easily write code to duplicate the 'for' loop structure.
A standard 'for' loop in more complex languages looks like this:
Code: [Select]
for (<init-expression>; <condition-expression>; <update-expression>)
  {
  [ statements to repeat in each loop
  }


Here is an AGI template that will do the same:
Code: [Select]
<init-expression>;
:forloop1
if (<condition-expression>) {
  [ statements to repeat in each loop
  <update-expression>;
  goto(forloop1);
}


Here's an actual example to see it in action:
Code: [Select]
v99 = 10;
:forloop1
if (v99 > 0) {
  [ statements to repeat in each loop
  print("Countdown: %v99");
  --v99;
  goto(forloop1);
}

WinAGI includes a snippet that handles creating the 'for' loop template. All you have to do is type
Code: [Select]
#for(v99 = 10, v99 > 0, --v99)#

and the code block is automatically inserted!

Snippets are a powerful tool in WinAGI that can help with a lot of other coding tasks. Check out the WinAGI Help File for more information, or post a question here, and I'd be happy to help.

« Last Edit: January 29, 2024, 02:40:29 PM by AGKorson »



Offline mnicolella

Re: Is there any way to do a "for" loop in AGI?
« Reply #1 on: January 29, 2024, 01:14:33 PM »
Looks good although in most languages the body won't execute at all if the condition fails initially, so you probably want to move the "statements to repeat in each loop" to be inside the condition

Otherwise for things like  for(i = 0; i < count; i++), if the count is 0 your template still executes the body once

Offline AGKorson

Re: Is there any way to do a "for" loop in AGI?
« Reply #2 on: January 29, 2024, 02:38:20 PM »
Good catch! I revised my post to reflect that. The next version of WinAGI will include the updated snippet text. In the meantime, it's super easy to update any snippet (or add new ones) by using the Code Snippets tool.

Offline lance.ewing

Re: Is there any way to do a "for" loop in AGI?
« Reply #3 on: January 29, 2024, 06:37:24 PM »
This touches on something that I've been thinking about for a while. Perhaps we should split it out into a separate topic, but what I've been wondering is whether it is possible to have an AGI game that runs within a single AGI cycle, i.e. it never leaves the first execution of the very first AGI cycle. This is theoretically possible with the existence of the goto statement and loops within LOGIC scripts. I can't think of any reason why it wouldn't work. There isn't any need as such for the interpreter to leave the single AGI cycle, other than to give the cpu some time off, and to control animation speed, which are both important things, but perhaps a certain type of non-adventure game could be written within a single AGI cycle. It wouldn't trigger any more executions of main part of the AGI cycle while that first one is still running. You could, for example, implement a "game loop" within this single executing cycle. It wouldn't have any pause time between iterations of the game loop though, and would be consuming CPU time constantly I assume. It is an interesting theoretical question though, and would be an interesting test case to test against any fan made AGI interpreters.

Offline AGKorson

Re: Is there any way to do a "for" loop in AGI?
« Reply #4 on: January 29, 2024, 11:50:28 PM »
Running a custom cycle within a single logic is definitely very doable. I used that technique in the Power Pack Demo game, to show the walkman. It's very useful when you need to interrupt the main cycle, for example to show a cutscene or other animation, but don't want to use a new.room command because of other considerations (usually to temporarily avoid cycling of the main story's animation).

The challenge is that you have to manage all the animation manually. Basically, you are replacing all the overhead activities that the interpreter does with equivalent manual code.

As a proof of concept, you could certainly create a game that never leaves logic 0, but I don't think there are many scenarios where that would be preferable to letting AGI do the heavy lifting between cycles. That's kind of the whole point of the interpreter.

I'd be happy to continue the discussion in another topic if there's further interest.
« Last Edit: January 30, 2024, 12:12:16 AM by AGKorson »

Offline doomlazer

Re: Is there any way to do a "for" loop in AGI?
« Reply #5 on: January 30, 2024, 01:11:49 AM »
What is the AGI "hello world"?

Offline AGKorson

Re: Is there any way to do a "for" loop in AGI?
« Reply #6 on: January 30, 2024, 09:53:25 AM »
What is the AGI "hello world"?

At its simplest: in logic 0:
Code: [Select]
print("Hello world");
quit(1);
return();

to feel more 'AGI-like', in logic 0:
Code: [Select]
if (v0 == 0) {
    new.room(1);
}
return();
in logic 1:
Code: [Select]
if (isset(f5)) {
    display(12, 33, "Hello world");
}

if (have.key()) {
    quit(1);
}
return();
« Last Edit: January 30, 2024, 11:40:37 PM by AGKorson »


SMF 2.0.19 | SMF © 2021, Simple Machines
Simple Audio Video Embedder

Page created in 0.173 seconds with 23 queries.