Skip to content

Map Scripts

KernCore edited this page Nov 29, 2021 · 4 revisions

Sven Co-op allows maps to use scripts. A map script is active as long as the map itself is active, and can manipulate just about everything in the map.

A Simple Map Script

The simplest map script is an empty script, since there are no required parts to it. As covered by the Script fundamentals documentation, there are a few functions that are automatically called. Here's a script that will output "Hello World!" without quotes to the console when the script is initialized:

void MapInit()
{
	g_Game.AlertMessage( at_console, "Hello World!\n" );
}

Where to place map scripts

Map scripts must be placed in the scripts/maps directory. You can place them in a subdirectory if you want, but be aware that using long paths can cause issues, so avoid using deeply nested paths and long directory names.

It is recommended that you use directories to group scripts together to avoid cluttering the main directory, and to avoid using the same name as other scripts. Using the name of the map or map series you're making the script(s) for will usually suffice.

How to load map scripts

There are 2 ways to load map scripts: by specifying it in the map cfg, or in a trigger_script entity. The starting directory is scripts/maps.

When using a map cfg to specify it, you must use the map_script command. You can use this command multiple times to specify multiple scripts.

For example:

map_script HLSP

When using a trigger_script, specifying a script name in "Script to load" (m_iszScriptFile) will load it as though it were specified in the map cfg.

Console commands

There are a few console commands that apply to map scripts:

Command name Purpose Admin only?
as_listmapscriptinfo Outputs map script information No
as_scriptbaseclasses Shows the custom entity base class implementations (Warning: creates a substantial amount of console output!) Yes

API availability

Most of the API is available for map scripts to use, but there are a few things that are exclusive to them:

  • Entity user data: only map scripts can use this to avoid plugins altering the behavior of map scripts.
  • Custom entities: both map scripts and plugins can define and use custom entities. In the past this used to be available only for map scripts to avoid plugins from altering the gameplay of a map, and to avoid creating problems when plugins are (re)loaded while a map is active, this is no longer the case.
  • Entity loader: since this behaves like the map loading process, only map scripts can access it to prevent plugins from altering gameplay.
  • map and map temp directories in the filesystem: Ability to read/write files in these directories is restricted to map scripts.
  • trigger_script
  • Hooks:
    • PrimaryAttack
    • SecondaryAttack
    • TertiaryAttack
    • PlayerUse
    • PlayerPreThink
    • PlayerPostThink
    • GetPlayerSpawnSpot

Consult the documentation for the respective features for more information. For information on which parts of the API are exclusive to other script types, consult their documentation.

Clone this wiki locally