Page 1 of 1

lua unit script questions.

Posted: 17 Mar 2011, 20:42
by smoth
*edits* turning thread into a general questions thread so I don't flood forum with stupid questions.








StartThread = start-script

(what is) = call-script?

What do I need to use for this?

Re: lua unit script question.

Posted: 17 Mar 2011, 20:51
by FLOZi
You just call a function in the normal lua way:

Code: Select all

local function DoSmoth(smothValue)
  smothCount = smothCount + smothValue
end

function script.Create()
  DoSmoth(9001)
end
And it will be executed immediately before returning to the function/thread that called it. (whereas of course StartThread(MyFunction, {arguments}) is run in parallel (well sort of))

Re: lua unit script question.

Posted: 17 Mar 2011, 21:16
by smoth
lol duh silly me. Thanks!

Re: lua unit script questions.

Posted: 18 Mar 2011, 21:01
by smoth
Ok, so I sat down and began converting my unit reading how the corcom in ca was coded. I am usure why but my unit aims yet never fires. Any ideas? he aims no problem but just won't fire, I cannot see what I am missing.

Code: Select all

-------------------------------------------------------
-- License:	Public Domain
-- Author:	Steve (Smoth) Smith
-- Date:	3/17/2011
-------------------------------------------------------

-- Piece names
local head		= piece 'head'
local base		= piece 'base'
local left		= piece 'left'
local right		= piece 'right'
local l_arm		= piece 'l_arm'
local l_forearm	= piece 'l_forearm'
local r_arm		= piece 'r_arm'
local r_forearm	= piece 'r_forearm'
local gun		= piece 'gun'
local fire		= piece 'fire'
local sheild	= piece 'sheild'
local cod		= piece 'cod'
local right_l	= piece 'right_l'
local left_l	= piece 'left_l'
local shin_l	= piece 'shin_l'
local shin_r	= piece 'shin_r'
local foot_r	= piece 'foot_r'
local foot_l	= piece 'foot_l'

-- State variables
local  ISMOVING, ISAIMING, speedMult = false, false, 2.4

-- Signal definitions
local SIG_AIM			=	2

-- effects for emitters
local yellowmuzzelflash = 1024

--local SMOKEPIECE1 = base

--#include "\headers\smoke.h"
--#include "\headers\walk.h"

function script.StartMoving()
	ISMOVING = true
end

function script.StopMoving()
	ISMOVING = false
end	
--------------------------------------------------------
--start ups :)
--------------------------------------------------------
function script.Create()
	-- Initial State
	
	Turn(r_forearm, x_axis, math.rad(-15),math.rad(130))
	Turn(fire, x_axis,math.rad(90))
				
	LEFTARM		=	TRUE;
	RIGHTARM	=	TRUE;
	HEAVY		=	TRUE;
			

	--StartThread(MotionControl)	
	--StartThread(SmokeUnit)
end

function script.AimFromWeapon(num)
	return head
end

function script.QueryWeapon(num)
	return fire
end
	
-----------------------------------------------------------------------
--gun functions;
-----------------------------------------------------------------------	
local function RestoreAfterDelay()
	Sleep(1000)
	
	Turn(base, y_axis, 0, math.rad(135))
	Turn(r_forearm, x_axis, math.rad(-15), math.rad(130))
	Turn(right, x_axis, 0, math.rad(130))
		
	ISAIMING = false
end

function script.AimWeapon(num, heading, pitch)
	Signal(SIG_AIM)
	SetSignalMask(SIG_AIM)
	
	ISAIMING = true
	
	--FixArms(0,1)
	
	Turn(base, y_axis, heading, math.rad(105))
	Turn(r_forearm, x_axis, math.rad(-45), math.rad(390))
	Turn(right,	x_axis, math.rad(-45) - pitch, math.rad(390))
			
	WaitForTurn(base, y_axis)
	WaitForTurn(right, x_axis)
	WaitForTurn(r_forearm, x_axis)
				
	RestoreAfterDelay()
	return true
end
	
function script.FireWeapon(num) 
	Move(right, z_axis, math.rad(-0.4))
	Turn(r_arm, x_axis, math.rad(-5))
		
	EmitSfx(fire, yellowmuzzelflash) --first shot is actual weapon
	Sleep(25)
		
	Turn(r_arm, x_axis, 0, math.rad(0.5))
	Move(right, z_axis, 0, math.rad(5))	
		
	Sleep(75)
		
	Move(right, z_axis, math.rad(0.4))
	Turn(r_arm, x_axis, math.rad(-5))
		
	EmitSfx(fire, yellowmuzzelflash)
	EmitSfx(fire, 2048)
	Sleep(25)
		
	Turn(r_arm, x_axis, 0, math.rad(0.5))
	Move(right, z_axis, 0, math.rad(5))			
			
	Sleep(75)
		
	Move(right, z_axis, math.rad(0.4))
	Turn(r_arm, x_axis, math.rad(-5))
		
	EmitSfx(fire, yellowmuzzelflash) 
	EmitSfx(fire, 2048)
	Sleep(25)
		
	Turn(r_arm, x_axis, 0, math.rad(0.5))
	Move(right, z_axis, 0, math.rad(5))	
			
	Sleep(500)
end
	
function script.Killed(recentDamage, maxHealth)
	local severity = recentDamage/maxHealth
	if (severity <= 99) then
		Explode(left, FALL)
		Explode(right, FALL)
		Explode(l_arm, FALL)
		Explode(l_forearm, FALL)
		Explode(r_arm, FALL)
		Explode(r_forearm, FALL)
		Explode(gun, FALL)
		return 3
	else
		return 0
	end
end
P.S. please don't bitch about code alignment, shit reads fine in notepad++ which is all I care about as that is my tool of choice.

Re: lua unit script questions.

Posted: 18 Mar 2011, 21:36
by smoth
function script.AimWeapon(num, heading, pitch)
Signal(SIG_AIM)
SetSignalMask(SIG_AIM)

ISAIMING = true

--FixArms(0,1)

Turn(base, y_axis, heading, math.rad(105))
Turn(r_forearm, x_axis, math.rad(-45), math.rad(390))
Turn(right, x_axis, math.rad(-45) - pitch, math.rad(390))

WaitForTurn(base, y_axis)
WaitForTurn(right, x_axis)
WaitForTurn(r_forearm, x_axis)

RestoreAfterDelay()
return true
end
lol found it.

Re: lua unit script questions.

Posted: 18 Mar 2011, 21:50
by smoth

Code: Select all

local function RestoreAfterDelay()
	Spring.Echo("restore")
	Sleep(1000)
============stuff====================
end

function script.AimWeapon(weaponID, heading, pitch)
	Spring.Echo("aim")
============stuff====================
	StartThread(RestoreAfterDelay)
end
	
function script.FireWeapon(weaponID) 
	Spring.Echo("ShoOT")
============stuff====================
end
removed extra animation crap, replaced with "============stuff====================" in this psuedo code.

so with the way we currently do firing/aiming/restordelay...

we get

aim
restore
ShoOT

So the weapon starts is restore after delay right after aiming. Kinda feels odd that EVERY TIME we aim we fire off the restore after delay thread, which then gets overridden by the next aim call. something like:

aim
restore
ShoOT
aim
restore
ShoOT
aim
restore
ShoOT

so we have a thread that is running for a sleep 1000 right? rong-o. The thread is restarted with each call to restoreafter delay. I put an echo at the end of restoreafterdelay to it is just starting an assload of threads and found out it doesn't

Code: Select all

local function RestoreAfterDelay()
	Spring.Echo("restore")
	Sleep(1000)
	
============stuff====================
	Spring.Echo("restore end")
end
this is what I got:

aim
restore
ShoOT
aim
restore
restore end

so it is restarting the thread each time it is called.

Is this operation expensive? why not just have restoreafterdelay as part of the already existing motion control loop which is standard in almost all scripts?

Re: lua unit script questions.

Posted: 18 Mar 2011, 23:52
by knorke
Is this operation expensive?
so far never noticed slowdowns with threads, even running multiple threads or quickly starting/stopping.

Re: lua unit script questions.

Posted: 19 Mar 2011, 05:32
by smoth
turn <degrees> speed <somenumbera>
turn <degrees> speed [somenumberb]

somenumbera converts fine in math.rad

what does the somenumberb actually represent?

Re: lua unit script questions.

Posted: 19 Mar 2011, 08:31
by smoth
<angular>
[linear]


that answer found.

now to my last connundrum

Code: Select all

function script.FireWeapon(weaponID) 

	Move(right, z_axis,  -0.8)
	Turn(r_arm, x_axis, -0.15)
	
	EmitSfx(fire, 1024) -- flare ceg
	--first shot is actual weapon
	Sleep(25)
		
	Turn(r_arm, x_axis, 0, 10)
	Move(right, z_axis, 0, 0, 10)	
		
	Sleep(75)
		
	Move(right, z_axis, -0.8)
	Turn(r_arm, x_axis, -0.15)

	EmitSfx(fire, 1024) -- flare ceg doesn't fire
	EmitSfx(fire, 2048) -- weapon does fire
	Sleep(25)
		
	Turn(r_arm, x_axis, 0, 10)
	Move(right, z_axis, 0, 0, 10)			
			
	Sleep(75)
		
	Move(right, z_axis,  -0.8)
	Turn(r_arm, x_axis, -0.15)

	EmitSfx(fire, 1024)  -- flare ceg doesn't fire
	EmitSfx(fire, 2048)  -- weapon does fire
	Sleep(25)
		
	Turn(r_arm, x_axis, 0, 100)
	Move(right, z_axis, 0, 0,  10)	
			
	Sleep(500)
end
anyone have a guess why the ceg only fires 1 time? I don't know and I am rather puzzled by it.

Re: lua unit script questions.

Posted: 19 Mar 2011, 16:53
by FLOZi
you only sleep for 100 milliseconds in between. 1/10th a second. Probably a shorter period of time than the duration of the CEG itself?

Re: lua unit script questions.

Posted: 20 Mar 2011, 19:10
by smoth
Apparently there was some kind of bug that was killing a ceg which had a ttl of 1 after sleep. It was a bug, it is getting addressed, I think. Solution, use a ttl of 2.