Create Timer in C# which calls function in LUA
See original GitHub issueNeoLua Version: 1.3.0
I’m trying to add a timer to LUA which calls a function in the LUA script. So far everything works except that I dont know how to call the LUA function in my c# timer class.
The following example raises an exception on OnTimer?.Invoke(this) saying “‘Nil’ cannot be called” OnTimer value on runtime is {Method = {Neo.IronLua.LuaResult Tick(System.Runtime.CompilerServices.Closure, System.Object)}}
Example to reproduce:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
using (var lua = new Neo.IronLua.Lua())
{
dynamic dg = lua.CreateEnvironment();
dg.createTimer = new Func<CustomTimer>(Awesome.createTimer);
dg.dochunk(@"local timer = nil
local ticks = 0
local function Tick(timer)
ticks = ticks + 1
printf(ticks)
end
timer = createTimer()
timer.Interval = 100
timer.OnTimer = Tick
timer.Enabled = true");
}
}
}
public class CustomTimer
{
public CustomTimer()
{
timer.Elapsed += Timer_Elapsed;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
//how to raise OnTimer?
OnTimer?.Invoke(this);
}
private Timer timer = new Timer();
public double Interval
{
get => timer.Interval;
set => timer.Interval = value;
}
public Func<object, object> OnTimer;
public bool Enabled
{
get => timer.Enabled;
set => timer.Enabled = value;
}
}
public static class Awesome
{
public static CustomTimer createTimer()
{
return new CustomTimer();
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Creating Timers in C++ using Lua
Creating Timers in C++ using Lua ; // this function is called in the game-loop to loop through all timers in the vector...
Read more >How do you make a timer on LUA?
The example below uses LuaSocket's gettime() function as wallclock. ... a some C glue to clock_nanosleep and pass the TIMER_ABSTIME flag.
Read more >RE: Timers, Callbacks & Swig
I am trying to add to my embedded App. the ability for Lua to have up to say 20 timers the user can...
Read more >Trying to make a timer that repeats. Really really confused ...
I have a button that starts the process by calling the updateTime function. Three variables are passed to the updateTime function: secondsLeft, ...
Read more >26 – Calling C from Lua
The C function gets its arguments from the stack and pushes the results on the stack. To distinguish the results from other values...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You declared the timer variable local, so the scope is only the first script. You need to set it to a global variable.
and run on the same environment
dg
.Thanks again for your help!