Jump to content

  • Log In with Google      Sign In   
  • Create Account

Stoping the execution of lua script in LuaInterface


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Razov   Members   -  Reputation: 111

Like
0Likes
Like

Posted 07 September 2012 - 01:57 PM

Hello,

lets say that i have got such code:

String script = @"
writeConsole("Starting.")
while true do
  writeConsole("I am alive...")
  sleep(1000)
end";
Lua lua = new Lua();
lua.DoString(script);

witch after executed in new thread keeps writing "I am alive..." evry second, like:


Starting.
I am alive...
I am alive...
I am alive...
.....

Is there any way to stop executing this loop (or any other lua script witch is in middle of execution at given point) from other thread ? And to reload execution of this script, so it will give output like this:

Starting.
I am alive...
I am alive...
I am alive...
Starting. // stop execution and start again in this place
I am alive...
I am alive...


Sponsor:

#2 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 08 September 2012 - 04:58 AM

I don't know which host language you're using (EDIT: ah C#, but which bindings library?), but the C API certainly gives you fine-grained control over how the Lua code is executed. So without using any additional threads, you call tell the interpreter to run a little bit of code at a time and check your restart/stopping condition in between.

From inside Lua itself, you can use coroutines.

However, if you were to explain the reasons for wanting to do this, I suspect that there might be a better approach to achieving your end goal.

Edited by e‍dd, 08 September 2012 - 05:51 AM.


#3 Ashaman73   Crossbones+   -  Reputation: 7992

Like
0Likes
Like

Posted 10 September 2012 - 06:13 AM

You can use a state variable which can be either modified in your own lua script or in your C# engine, something like this:

MyStateVar = "stopped"
..
MyStateVar = "running"
while MyStateVar=="running" do
  .. game logic ...
  if ...escape key pressed... then
	MyStateVar = "stopped"
  end
end
..

Edited by Ashaman73, 10 September 2012 - 06:13 AM.


#4 Razov   Members   -  Reputation: 111

Like
0Likes
Like

Posted 17 September 2012 - 12:59 PM

Thats not the point.

I am using LuaInterface for C#.

I am creating an application, where user can write his own lua code and execute it (new thread). I want to have possiblity to stop (interput) the execution of this lua code from C#. Lets see an example:

Lets say there is such application:

Posted Image

When You change "Enabled" CheckBox Checked to true, lua executes onEnabled function (this part is already done.), witch starts loop
When You change "Enabled" CheckBox Checked to false, execution of lua (there is loop running) is terminated/stoped, but lua variables stays untoutched (not cleaned)
When You press "RELOAD" button, execution of lua is stopped (if its running), then lua enverionment is cleared, and lua code from textbox is executed, so user can make changes to it and press reload.

Example output it would give:

["Enabled" Checked=true]
eC: 1 / wC: 1
eC: 1 / wC: 2
eC: 1 / wC: 3
["Enabled" Checked=false]
[time passes, nothing appears on log...]
["Enabled" Checked=true]
eC: 2 / wC: 4
eC: 2 / wC: 5
[RELOAD pressed]
eC: 1 / wC: 1
eC: 1 / wC: 2
...

The main problem i am facing right now is how i can stop execution of running lua code from c#?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS