Table Util
Utility functions for various calculations.
Functions
| HasLineOfSight(roomID, posA, posB) | Determine if there is a clear line of sight between two positions. |
| CalculateHorizontalDistance(posA, posB) | Calculate the horizontal distance between two positions. |
| GetDisplayPosition(worldPos) | Get the projected display space position of a 3D world position. |
| PercentToScreen(x, y) | Translate a pair display position coordinates to pixel coordinates. To be used with Strings.DisplayString and Strings.DisplayString:SetPosition. |
| PercentToScreen(percentPos) | Translate a Vec2 of display position coordinates to Vec2 pixel coordinates. To be used with Strings.DisplayString and Strings.DisplayString:SetPosition. |
| ScreenToPercent(x, y) | Translate a pair of pixel coordinates to display position coordinates. |
| ScreenToPercent(screenPos) | Translate a Vec2 of pixel coordinates to Vec2 display position coordinates. |
| PickMoveableByDisplayPosition(position) | Pick a moveable by the given display position. |
| PickStaticByDisplayPosition(position) | Pick a static mesh by the given display position. |
| PrintLog(Message, logLevel, [allowSpam]) | Write messages within the Log file. For native Lua handling of errors, see Error management and debug.traceback |
Functions
- HasLineOfSight(roomID, posA, posB)
-
Determine if there is a clear line of sight between two positions. Limited to room geometry. Objects are ignored.
Parameters:
- roomID float Room ID of the first position's room.
- posA Vec3 First position.
- posB Vec3 Second position.
Returns:
-
bool
true if there is a line of sight, false if not.
Usage:
local flamePlinthPos = flamePlinth:GetPosition() + Vec3(0, flamePlinthHeight, 0); local test = TEN.Util.HasLineOfSight(enemyHead:GetRoomNumber(), enemyHead:GetPosition(), flamePlinthPos); print(tostring(test))
- CalculateHorizontalDistance(posA, posB)
-
Calculate the horizontal distance between two positions.
Parameters:
Returns:
-
float
Horizontal distance between the two positions.
Usage:
local dist = TEN.Util.CalculateHorizontalDistance(Lara:GetPosition(), enemyHead:GetPosition()) - GetDisplayPosition(worldPos)
-
Get the projected display space position of a 3D world position. Returns nil if the world position is behind the camera view.
Parameters:
- worldPos Vec3 3D world position.
Returns:
-
Vec2
Projected display space position in percent.
Usage:
-- Example: Display a string at the player's position. local displayPos = TEN.Util.GetDisplayPosition(Lara:GetPosition()) local str = TEN.Strings.DisplayString('You are here!', displayPos) TEN.Strings.ShowString(str, 4)
- PercentToScreen(x, y)
-
Translate a pair display position coordinates to pixel coordinates.
To be used with Strings.DisplayString and Strings.DisplayString:SetPosition.Parameters:
- x float X component of the display position.
- y float Y component of the display position.
Returns:
- int x X coordinate in pixels.
- int y Y coordinate in pixels.
Usage:
-- Example 1: Simple usage of PercentToScreen. local screenX, screenY = TEN.Util.PercentToScreen(10, 10) local pos = TEN.Vec2(screenX, screenY) local str1 = TEN.Strings.DisplayString("Position at 10% X and 10% Y", pos) -- Example 2: Using PercentToScreen with DisplayString:SetPosition. str1:SetPosition(TEN.Util.PercentToScreen(50, 50))
- PercentToScreen(percentPos)
-
Translate a Vec2 of display position coordinates to Vec2 pixel coordinates.
To be used with Strings.DisplayString and Strings.DisplayString:SetPosition.Parameters:
- percentPos Vec2 Display position to translate to pixel coordinates.
Returns:
-
Vec2
Pixel coordinates.
Usage:
-- Example: Translate a display position to pixel coordinates. local percentPos = TEN.Vec2(25, 75) local screenPos = TEN.Util.PercentToScreen(percentPos) local str1 = TEN.Strings.DisplayString("Position at 25% X and 75% Y", screenPos) -- Example 2: Using PercentToScreen with DisplayString:SetPosition. str1:SetPosition(TEN.Util.PercentToScreen(TEN.Vec2(50, 50)))
- ScreenToPercent(x, y)
-
Translate a pair of pixel coordinates to display position coordinates.
To be used with Strings.DisplayString:GetPosition.
Parameters:
- x int X pixel coordinate to translate to display position.
- y int Y pixel coordinate to translate to display position.
Returns:
- float x X component of display position.
- float y Y component of display position.
Usage:
-- Example: Translate pixel coordinates to display position. local percentX, percentY = TEN.Util.ScreenToPercent(800, 600) -- Example 2: Using ScreenToPercent with DisplayString:GetPosition. local screenPos = str1:GetPosition() local percentPos = TEN.Util.ScreenToPercent(screenPos.x, screenPos.y)
- ScreenToPercent(screenPos)
-
Translate a Vec2 of pixel coordinates to Vec2 display position coordinates.
To be used with Strings.DisplayString:GetPosition.
Parameters:
- screenPos Vec2 Pixel coordinates to translate to display position.
Returns:
-
Vec2
Display position.
Usage:
-- Example: Translate pixel coordinates to display position. local screenPos = TEN.Vec2(400, 300) local percentPos = TEN.Util.ScreenToPercent(screenPos) -- Example 2: Using ScreenToPercent with DisplayString:GetPosition. local textPos = str1:GetPosition() local percentPos = TEN.Util.ScreenToPercent(textPos)
- PickMoveableByDisplayPosition(position)
-
Pick a moveable by the given display position.
Parameters:
- position Vec2 Display space position in percent.
Returns:
-
Moveable
Picked moveable (nil if no moveable was found under the cursor).
Usage:
-- Example: Pick a moveable at the center of the screen. local screenCenter = TEN.Vec2(50, 50) local pickedMoveable = TEN.Util.PickMoveableByDisplayPosition(screenCenter) if pickedMoveable then print("Picked moveable: " .. pickedMoveable:GetName()) else print("No moveable found at the center of the screen.") end
- PickStaticByDisplayPosition(position)
-
Pick a static mesh by the given display position.
Parameters:
- position Vec2 Display space position in percent.
Returns:
-
Static
Picked static mesh (nil if no static mesh was found under the cursor).
Usage:
-- Example: Pick a static mesh at the center of the screen. local screenCenter = TEN.Vec2(50, 50) local pickedStatic = TEN.Util.PickStaticByDisplayPosition(screenCenter) if pickedStatic then print("Picked static mesh.") else print("No static mesh found at the center of the screen.") end
- PrintLog(Message, logLevel, [allowSpam])
-
Write messages within the Log file.
For native Lua handling of errors, see Error management and debug.tracebackParameters:
- Message string to be displayed within the log.
- logLevel LogLevel Log level to be displayed.
- allowSpam bool If true, allows continuous spamming of the message. Optional.
Usage:
TEN.Util.PrintLog('test info log', TEN.Util.LogLevel.INFO) TEN.Util.PrintLog('test warning log', TEN.Util.LogLevel.WARNING) TEN.Util.PrintLog('test error log', TEN.Util.LogLevel.ERROR) -- spammed message TEN.Util.PrintLog('test spam log', TEN.Util.LogLevel.INFO, true)