I'm using LuaInterface to spawn bullet patterns for a bullet hell game I'm creating.
However, if I have a "while(true)" loop in the LUA script, the whole program freezes.
I want to get the LUA script to run in a separate thread, but I can't figure out how to do it.
Can you help me?
I don't know if you do have it, but you need a function to update graphics or the application itself.
Since you're using LuaInterface, I'll assume you're using .NET
Application.DoEvents(); will run events throughout the entire application. So you can basically still use the application, even in a loop.
Or if you're using some graphics engine (SDL, OpenGL, etc), then you would use the function to upgrade graphics.
For gaming purposes, I say you should use both.
I'm not sure if you can get Lua to run in a program with two threads. But the above should emulate it decently.
I'm using SFML. I don't understand what you mean.
I have a "update" method and a "draw" method, but where does the second thread needs to go?
I would have a Lua function that does the "Update()" function.
This function would go in the while(true) lua loop. (most likely at the end of the loop)
I haven't used SFML, but looks pretty neat.
Looking at this: [url]http://www.sfml-dev.org/documentation/1.6/[/url] I see that App.Display(); would update the graphics and application window.
But I don't want my LUA scripts to call the "Update()" functions, I want them to create new bullets in different positions, with different speeds, etc.
It should run "parallel" to the existing "Update()" script but it needs to be capable of waiting a certain amount of frames and repeat itself without affecting the real program.
That's not a very good way to do it. The answer is don't block in your script.
[QUOTE=blankthemuffin;21216205]That's not a very good way to do it. The answer is don't block in your script.[/QUOTE]
I need to have yield; and wait(n); functions to create delayed bullet patterns.
[QUOTE=SupahVee;21230087]I need to have yield; and wait(n); functions to create delayed bullet patterns.[/QUOTE]
You don't have to yield; you can call a Lua function on every frame you want Lua to be able to do something and do the timing in there instead. Or you can use a coroutine and yield out of 'wait' to something that returns control to your script after a set amount of time.
[QUOTE=jA_cOp;21230162]You don't have to yield; you can call a Lua function on every frame you want Lua to be able to do something and do the timing in there instead.[B] Or you can use a coroutine and yield out of 'wait' to something that returns control to your script after a set amount of time.[/B][/QUOTE]
Sorry if I'm too demanding, but can you show me an example? I've never worked with coroutines.
[QUOTE=SupahVee;21230497]Sorry if I'm too demanding, but can you show me an example? I've never worked with coroutines.[/QUOTE]
Lua to illustrate:
[lua]
function wait(time)
coroutine.yield(time)
end
function update()
CreateBullet(...)
wait(1.5)
CreateBullet(...)
end
local thread
function init()
thread = coroutine.create(function() while true do update() end)
end
function resume()
local waitTime = thread:resume()
return waitTime
end
[/lua]
Naturally, you don't have to do the coroutine creation and resuming from Lua. Resume returns when 'wait' is called, returning the time to wait. Once that time has passed, resume expects to be called again as soon as possible (for as accurate waiting as possible).
So if I design scripts like the one you posted, I just need to do Lua.DoFile(path here) and it will work correctly?
[QUOTE=SupahVee;21230973]So if I design scripts like the one you posted, I just need to do Lua.DoFile(path here) and it will work correctly?[/QUOTE]
Only the "update" function in my example should be in your bullet scripts. The rest is how to set up coroutines for a wait function. No, you can't just run Lua.DoFile on the script... if you don't understand it you should read up on it, as you probably won't be able to use it correctly without understanding coroutines (they're not like pre-emptive threads).
And where do I put the init, resume and wait functions then?
Anyway, you're right, I should read something about it. Any suggestion? Or first google result?
[QUOTE=SupahVee;21231690]And where do I put the init, resume and wait functions then?
Anyway, you're right, I should read something about it. Any suggestion? Or first google result?[/QUOTE]
The Lua manual does have good docs on coroutines, but it only contains a single example as far as I can remember. Googling might find you some more examples if you need them. If you're still struggling after reading the manual on coroutines and couldn't find anything on google, feel free to ask and I'll do my best explaining it (I know it took me a while to understand them and how they were useful).
If you're going to go that far just write your main loop in Lua lol.
[QUOTE=Jawalt;21233558]If you're going to go that far just write your main loop in Lua lol.[/QUOTE]
Well if you saw my snippet, you'd see that it is quite simple and you'd still need to use coroutines if you wanted to create bullet patterns with pauses without blocking everything else. You could also use something like how Think works in Garry's Mod, but it's harder to make pauses and needs a lot more Lua code which kind of defeats the point of using Lua for scripting bullet patterns.
Sorry, you need to Log In to post a reply to this thread.