Modding/Templates/Freeplay

From Desynced Wiki

This mod template customizes the starting loadout of the default Freeplay scenario.

Code[edit | edit source]

def.json[edit | edit source]

Content

{
    "id": "ModTemplateFreeplay",
    "name": "Mod Template Freeplay",
    "version_name": "1.0",
    "version_code": 1,
    "author": "The Desynced Team",
    "homepage": "https://www.desyncedgame.com/",
    "description": "Mod Template Freeplay",
    "packages": {
        "Scenario": {
            "name": "Scenario",
            "entry": "scenario.lua",
            "dependencies": [ "Main/Freeplay" ],
            "type": "Scenario"
        }
    }
}

scenario.lua[edit | edit source]

Content

local package = ...

-- called when starting a new game (skipped when loading a save or joining a multiplayer game)
function package:setup_scenario(settings)
end

-- called when mod is initializing
function package:init()
	-- disable the on_player_faction_spawn function on the main package
	Game.GetModPackage("Main/Freeplay").on_player_faction_spawn = nil
end

-- called when starting up a new game
function package:on_world_spawn()
end

-- called when a new player faction is spawned or respawned
function package:on_player_faction_spawn(faction, is_respawn)
	-- select starting location for player faction
	faction.home_location = GetPlayerFactionHomeOnGround()
	local loc = faction.home_location

	-- spawn a bot
	local bot = Map.CreateEntity(faction, "f_bot_1s_a")
	bot:Place(loc.x, loc.y)

	-- unlock starter tech
	faction:Unlock("t_robot_tech_basic")

	-- show intro
	faction:RunUI("OnFreeplayFactionSpawned")

	-- show welcome message after 3 seconds
	Map.Delay("StartOfGame", 15, { faction = faction })
end