-- Called when script loadsfunction on_load() console_out("Script loaded")end-- Called when script unloadsfunction on_unload() console_out("Script unloaded")end-- Called every tick (30 times per second)function on_tick() -- Game logic hereend-- Called every framefunction on_frame() -- Rendering logic hereend-- Called before camera updatefunction on_camera() -- Camera manipulation hereend-- Called on map loadfunction on_map_load() local map_name = get_map_name() console_out("Loaded map: " .. map_name)end-- Called when player spawnsfunction on_spawn(player_index) console_out("Player spawned: " .. player_index)end-- Called when player dies function on_death(victim, killer) console_out("Player died")end-- Called on chat messagefunction on_chat(player, message, channel) console_out(player .. ": " .. message) -- Return true to block the message return falseend-- Called when object is createdfunction on_object_create(object_id) -- Handle object creationend
-- Get current map namelocal map = get_map_name()-- Get game typelocal gametype = get_gametype()-- Check if in multiplayerlocal is_mp = is_multiplayer()-- Get tick countlocal ticks = get_tick_count()
-- Get player countlocal count = get_player_count()-- Iterate playersfor i = 0, 15 do local player = get_player(i) if player then local name = player.name local team = player.team local score = player.score endend-- Get local playerlocal local_player = get_local_player()
-- Get object by IDlocal object = get_object(object_id)if object then -- Read object data local x, y, z = object.x, object.y, object.z local health = object.health -- Modify object object.x = x + 1.0 object.health = 1.0end-- Create objectlocal tag_id = get_tag_id("weapons\\pistol\\pistol", "weap")if tag_id then local obj_id = create_object(tag_id, x, y, z)end-- Delete objectdelete_object(object_id)
-- scripts/map/welcome.luafunction on_map_load() local map = get_map_name() local messages = { bloodgulch = "Welcome to Blood Gulch!", sidewinder = "Welcome to Sidewinder!", dangercanyon = "Welcome to Danger Canyon!" } local msg = messages[map] or "Welcome to " .. map console_out(msg)end
-- scripts/global/auto_reload.luafunction on_tick() local local_player = get_local_player() if not local_player then return end -- Get player's weapon local weapon = get_weapon(local_player) if not weapon then return end -- Auto reload when ammo is low if weapon.ammo < weapon.max_ammo * 0.2 then execute_command("reload") endend
-- BAD: Expensive operation every tickfunction on_tick() for i = 1, 1000 do -- Heavy computation endend-- GOOD: Only when neededlocal counter = 0function on_tick() counter = counter + 1 if counter >= 30 then -- Once per second counter = 0 -- Heavy computation endend
function on_tick() local player = get_local_player() if not player then return end local obj = get_object(player.object_id) if obj then local pos = string.format("X: %.2f, Y: %.2f, Z: %.2f", obj.x, obj.y, obj.z) console_out(pos) endend
Chat Logger
function on_chat(player, message, channel) local timestamp = os.date("%Y-%m-%d %H:%M:%S") local log = string.format("[%s] %s: %s\n", timestamp, player, message) -- Append to log file local current = read_file("data/global/chat.log") or "" write_file("data/global/chat.log", current .. log) return false -- Don't block the messageend