Lua unit scripting - Page 5

Lua unit scripting

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Master-Athmos
Posts: 916
Joined: 27 Jun 2009, 01:32

Re: Lua unit scripting

Post by Master-Athmos »

Yeah - it was that. Damn you case sensitive spelling! :P
Of course fixing that immediately spawns the next problem: It pretty much seems to abort at the same spot saying...

Code: Select all

 LuaRules::RunCallIn: error = 2, CLuaUnitScript::Deactivate, not in a thread
So what can be done about that?
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Lua unit scripting

Post by aegis »

it's being called from the main thread, which apparently isn't right

edit: tobi answered
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: Lua unit scripting

Post by Tobi »

Use StartThread to start a new thread, and run stuff that needs to wait/sleep there.
Master-Athmos
Posts: 916
Joined: 27 Jun 2009, 01:32

Re: Lua unit scripting

Post by Master-Athmos »

Yeah, when putting the commands in an own function and calling it in Deactivate() it works. So every time I want to do anything with WaitForxxxx inside engine related functions I need to open up a new thread?
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Lua unit scripting

Post by aegis »

yes. the WaitForMove function has a check to make sure you're in a thread
Master-Athmos
Posts: 916
Joined: 27 Jun 2009, 01:32

Re: Lua unit scripting

Post by Master-Athmos »

But this is just about waits and sleeps or are there other commands one has to outsource in a seperate thread?
User avatar
aegis
Posts: 2456
Joined: 11 Jul 2007, 17:47

Re: Lua unit scripting

Post by aegis »

you can't wait/sleep in the main thread because it would block spring from running
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: Lua unit scripting

Post by Tobi »

Example:

Usually there is no need for a thread when Spring calls this function, so it runs from main thread.

Code: Select all

function script.QueryWeapon()
  return somepiece
end
Now consider you want to add lots of animation code to it anyway:

Code: Select all

function script.QueryWeapon()
  Turn(wheel, x_axis, 18, 1.5)
  WaitForTurn(wheel, x_axis)
  Move(base, y_axis, 42, 0.1)
  WaitForMove(base, y_axis)
  -- etc.
  return somepiece
end
Then you'll get the error you get in the first WaitFor/Sleep function.

For now, rewrite as:

Code: Select all

local function MyAnim()
  Turn(wheel, x_axis, 18, 1.5)
  WaitForTurn(wheel, x_axis)
  Move(base, y_axis, 42, 0.1)
  WaitForMove(base, y_axis)
  -- etc.
end
function script.QueryWeapon()
  StartThread(MyAnim)
  return somepiece
end
Post Reply

Return to “Engine”