[RESOLVED] Does AGS have something similar to coroutines

Started by Joacim Andersson, Thu 23/01/2025 09:47:18

Previous topic - Next topic

Joacim Andersson

I want under certain conditions to show a GUI when an inventory item is used on a hotspot, character, or object in the scene. In most cases this inventory item can't be used on that hotspot (object or character) but in any case, I want a GUI to be shown, but then my function code should pause until the GUI is closed.

So my question is: Does AGS have something like coroutines or something else I can use to yield the execution of a function?

I'm using version 3.6.1

Crimson Wizard

No, and currently engine does not process player input on GUI while there's a waiting script.
The only way is to
- Show GUI
- Finish the current script
- Let player interact with gui
- Detect when GUI is closed and run the continuation of a script, either from GUI's event, or from rep-exec which tests for gui visibility.

Joacim Andersson

That it doesn't process any input on a GUI during wait, is the first thing I noticed, since I tried something like this:
Code: ags
gMyGui.Visible = true;
while (gMyGui.Visible)
  Wait(1); 
But then I, unfortunately, couldn't use the GUI. So I guess I have to rethink my approach and develop another solution. But thank you anyway for your reply.

Danvzare

Quote from: Joacim Andersson on Thu 23/01/2025 11:23:40That it doesn't process any input on a GUI during wait, is the first thing I noticed, since I tried something like this:
Code: ags
gMyGui.Visible = true;
while (gMyGui.Visible)
  Wait(1); 
But then I, unfortunately, couldn't use the GUI. So I guess I have to rethink my approach and develop another solution. But thank you anyway for your reply.
There are ways to work around it, or "fake" it. Although they are difficult to implement and quite error prone if you haven't considered every contingency.
As such, it usually is better to just rethink your approach.

But if you really wanted to keep to the same idea. The way I'd try and tackle it, would be to store the necessary variables just before showing the GUI and then essentially ending the interaction. Then when you press to close that GUI, it takes those variables and sends them to a function that essentially finishes off the interaction that had started. I think that would accomplish what you described.
I'm not sure how you'd generic-ify it though. So it'd probably quite a lot of bespoke programming.  ???

Joacim Andersson

#4
Quote from: Danvzare on Thu 23/01/2025 12:38:48But if you really wanted to keep to the same idea. The way I'd try and tackle it, would be to store the necessary variables just before showing the GUI and then essentially ending the interaction. Then when you press to close that GUI, it takes those variables and sends them to a function that essentially finishes off the interaction that had started.
Yes doing something similar to this was my first attempt, however, to do this I have to keep track of what the player clicked on and since AGS doesn't have a generic type (like in C# or Java or anything like C++ Template),  I  can't, in one variable, store a hotspot, object, or character. So I dropped that approach.

If AGS at least had something like System.Object or a Variant data type, this would have been easier.

eri0o

You can use multiple variables and a type or an int for ID and the type.

In my ArrowSelect module I did something similar with interactives

https://github.com/ericoporto/arrowselect/blob/32625e45aea809e9e9e8acbefed37225c8c01465/arrowselect_demo/arrowselect.ash#L72

Joacim Andersson

Quote from: eri0o on Thu 23/01/2025 15:18:34You can use multiple variables and a type or an int for ID and the type.

In my ArrowSelect module I did something similar with interactives

https://github.com/ericoporto/arrowselect/blob/32625e45aea809e9e9e8acbefed37225c8c01465/arrowselect_demo/arrowselect.ash#L72
Yes, using a custom enum to remember the type is what I'm now doing (or started to do after the first reply in this thread). Thank you.

Crimson Wizard

Speaking of coroutines, by coincidence, I've got a working test with coroutines, for AGS 4 version branch:
https://www.adventuregamestudio.co.uk/forums/editor-development/feature-request-behavior-of-hotspot-that-results-in-a-room-change/msg636668451/#msg636668451

That's only a proof of concept, not a complete feature.

SMF spam blocked by CleanTalk