User:
Generate Lua code for the Trailmakers modding API based on the following prompt. Adhere strictly to these guidelines:
FUNCTION USAGE:
Use ONLY the Trailmakers modding functions (tm.*) and basic Lua functions supported by MoonSharp.
DO NOT use any Lua standard libraries or functions not supported by MoonSharp (e.g., io, os, debug, package, coroutine).
DO NOT invent or use any functions not explicitly listed in the Trailmakers API documentation.
Each tm.* function must be used exactly as documented, with the correct number and types of parameters.
CODE STRUCTURE:
Use 'function update()' ONLY for actions that need to happen continuously or repeatedly (e.g., checking player position, updating UI).
For one-time actions or initializations, place the code outside any function.
If the prompt requires a one-time action, do not use 'function update()'.
Keep the code concise and directly addressing the prompt requirements.
TRAILMAKERS-SPECIFIC FUNCTIONS (Use these precisely):
Logging: tm.os.Log(message) - Use for any console output.
Vectors: tm.vector3.Create(x, y, z) - Use for all 3D positions or directions.
Object Spawning: tm.physics.SpawnObject(position, name) - Use for spawning in-game objects.
Player Interaction: tm.players.* functions (e.g., tm.players.GetPlayerPosition(playerId)) - Use for player-related actions.
UI: tm.playerUI.* functions (e.g., tm.playerUI.AddUIButton(playerId, id, text, callback)) - Use for creating UI elements.
Audio: tm.audio.PlayAudioAtPosition(audioName, position, duration) - Use for playing sounds.
Input: tm.input.RegisterFunctionToKeyDownCallback(playerId, functionName, keyName) - Use for handling player input.
DATA TYPES (Use these correctly):
PlayerID: Always a number (0-7). Player 0 is always the host.
StructureID: Always a string.
ModVector3: Created using tm.vector3.Create(x, y, z).
ModQuaternion: Created using tm.quaternion.Create().
Use tonumber() or tostring() when type conversion is necessary.
TIME AND EVENTS:
For time-based actions, use tm.os.GetTime() or tm.os.SimulationTime().
For frame-time actions, use tm.os.GetModDeltaTime().
CODE STYLE AND BEST PRACTICES:
Use proper indentation (4 spaces) and correct Lua syntax.
Use local variables whenever possible to avoid global namespace pollution.
Assistant
local function createObject(playerId)
local playerPosition = tm.players.GetPlayerPosition(playerId)
local spawnPosition = tm.vector3.Create(playerPosition.x, playerPosition.y + 5, playerPosition.z)
tm.physics.SpawnObject(spawnPosition, "Your_Spawnable_Name_Here") -- Replace with the actual spawnable name
tm.os.Log("Object spawned at " .. tostring(spawnPosition) .. " by player " .. playerId)
end
tm.input.RegisterFunctionToKeyDownCallback(0, "createObject", "C") -- Player 0 presses 'C' to create an object
Initialize variables outside update() when they don't need to be reset every frame.
Add brief comments only if necessary to explain complex logic.
Always check if required parameters are not nil before using them in functions.
PERFORMANCE CONSIDERATIONS:
Avoid creating new objects (especially vectors) inside update() if possible. Initialize them outside and reuse.
Use tm.os.SetModTargetDeltaTime() to control update frequency if necessary.
ERROR HANDLING:
Use pcall() for operations that might fail, and log errors with tm.os.Log().
LIMITATIONS:
Remember that the mod runs on the server, so client-side concepts don't apply.
There's no concept of "frames" in the traditional sense; everything is based on update cycles.
RELEVANT FUNCTIONS:
The following functions are particularly relevant to this prompt. Use these functions as needed:
tm.os.WriteAllText_Dynamic: Create or overwrite a file in the mods dynamic data directory. Files in the dynamic data directory can be both read and written to. The dynamic data directory will NOT be uploaded to the steam workshop when you upload your mod. When a mod is run through the steam workshop, the dynamic data, unlike static data, is not located in the steam workshop directory, but is located in the steam user data directory instead Parameters: path (string): Relative file path, stringToWrite (string): Data to write in the file. Returns: nil.
If a function is not listed here but is mentioned in the static guidelines above, you may use it if it's necessary for the prompt. However, prioritize using the functions listed here.
11. SPAWNABLE NAMES:
The following spawnable names are relevant to this prompt:
Use ONLY these EXACT names when spawning objects with tm.physics.SpawnObject(). Do not invent or modify any spawnable names.
12. AUDIO IDS:
The following audio IDs are relevant to this prompt:
Use ONLY these EXACT IDs when playing audio with tm.audio.PlayAudioAtPosition(). Do not invent or modify any audio IDs.
13. IMPORTANT:
- If no relevant spawnable names or audio IDs are provided, do not invent or guess any. Use only the exact names and IDs listed above.
- If the prompt requires a spawnable or audio that isn't listed, use a placeholder and add a comment indicating that the correct spawnable or audio ID should be used.
Examples of correct usage:
Spawning an object every 5 seconds (only if "PFB_Cube" is in the spawnable list):
local nextSpawnTime = 0
local spawnPosition = tm.vector3.Create(0, 10, 0)
function update()
local currentTime = tm.os.GetTime()
if currentTime >= nextSpawnTime then
tm.physics.SpawnObject(spawnPosition, "PFB_Cube")
nextSpawnTime = currentTime + 5
tm.os.Log("PFB_Cube spawned at " .. tostring(spawnPosition))
end
end
Responding to player input:
local function onAPressed(playerId)
local playerPosition = tm.players.GetPlayerPosition(playerId)
tm.os.Log("Player " .. playerId .. " pressed A at position: " .. tostring(playerPosition))
end
Creating UI for a player:
tm.playerUI.AddUIButton(0, "myButton", "Click me!", function(data)
tm.os.Log("Button clicked by player " .. data.playerId)
end)
Prompt: create a
Provide only the Lua code without any explanations or text outside the code block. Ensure the code is complete, concise, and directly addresses the prompt requirements using only the allowed Trailmakers modding functions and basic Lua. Do not include any additional comments or explanations unless they are crucial for understanding complex logic within the code.
User:
Generate Lua code for the Trailmakers modding API based on the following prompt. Adhere strictly to these guidelines:
FUNCTION USAGE:
Use ONLY the Trailmakers modding functions (tm.*) and basic Lua functions supported by MoonSharp.
DO NOT use any Lua standard libraries or functions not supported by MoonSharp (e.g., io, os, debug, package, coroutine).
DO NOT invent or use any functions not explicitly listed in the Trailmakers API documentation.
Each tm.* function must be used exactly as documented, with the correct number and types of parameters.
CODE STRUCTURE:
Use 'function update()' ONLY for actions that need to happen continuously or repeatedly (e.g., checking player position, updating UI).
For one-time actions or initializations, place the code outside any function.
If the prompt requires a one-time action, do not use 'function update()'.
Keep the code concise and directly addressing the prompt requirements.
TRAILMAKERS-SPECIFIC FUNCTIONS (Use these precisely):
Logging: tm.os.Log(message) - Use for any console output.
Vectors: tm.vector3.Create(x, y, z) - Use for all 3D positions or directions.
Object Spawning: tm.physics.SpawnObject(position, name) - Use for spawning in-game objects.
Player Interaction: tm.players.* functions (e.g., tm.players.GetPlayerPosition(playerId)) - Use for player-related actions.
UI: tm.playerUI.* functions (e.g., tm.playerUI.AddUIButton(playerId, id, text, callback)) - Use for creating UI elements.
Audio: tm.audio.PlayAudioAtPosition(audioName, position, duration) - Use for playing sounds.
Input: tm.input.RegisterFunctionToKeyDownCallback(playerId, functionName, keyName) - Use for handling player input.
DATA TYPES (Use these correctly):
PlayerID: Always a number (0-7). Player 0 is always the host.
StructureID: Always a string.
ModVector3: Created using tm.vector3.Create(x, y, z).
ModQuaternion: Created using tm.quaternion.Create().
Use tonumber() or tostring() when type conversion is necessary.
TIME AND EVENTS:
For time-based actions, use tm.os.GetTime() or tm.os.SimulationTime().
For frame-time actions, use tm.os.GetModDeltaTime().
CODE STYLE AND BEST PRACTICES:
Use proper indentation (4 spaces) and correct Lua syntax.
Use local variables whenever possible to avoid global namespace pollution.
Initialize variables outside update() when they don't need to be reset every frame.
Add brief comments only if necessary to explain complex logic.
Always check if required parameters are not nil before using them in functions.
PERFORMANCE CONSIDERATIONS:
Avoid creating new objects (especially vectors) inside update() if possible. Initialize them outside and reuse.
Use tm.os.SetModTargetDeltaTime() to control update frequency if necessary.
ERROR HANDLING:
Use pcall() for operations that might fail, and log errors with tm.os.Log().
LIMITATIONS:
Remember that the mod runs on the server, so client-side concepts don't apply.
There's no concept of "frames" in the traditional sense; everything is based on update cycles.
RELEVANT FUNCTIONS:
The following functions are particularly relevant to this prompt. Use these functions as needed:
tm.os.WriteAllText_Dynamic: Create or overwrite a file in the mods dynamic data directory. Files in the dynamic data directory can be both read and written to. The dynamic data directory will NOT be uploaded to the steam workshop when you upload your mod. When a mod is run through the steam workshop, the dynamic data, unlike static data, is not located in the steam workshop directory, but is located in the steam user data directory instead Parameters: path (string): Relative file path, stringToWrite (string): Data to write in the file. Returns: nil.
If a function is not listed here but is mentioned in the static guidelines above, you may use it if it's necessary for the prompt. However, prioritize using the functions listed here.
11. SPAWNABLE NAMES:
The following spawnable names are relevant to this prompt:
Use ONLY these EXACT names when spawning objects with tm.physics.SpawnObject(). Do not invent or modify any spawnable names.
12. AUDIO IDS:
The following audio IDs are relevant to this prompt:
Use ONLY these EXACT IDs when playing audio with tm.audio.PlayAudioAtPosition(). Do not invent or modify any audio IDs.
13. IMPORTANT:
- If no relevant spawnable names or audio IDs are provided, do not invent or guess any. Use only the exact names and IDs listed above.
- If the prompt requires a spawnable or audio that isn't listed, use a placeholder and add a comment indicating that the correct spawnable or audio ID should be used.
Examples of correct usage:
Spawning an object every 5 seconds (only if "PFB_Cube" is in the spawnable list):
local nextSpawnTime = 0
local spawnPosition = tm.vector3.Create(0, 10, 0)
function update()
local currentTime = tm.os.GetTime()
if currentTime >= nextSpawnTime then
tm.physics.SpawnObject(spawnPosition, "PFB_Cube")
nextSpawnTime = currentTime + 5
tm.os.Log("PFB_Cube spawned at " .. tostring(spawnPosition))
end
end
Responding to player input:
local function onAPressed(playerId)
local playerPosition = tm.players.GetPlayerPosition(playerId)
tm.os.Log("Player " .. playerId .. " pressed A at position: " .. tostring(playerPosition))
end
Creating UI for a player:
tm.playerUI.AddUIButton(0, "myButton", "Click me!", function(data)
tm.os.Log("Button clicked by player " .. data.playerId)
end)
Prompt: create a
Provide only the Lua code without any explanations or text outside the code block. Ensure the code is complete, concise, and directly addresses the prompt requirements using only the allowed Trailmakers modding functions and basic Lua. Do not include any additional comments or explanations unless they are crucial for understanding complex logic within the code.