Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does setTimeout and setInterval can't be transpiled to Lua? #1467

Open
ProgrammingLife opened this issue Jul 10, 2023 · 6 comments
Open
Labels

Comments

@ProgrammingLife
Copy link

I've just noticed that setTimeout and setInterval can't be transpiled to Lua with TypescriptToLua but there are some solutions for this in Lua such as timers (wait), coroutines, etc... then why it can't transpile setTimeout and setInterval functions?

@lolleko
Copy link
Member

lolleko commented Jul 10, 2023

I think the reason is, that until now, no one asked for it. Most applications/games offer a custom API that can achieve similar behavior.
Anyway, It would be nice to have support for setTimeout and setInterval via LuaLib.
I am not 100% certain the exact same behavior can be accomplished with coroutines, but it is worth it to give it a try.

@Perryvw
Copy link
Member

Perryvw commented Jul 10, 2023

Generally you need two things to implement a timer:

  • Something that triggers the timer update
  • A way to get the current time

I am not aware of a way to get these with just bare Lua. Generally every Lua target environment I have seen has its own way of doing these things, hence we cannot just provide a generic implementation. Even if we had an implementation, we would probably still need you to somehow provide some code to do these two things. At that point it is probably simpler to just use the environment-specific timing capabilities with some declarations.

I am not against providing a lualib polyfill for these, but it needs to work for all (or most of) our targets, otherwise we are better off just deferring this to the user. I'm open to suggestions for ways to do this.

@lolleko
Copy link
Member

lolleko commented Jul 10, 2023

Just checked, there is indeed no good way to get the current time. Luas os.time() returns time in seconds only. On top of that it is system dependent und inaccurate.

So I don't think adding support for setInterval and setTimeout will be possible.

@Cheatoid
Copy link
Contributor

Cheatoid commented Oct 4, 2023

One way would be to go full abstract/agnostic, so no matter what Lua environment is being used, a programmer would provide these requirements/arguments to the lualib. Expose a method to set it up before being able to use it?
Then the rest simply becomes just a common Timer implementation.

@Z3rio
Copy link
Contributor

Z3rio commented Oct 4, 2023

Yeah, some way of integrating this would be great, personally, I'm running a Lua runtime that does support this, which makes it a bit annoying that I cant use it.

@Perryvw
Copy link
Member

Perryvw commented Oct 4, 2023

If you have to provide some binding code to make setTimeout work with your own timers, what is the benefit over just using that timer declaration directly?

In fact you could just add your own setTimeout implementation to your environment to be used by the default declarations:

function setTimeout(handler: () => void, timeout: number) {
    // code calling your environment timer here
}

(you will need to make sure this lua file is loaded as part of the global scope ofc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants