StartCountdown

» Siedler Map Source Forum » Siedler DEdK Script Forum » StartCountdown

Seiten: 1

Play4FuN
#1
14.01.2023 12:27
Beiträge: 704

StartCountdown

Da ich lua Code zu meinen Tutorials jetzt nicht mehr (kostenlos) auf codepile laden kann und es inhaltlich auch hierher passt ist hier der StartCountdown Code mit ein paar Anpassungen meinerseits.

-- updated: if a countdown is already visible, a new one can be started without displaying it
-- added: optional parameter (any)
-- bug fix: show countdown again after loading a savegame
function StartCountdown(_Limit, _Callback, _Show, _Parameter)
	
	assert(type(_Limit) == "number")
	
	Counter.Index = (Counter.Index or 0) + 1
	
	if _Show and CountdownIsVisisble() then
			--assert(false, "StartCountdown: A countdown is already visible")
			Message("StartCountdown Error: A countdown is already visible")
	_Show = false
	end
	
	Counter["counter" .. Counter.Index] = {
		Limit = _Limit,
		TickCount = 0,
		Callback = _Callback,
		Show = _Show,
		Finished = false,
		parameter = _Parameter,	-- can be nil
	}
	
	if _Show then
			MapLocal_StartCountDown(_Limit)
	end
	
	if Counter.JobId == nil then
			Counter.JobId = StartSimpleJob("CountdownTick")
	end
	
	if not gvRestoreCountdownFix then
		AddOnSaveGameLoaded( RestoreVisibleCountdown )
		gvRestoreCountdownFix = true
	end
	
	return Counter.Index
end

function StopCountdown(_Id)
	
	if Counter.Index == nil then
			return
	end
	
	if _Id == nil then
			for i = 1, Counter.Index do
					if Counter.IsValid("counter" .. i) then
							if Counter["counter" .. i].Show then
									MapLocal_StopCountDown()
							end
							Counter["counter" .. i] = nil
					end
			end
	else
			if Counter.IsValid("counter" .. _Id) then
					if Counter["counter" .. _Id].Show then
							MapLocal_StopCountDown()
					end
					Counter["counter" .. _Id] = nil
			end
	end
end

function CountdownTick()
	
	local empty = true
	for i = 1, Counter.Index do
			if Counter.IsValid("counter" .. i) then
					if Counter.Tick("counter" .. i) then
							Counter["counter" .. i].Finished = true
					end
					
					if Counter["counter" .. i].Finished and not IsBriefingActive() then
							if Counter["counter" .. i].Show then
									MapLocal_StopCountDown()
							end
							
							if type(Counter["counter" .. i].Callback) == "function" then
									Counter["counter" .. i].Callback( Counter["counter" .. i].parameter )
							end
							
							Counter["counter" .. i] = nil
					end
					
					empty = false
			end
	end
	
	if empty then
			Counter.JobId = nil
			Counter.Index = nil
			return true
	end
end

function CountdownIsVisisble()
	
	if not Counter.Index then
		return false
	end
	
	for i = 1, Counter.Index do
		if Counter.IsValid("counter" .. i) and Counter["counter" .. i].Show and not Counter["counter" .. i].Finished then
			-- LuaDebugger.Break()
			return true
		end
	end
	
	return false
end

function CountdownGetVisisbleCounter()
	
	if not Counter.Index then
		return false
	end
	
	for i = 1, Counter.Index do
			if Counter.IsValid("counter" .. i) and Counter["counter" .. i].Show then
					return Counter["counter" .. i]
			end
	end
	
	return false
end


Damit ein CD nach dem Laden eines Spielstands auch wieder angezeigt wird:

-- visible countdown disappears after loading a savegame
--> check if a counter should be visible and show it
function RestoreVisibleCountdown()
	local visibleCounter = CountdownGetVisisbleCounter()
	if not visibleCounter then
		return
	end
	
	local ultimatumTime = visibleCounter.Limit - visibleCounter.TickCount
	GUIQuestTools.ToggleStopWatch( ultimatumTime, 1 )
end

-- add a function to be executed on Mission_OnSaveGameLoaded
-- idea from bobby (?)
function AddOnSaveGameLoaded( func )
	
	if not OnSaveGameLoaded then
		-- init
		OnSaveGameLoaded = {
			Functions = {},
			OrigFunction = Mission_OnSaveGameLoaded,
		}
		Mission_OnSaveGameLoaded = function()
			OnSaveGameLoaded.OrigFunction()
			for i = 1, table.getn( OnSaveGameLoaded.Functions ) do
				OnSaveGameLoaded.Functions[i]()
			end
		end
	end
	
	table.insert( OnSaveGameLoaded.Functions, func )
end



____________________
LG Play4FuN

Siedler DEdK Mapping + Scripting Tutorials

Seiten: 1

SiteEngine v1.5.0 by nevermind, ©2005-2007
Design by SpiderFive (www.siedler-games.de) - English translation by juja

Impressum