Example: Color Changing Functions

Example: Color Changing Functions

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

Moderator: Moderators

Post Reply
User avatar
REVENGE
Posts: 2382
Joined: 24 Aug 2006, 06:13

Example: Color Changing Functions

Post by REVENGE »

While writing the ctf gadget, I made some color changing functions, which I'm posting here as example code free for use (probably by newbies).

In order to use them, you must first setup a timer during initialization. For these, I've used

Code: Select all

local startTimer = Spring.GetTimer()
I also localized

Code: Select all

Spring.DiffTimers()
as

Code: Select all

DiffTimers()
Once that is done, here come the functions:

Code: Select all

--Cycles from 'value1' to 'value2' at designated 'rate'
--There are 3 options that control switching: 0 or nil is a square wave, 1 is a sawtooth wave, 2 is a sine wave
function ColorCycle(r1, g1, b1, a1, r2, g2, b2, a2, rate, option)
	local diffTime = DiffTimers(GetTimer(), startTimer)
	local factor	
	if (option == 1) then
		factor = math.abs(1.0 - math.mod(diffTime * rate, 2.0))
	elseif (option == 2) then
		factor = math.abs(math.sin(math.mod(diffTime * rate, 2.0) * math.pi))
	else
		if (math.mod(math.floor(diffTime * rate), 2.0) == 0) then
			return r1, g1, b1, a1
		else
			return r2, g2, b2, a2
		end
	end
	
	local r = r1 + ((r2 - r1) * factor)
	local g = g1 + ((g2 - g1) * factor)
	local b = b1 + ((b2 - b1) * factor)	
	local a = a1 + ((a2 - a1) * factor)
	return r, g, b, a	
end
		
--Fades from 'value1' to 'value2' at designated 'rate'
--Direction is binary: 1 fades in, 0 or nil fades out, default is fade out
function ColorFade(r1, g1, b1, a1, r2, g2, b2, a2, rate, direction)
	local diffTime	= DiffTimers(GetTimer(), startTimer)
	local factor
	if (direction == 1) then
		factor = math.abs(1.0 - math.mod(diffTime * rate, 1.0))
	else
		factor = math.abs(math.mod(diffTime * rate, 1.0))
	end
	
	local r = r1 + ((r2 - r1) * factor)
	local g = g1 + ((g2 - g1) * factor)
	local b = b1 + ((b2 - b1) * factor)	
	local a = a1 + ((a2 - a1) * factor)
	return r, g, b, a
end
Questions, comments, suggestions are welcome. I'm posting these here, with the hope that maybe in the future, we could get a better practical tutorial to lua for Spring going.
User avatar
Evil4Zerggin
Posts: 557
Joined: 16 May 2007, 06:34

Re: Example: Color Changing Functions

Post by Evil4Zerggin »

Question: To measure local time, assuming accuracy is not too important and only differences in time matter (e.g., widget gfx), is it better to use DiffTimers() or keep track of time through widget:Update(dt)?
User avatar
REVENGE
Posts: 2382
Joined: 24 Aug 2006, 06:13

Re: Example: Color Changing Functions

Post by REVENGE »

I don't know about efficiency, but one important difference to keep in mind is that timers don't pause when you pause the game, but it looks like update(dt) will.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Example: Color Changing Functions

Post by Argh »

Er... a lot of stuff like special effects needs to alter over gameframe-time, not absolute time. It's pretty important that it stays locked with sync to some extent. I really like the gamecode example though, that looks like it'd be useful if I ever have time to mess around with that end of things again.
User avatar
Evil4Zerggin
Posts: 557
Joined: 16 May 2007, 06:34

Re: Example: Color Changing Functions

Post by Evil4Zerggin »

REVENGE wrote:I don't know about efficiency, but one important difference to keep in mind is that timers don't pause when you pause the game, but it looks like update(dt) will.
Update(dt) doesn't pause either.
User avatar
REVENGE
Posts: 2382
Joined: 24 Aug 2006, 06:13

Re: Example: Color Changing Functions

Post by REVENGE »

Evil4Zerggin wrote:
REVENGE wrote:I don't know about efficiency, but one important difference to keep in mind is that timers don't pause when you pause the game, but it looks like update(dt) will.
Update(dt) doesn't pause either.
Hmm...then the only synced methods of timing I can think of are manually getting gameframe diffs or gamesecond diffs. Any other way I don't know about?
Post Reply

Return to “Lua Scripts”