Page 1 of 1
Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 06:22
by SanadaUjiosan
Quick question: Is there a gadget out there that will affect how long a unit exists in the game? What I'm wanting is something to make a specific unit (a camera-tower type thing) only last for a specific duration of time (let's say 30 seconds.)
So like a unit would be able to build this camera tower, and the camera tower would sit there, being all camera-y, but when it's 30 seconds was up, it'd detonate and go away.
I'm guessing this is an easy thing to pull off, but I'm wondering if anyone's written a gadget for it.
And no, I am not going to do it. I'm not a programmer. It is either going to have to already exist, or a non-existent nice person write it for me.
Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 07:48
by MidKnight
What if I make it really easy for you?
UnitCreated()
Tells you when a unit is built. gives you:
"unitID, unitDefID, teamID, builderID"
Spring.GetGameFrame()
Will get you the number of sim frames since game start.
GameFrame()
Is called every time there is a new frame. Spring has a fixed rate of 30 sim frames per second (IIRC), so 30 seconds would be 900 frames.
Spring.DestroyUnit(UnitID, selfd, reclaimed)
Destroys the unit with the given UnitID.
selfd can be set to true or false. true makes it explode.
reclaimed can be set to true or false. true makes it just disappear off the face of the map.
I'd write the script for you, but it's 2 AM right now and everything's swirly. Sorry.

Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 08:05
by SanadaUjiosan
If you'd like to write it when it's not 2am, it would be greatly appreciated. Maybe this super simple script could help me start to even fathom how gadgets work. I do well with examples. It took me like a month or two of staring at a simple animation script to understand how a weapon fires.
Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 09:24
by KDR_11k
Also in the unit script you can just add sleep 30000; get KILL_UNIT();.
Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 14:04
by Google_Frog
It is a really easy gadget but since you said you do not want to do any programming at all...
Code: Select all
local KILL_OFFSET = 900 -- 30 seconds
local cameraUDID = UnitDefNames["camera"].id
local killTable = {}
local killTableByUnitID = {}
function gadget:UnitCreated(unitID,udid)
if cameraUDID == udid then
local f = Spring.GetGameFrame() + KILL_OFFSET
killTable[f] = killTable[f] or {}
killTable[f][unitID] = true
killTableByUnitID[unitID] = f
end
end
function gadget:GameFrame(f)
if killTable[f] then
for unitID,_ in pairs(killTable[f]) do
if Spring.IsValidUnit(unitID) then
Spring.DestroyUnit(unitID, false, false)
end
killTableByUnitID[unitID] = nil
end
killTable[f] = nil
end
end
function gadget:UnitDestroyed(unitID)
if killTableByUnitID[unitID] then
killTable[killTableByUnitID[unitID]][unitID] = nil
killTableByUnitID[unitID] = nil
end
end
This is just typed from scratch in the last 5 minutes. As such I some arguments might be the wrong way around etc... Also there is a use of pairs that could be removed but it should be pretty inconsequential at the frequency and size of table it is used for.
So it depends on how much of a non-programmer you are. Can you slap a gadget:info on top and debug the occasional syntax error?
I like KDR_11K's idea too, it is worth looking into.
Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 19:44
by SanadaUjiosan
I like KDR's idea too, it seems simple and easy.
Could someone give me an example of what that little bit would look like in the script? I don't know what "get KILL_UNIT()" should look like.
This is what I tried:
Code: Select all
function script.Create()
Sleep(300)
get KILL_UNIT()
end
Very dumb, but it's all I could think of at the moment. (the sleep is low for testing reasons)
Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 19:49
by FLOZi
You are mixing COB and LUS.
Code: Select all
Spring.UnitScript.GetUnitValue(COB.KILL_UNIT)
But if you are using LUS you should just use the standard API anyway:
Code: Select all
Spring.DestroyUnit(unitID, selfD, reclaimed)
As explained by MidKnight.
Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 19:50
by CarRepairer
SanadaUjiosan wrote:I like KDR's idea too, it seems simple and easy.
Could someone give me an example of what that little bit would look like in the script? I don't know what "get KILL_UNIT()" should look like.
This is what I tried:
Code: Select all
function script.Create()
Sleep(300)
get KILL_UNIT()
end
Very dumb, but it's all I could think of at the moment. (the sleep is low for testing reasons)
KDR is an old dinosaur who uses extinct languages like BOS/COB.
Code: Select all
function script.Create()
Sleep(30000)
Spring.DestroyUnit(unitID)
end
Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 19:55
by FLOZi
Why the change in Sleep? Both bos/cob and lus use milliseconds.
Re: Existance of gadget that makes temporary units
Posted: 27 Feb 2011, 20:36
by SanadaUjiosan
Success!
I happily thank you guys. Lines in the credits.txt to CT for everyone!