User:Fonikku/Common LUA Scripts

From SRB2 Wiki
Jump to navigation Jump to search

Common LUA Scripts and Functions that i use for my mods.

Math library

Clamp

rawset(_G, "Clamp", function(n, _min, _max)
	if (n < _min) n = _min
	elseif (n > _max) n = _max end
	return n
end)

SignOf

rawset(_G, "SignOf", function(val)
	return Clamp(val, -1, 1)
end)

FixedSignOf

rawset(_G, "FixedSignOf", function(val)
	return Clamp(val, -FRACUNIT, FRACUNIT-1)
end)

LinearApproach

rawset(_G, "LinearApproach", function(val, to, shift)
	if (val < to)
		return min(val + shift, to)
	else
		return max(val - shift, to)
	end
end)

Base library

Base Functions

SafeFreeslot

rawset(_G, "SafeFreeslot", function(...)
	for k, v in ipairs({...}) do
		if tostring(v) == nil return end
		if rawget(_G, v) == nil
			freeslot(v)
		end
	end
end)

G_Game

G_CoopLives

rawset(_G, "G_CoopLives", function()
	return CV_FindVar("cooplives").value
end)

P_User

P_MobjUnderwater

rawset(_G, "P_MobjUnderwater", function(target)
	if target and target.eflags & MFE_UNDERWATER
		return true
	end
	return false
end)

Console Library

CV_FindValue

rawset(_G, "CV_FindValue", function(cvar)
	return CV_FindVar(cvar).value
end)