Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

Is there a way to enter dynamically data into a lua script using C#?

I am trying to implement the following logic using C#/Lua:

Inside my C# application, I have a form with a textbox that allows me to enter a signal value and pass it then to the Lua script via (state["timeToCollection"] = signalValue.Text;). The idea is that when this value is equal to "low", some other calls will happen.

The issue that I am facing currently is that this signalValue.Text needs to be filled out before executing the Lua script and not after executing it (desired behavior). Is there a way to do this dynamically, meaning, I execute the Lua script, Lua script checks for signalValue.Text value, if the value is not low, it just keeps running until the user enters low in the form.

Thanks

C#

state.DoFile(luaScriptFile); //execute lua script

state["timeToCollection"] = signalValue.Text; // pass signal value

Lua Script:

function DM()
     while (timeToCollision == 'low') 
     do
        UDC:eventImageDataReq('AutoHitch',01)
     end
        
end

DM()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have the Lua interpreter, so to set a variable simply set it:

state.DoString($"timeToCollision = '{signalValue.Text}'");

You didn't mention what flavor of C# Lua bindings you're using, but for example MoonSharp, since it's a fully managed re-implementation of Lua, exposes the variable tables directly, so you can use something like state.Globals["timeToCollision"] to set your values directly, without having to parse and execute Lua code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...