Help making a special power - Page 2

Help making a special power

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

User avatar
code_man
Posts: 260
Joined: 19 Jan 2014, 13:10

Re: Help making a special power

Post by code_man »

I dont know how im supposed to picture this working.
I figgure il just post the code here so you can see sort of what i want.

Code: Select all

-- satelite_scan.lua

--

function gadget:GetInfo()
    return {
        name = "Satelite Scan",
        desc = "Gives a satelite scan power to players",
        author = "Code_Man",
        date = "2014-1-3",
        license = "Public domain",
        layer = 1,
        enabled = true
    }
end

if (not gadgetHandler:IsSyncedCode()) then
    return false
end

local timerTable = {}

local function scan(team)
    --if (timerTable[team] < Spring.GetGameSeconds()) then
    --    return false
    --end
    local unit = Spring.CreateUnit("scan_object", Game.mapSizeX / 2, 300, Game.mapSizeZ / 2, 0, team)
    timerTable[team] = Spring.GetGameSeconds() + 60
    GG.Delay.DelayCall(Spring.DestroyUnit, {unit}, 1000)
end

function gadget:RecvLuaMsg(msg, playerID)
    local active, spectator, teamID = Spring.GetPlayerInfo(playerID)
    --if (not spectator) and active then
        scan(TeamID)
    --end
end

function gadget:GameStart()
    for _, team in ipairs(Spring.GetTeamList()) do
        if (team ~= Spring.GetGaiaTeamID()) then
            timerTable[team] = Spring.GetGameSeconds() + 60
        end
    end
end

function gadget:GameFrame(frame)
    for _, team in ipairs(Spring.GetTeamList()) do
        if (team ~= Spring.GetGaiaTeamID()) then
            if(timerTable[team] >= Spring.GetGameSeconds()) then
                Spring.SetTeamRulesParam(team, "SateliteScanTimer", timerTable[team] - Spring.GetGameSeconds())
            end
        end
    end
end
As for the widget, it works just fine i tested with echo and that works properly when i press the button.
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Help making a special power

Post by knorke »

Spring wants to prevent you to use CreateUnit in RecvLuaMsg()
Why excactly, I do not know. Maybe has something to do with preventing desyncs, but no idea.
This should work, hope comments explain it:

Code: Select all

function gadget:GetInfo()
    return {
        name = "Satelite Scan",
        desc = "Gives a satelite scan power to players",
        author = "Code_Man",
        date = "2014-1-3",
        license = "Public domain",
        layer = 1,
        enabled = true
    }
end

if (not gadgetHandler:IsSyncedCode()) then
    return false
end

local timerTable = {}
local scannersToSpawn = {} --stores scans that will be done next gameframe

local function scan(teamID, x,z)
    --if (timerTable[team] < Spring.GetGameSeconds()) then
    --    return false
    --end
    local unit = Spring.CreateUnit("tptank", x, 300, z, "west",teamID)
    --timerTable[team] = Spring.GetGameSeconds() + 60
    --GG.Delay.DelayCall(Spring.DestroyUnit, {unit}, 1000)
end



function gadget:RecvLuaMsg(msg, playerID)
    local name, active, spectator, teamID = Spring.GetPlayerInfo(playerID)
    --if (not spectator) and active then
	local x,z=Game.mapSizeX / 2 , Game.mapSizeX / 2	--real coordinates would be included in the msg string	
	scannersToSpawn[#scannersToSpawn+1] = {ownerTeam=teamID, scanX=x, scanZ=z}
end

function gadget:GameStart()	--use Initialize(), will work on /luarules reload = easier testing
    for _, team in ipairs(Spring.GetTeamList()) do
        if (team ~= Spring.GetGaiaTeamID()) then
            timerTable[team] = Spring.GetGameSeconds() + 60
        end
    end
end

function gadget:GameFrame(frame)
if frame%60==0 then Spring.Echo ("alive"..frame) end --pro way to see if gadget has not crashed yet	
	if (#scannersToSpawn>0) then	--are scans waiting to be done?
		Spring.Echo ("spawn some scanners!")
		for i,v in ipairs (scannersToSpawn) do	--loop over all scans that need to be done
			Spring.Echo ("i="..i .. "ownerTeam="..scannersToSpawn[i].ownerTeam)
			scan (scannersToSpawn[i].ownerTeam, scannersToSpawn[i].scanX, scannersToSpawn[i].scanX)
		end
		scannersToSpawn = {} --all scans done, clear table
	end
end
User avatar
code_man
Posts: 260
Joined: 19 Jan 2014, 13:10

Re: Help making a special power

Post by code_man »

Ok its roughly working now, thanks guys. :-)
Post Reply

Return to “Lua Scripts”