Resuming a function when message is resolved

Let’s say I got this setup:

main.collection
|potato.go
||potato.script
|hungry.go
||hungry.script

Now, in update in hungry.script I need to run a function in potato.script and then use its return value in hungry.script.

The ideas I have so far are:
Sending a callback in the message to potato.script. This will quickly end me up in callback hell though and I would prefer not using it, and it also makes things really weird because the callback would be run with the environment (hope that’s the proper term) for potato.script when the code actually is in hungry.script
Putting the function I want to run in hungry.script within a coroutine, sending the message to potato.script and putting the coroutine in the message and then yielding. Then when potato.script has called its function it would resume the coroutine and somehow shove the values back into it. Not sure this would work either.

What is the best way to do this?

Have you considered using Lua modules?

What you want to do would be super easy to implement.


edit: at least if the objective is to share values between scripts

1 Like

I did actually have that as an idea but killed it due to the same issues as with callbacks. It’s probably the way to go though. The idea now having a table in a module and then adding a function from potato.script and using it’s id as the key and that makes it so I can treat it in a very similar way that I would with messages. It’s probably the cleanest approach.
Thanks a ton, really appreciate it!

1 Like

What is cool with Lua modules is that you can easily and instantly share advanced/complex stuff (with many scripts at the same time): not only numbers and strings, but also tables, userdata (url etc.), object positions, etc.

You need to keep your code clean and “under control” but for me (maybe others would disagree), when it’s about sharing values, I find them much more convenient than messages (even though I still use messages on specific occasions).

It’s really been a game changer for my current project and building it without Lua modules would have been a real pain in the ass.

Anyway, glad it helped :nerd_face: Hope you’ll make the most of these modules.

1 Like

Once again, thanks a lot! I’ll be using them for common data from now on, just from quickly testing it it is very practical :smiley:

3 Likes