- Joined
- Jun 23, 2007
- Messages
- 4,066
vJass: Advanced Initialization
Table of Contents
Introduction
Manual Initialization
Notes
Table of Contents
Introduction
This tutorial will teach you how to control the order and manner in which code is initialized.
Generally vJass initializers are ran first, then custom triggers are ran after. However you can't pick the order by default.
I will show you how this is changeable and how you can also avoid relatively slow ExecuteFunc calls.
JassHelper (or JNGP) is required.
Main and ConfigGenerally vJass initializers are ran first, then custom triggers are ran after. However you can't pick the order by default.
I will show you how this is changeable and how you can also avoid relatively slow ExecuteFunc calls.
JassHelper (or JNGP) is required.
Every map has two core functions it runs on initialization (main and config).
Inside these functions contains a bunch of generated code that initializes things in your map like custom triggers (InitTrig_Function).
Typically in vJass you initialize functions by using the
Inside
Now, there's nothing wrong with this however maybe you want more control over how things are handled. Not to mention ExecuteFunc calls are significantly slower than normal function calls (*).
To do this we use the very convenient
Before we do anything, lets go over each function.
Main
Config
Inside these functions contains a bunch of generated code that initializes things in your map like custom triggers (InitTrig_Function).
Typically in vJass you initialize functions by using the
initializer
keyword or by a structs onInit method. Inside
function main
is where JassHelper handles custom initializers by placing anExecuteFunc
call for each one. For example
JASS:
library Lib initializer onInit
private function onInit takes nothing returns nothing
endfunction
endlibrary
// adds this to your scripts main function
function main takes nothing returns nothing
//..
call ExecuteFunc("Lib___onInit")
endfunction
To do this we use the very convenient
//! inject
feature which allows you to modify the config and main functions of your maps script. You can read more about the inject command in the JassHelper Manual.Before we do anything, lets go over each function.
Main
- This function is ran after the config function.
- If you don't place dovjassinit inside the inject block then JassHelper will throw an error.
- Responsible for calling functions that create the editors preplaced objects in your map (units, cameras, regions).
JASS://! inject main /* * dovjassinit places all vJass initializers. * This way you can control what's initialized before or after them. */ //! dovjassinit // These initialize preplaced objects in the editor call CreateRegions() call CreateCameras() call CreateAllItems() call CreateAllUnits() // call InitTrig's call InitCustomTriggers() //! endinject
Config
- This function is ran before the main function.
- Does not allow dovjassinit (will throw error).
- Requires
SetPlayers
andInitCustomPlayerSlots
function calls otherwise your map won't load. Normally the first argument of SetPlayers is the amount of players in the map, however it seems that any number works. - Responsible for defining start locations. If the function is overwritten and no
DefineStartLocation
calls are made, thenGetPlayerStartLocation
will return null.
JASS://! inject config call SetPlayers(0) call DefineStartLocation(0, 704.0, - 832.0) call InitCustomPlayerSlots() //! endinject
Manual Initialization
We are going to modify "main", not "config" because injecting config doesn't allow dovjassinit.
For the sake of example, lets say you have a struct
and you want it initialized before everything else. We can do so like this:
This proves useful for code optimization (faster execution time) and management.
If the function is scoped you can simply use the compilation name. You're also likely going to need to call the World Editor generated functions.
There, you have full control over initialization.
For the sake of example, lets say you have a struct
JASS:
struct Data
static method customInit takes nothing returns nothing
endmethod
endstruct
JASS:
//! inject main
call Data.customInit()
//! dovjassinit
//! endinject
If the function is scoped you can simply use the compilation name. You're also likely going to need to call the World Editor generated functions.
JASS:
library Lib
private function onInit takes nothing returns nothing
endfunction
public function Init takes nothing returns nothing
endfunction
endlibrary
//! inject main
call Lib___onInit() // ___ private
call Lib_Init() // _ public
//! dovjassinit
// These initialize preplaced objects in the editor
call CreateRegions()
call CreateCameras()
call CreateAllItems()
call CreateAllUnits()
// call (non-vjass) custom triggers initialization functions
call InitCustomTriggers()
//! endinject
Notes
- JassHelper likely uses ExecuteFunc to avoid the thread crashing (OP Limit) or to be able to call functions from anywhere in the script.
- There can only be one inject main/config block per map, so don't include it in public resources. It's meant to allow the map maker to have more control, not give resources initialization priority.
- If you'd like you can use the config block instead of main for whatever reason.
- If you're hitting the OP limit with your custom initialization you could split them up across each function.
Last edited: