DeckerdJames
Warlord
- Joined
- Nov 1, 2019
- Messages
- 277
For Lua/Civ modding novices like myself:
When the UI and GamePlay script files are processed, as the game loads, the game engine runs the things that are not inside a function declaration immediately.
Things that run immediately are usually things like event subscriptions
and any function calls, such as the Initialize function call
function declarations do not run until they are called.
Function declarations start with the word -> function <- and terminate with the word -> end <-
Something that a programmer/modder has to be aware of is that a function must be declared before it can be executed. That is why the initialize function call is usually at the end of the script file.
This will give a runtime error:
When Initialize is called, myFunction() has not been created yet, so when myFunction() is called, the Lua error will say it expected a function but got nil.
That is why the format of the script file is normally ordered like this:
The main this is that all the functions that the Initialize() function will call have been declared before they are called.
My experience is limited, but this is what I have discovered about Initialize functions. If any experts see something wrong with this, please, feel free to correct this.
When the UI and GamePlay script files are processed, as the game loads, the game engine runs the things that are not inside a function declaration immediately.
Things that run immediately are usually things like event subscriptions
Code:
Events.UnitMoved.Add( OnUnitMoved ) -- this line runs immediately
and any function calls, such as the Initialize function call
Code:
Initialize()
function declarations do not run until they are called.
Function declarations start with the word -> function <- and terminate with the word -> end <-
Code:
function myFunction()
end
Something that a programmer/modder has to be aware of is that a function must be declared before it can be executed. That is why the initialize function call is usually at the end of the script file.
This will give a runtime error:
Code:
function Initialize()
myFunction() -- call this function and execute the code
end
Initialize() -- call and execute the Initialize function
function myFunction()
print("This is my function")
end
When Initialize is called, myFunction() has not been created yet, so when myFunction() is called, the Lua error will say it expected a function but got nil.
That is why the format of the script file is normally ordered like this:
Code:
function myFunction()
print("This is my function")
end
function Initialize()
myFunction() -- call this function and execute the code
end
Initialize() -- call and execute the Initialize function
The main this is that all the functions that the Initialize() function will call have been declared before they are called.
My experience is limited, but this is what I have discovered about Initialize functions. If any experts see something wrong with this, please, feel free to correct this.
Last edited: