2024-12-16 21:25:17 +01:00
-----
--- Type - This molule contains functions that allow to check the data type of a variable. It also contains functions that allow to check if the variable is a TEN primitive class or a LevelFuncs.
--
--
-- To use the functions within the scripts, the module must be called:
-- local Type = require("Engine.Type")
--
--
-- Example usage: check if a variable is of type Vec3()
-- local Type= require("Engine.Type")
--
-- LevelFuncs.SetLaraPos = function (pos)
-- if Type.IsVec3(pos) then
-- Lara:SetPosition(pos)
-- end
-- end
--
--
-- You can use the `not` keyword together with the functions of the Type module.
--
-- Example: checking if variable does not have a null value
-- LevelFuncs.AddProp = function (prop)
-- if not Type.IsNull(prop) then
-- LevelVars.property = prop
-- end
-- end
-- @luautil Type
2025-02-09 15:58:15 +01:00
local color = TEN.Color ( 0 , 0 , 0 )
local rotation = TEN.Rotation ( 0 , 0 , 0 )
local time = TEN.Time ( )
local vec2 = TEN.Vec2 ( 0 , 0 )
local vec3 = TEN.Vec3 ( 0 , 0 , 0 )
LevelFuncs.TypeControlLevelFunc = function ( ) end
2024-12-16 21:25:17 +01:00
2025-02-09 15:58:15 +01:00
local Type = { }
2024-12-16 21:25:17 +01:00
2025-02-09 15:58:15 +01:00
--- Check if the variable is a number.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a number, *false* otherwise
-- @usage
-- --example of use
-- local num = 255
-- if Type.IsNumber(num) then
-- num = num + 1
-- end
2024-12-16 21:25:17 +01:00
Type.IsNumber = function ( variable )
return type ( variable ) == " number "
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a string.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a string, *false* otherwise
-- @usage
-- --example of use
-- local str = "Hi"
-- if Type.IsString(str) then
-- TEN.Util.PrintLog(str .. "everyone!", Util.LogLevel.INFO)
-- end
2024-12-16 21:25:17 +01:00
Type.IsString = function ( variable )
return type ( variable ) == " string "
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a boolean.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a boolean, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.test = function (test)
-- if Type.IsBoolean(test) then
-- LevelVars.test = test
-- else
-- TEN.Util.PrintLog("Error!", Util.LogLevel.ERROR)
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsBoolean = function ( variable )
return type ( variable ) == " boolean "
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a table.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a table, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.PairsTable = function (table)
-- if Type.IsTable(table) then
-- for k, v in pairs(table) do
-- TEN.Util.PrintLog(tostring(k) .. " - " .. tostring(v), Util.LogLevel.INFO)
-- end
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsTable = function ( variable )
return type ( variable ) == " table "
end
2025-02-09 15:58:15 +01:00
--- Check if the variable has a null value.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a null, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.AddProp = function (prop)
-- if Type.IsNull(prop) then
-- TEN.Util.PrintLog("Error!", Util.LogLevel.ERROR)
-- else
-- LevelVars.property = prop
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsNull = function ( variable )
return type ( variable ) == " nil "
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a function.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a function, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.RunFunc = function (func)
-- if Type.IsFunction(func) then
-- func()
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsFunction = function ( variable )
return type ( variable ) == " function "
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a @{Color}.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a Color, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.SetColor = function(color)
-- if Type.IsColor(color) then
-- string:SetColor(color)
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsColor = function ( variable )
2025-02-09 15:58:15 +01:00
return getmetatable ( variable ) == getmetatable ( color )
2024-12-16 21:25:17 +01:00
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a @{Rotation}.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a Rotation, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.SetRotation = function (rot)
-- if Type.IsRotation(rot) then
-- Lara:SetRotation(rot)
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsRotation = function ( variable )
2025-02-09 15:58:15 +01:00
return getmetatable ( variable ) == getmetatable ( rotation )
2024-12-16 21:25:17 +01:00
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a @{Vec2}.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a Vec2, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.SetSpritePos = function (pos)
-- if Type.IsVec2(pos) then
-- sprite:SetPosition(pos)
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsVec2 = function ( variable )
2025-02-09 15:58:15 +01:00
return getmetatable ( variable ) == getmetatable ( vec2 )
2024-12-16 21:25:17 +01:00
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a @{Vec3}.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a Vec3, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.SetLaraPos = function (pos)
-- if Type.IsVec3(pos) then
-- Lara:SetPosition(pos)
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsVec3 = function ( variable )
2025-02-09 15:58:15 +01:00
return getmetatable ( variable ) == getmetatable ( vec3 )
2024-12-16 21:25:17 +01:00
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a @{Time} object.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a Time object, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.IncreaseTime = function (time)
-- if Type.IsTime(time) then
-- time + 1
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsTime = function ( variable )
2025-02-09 15:58:15 +01:00
return getmetatable ( variable ) == getmetatable ( time )
2024-12-16 21:25:17 +01:00
end
2025-02-09 15:58:15 +01:00
--- Check if the variable is a LevelFunc.
-- @tparam variable variable to be check
-- @treturn boolean *true* if the variable is a LevelFunc, *false* otherwise
-- @usage
-- --example of use
-- LevelFuncs.SetCallback = function (func)
-- if Type.IsFunction(func) then
-- TEN.Logic.AddCallback(TEN.Logic.CallbackPoint.PRELOOP, func)
-- end
-- end
2024-12-16 21:25:17 +01:00
Type.IsLevelFunc = function ( variable )
2025-02-09 15:58:15 +01:00
return getmetatable ( variable ) == getmetatable ( LevelFuncs.TypeControlLevelFunc )
2024-12-16 21:25:17 +01:00
end
return Type