question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create Timer in C# which calls function in LUA

See original GitHub issue

NeoLua 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
neolithoscommented, Sep 29, 2018

You declared the timer variable local, so the scope is only the first script. You need to set it to a global variable.

timer = createTimer()

and run on the same environment dg.

timer.Enabled = false;
0reactions
Symbaicommented, Sep 29, 2018

Thanks again for your help!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found