Difference between revisions of "WD2 ScriptHook: Script Table"

From Nomad DB
(Created page with "Every Script has its own ''Script Table''. It returns a Lua table that can be used for two things: # Storing variables for the Script # Callback / E...")
 
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Every [[WD2 ScriptHook: Script|Script]] has its own ''Script Table''. It returns a Lua table that can be used for two things:
+
{{Page_WD2}}
 +
Every [[WD2 ScriptHook: Script|Script]] has its own ''Script Table''. It returns a '''Lua table''' that can be used for two things:
 
# Storing variables for the Script
 
# Storing variables for the Script
 
# Callback / Event Handlers
 
# Callback / Event Handlers
Using the Event Handlers is also the only way to properly unregister event listeners from the Game's event system.
+
Using the Event Handlers is also the only way to properly unregister event listeners from the Game's event system.<noinclude> We strongly recommend not using global variables or functions in Scripts, since these won't be properly garbage collected and could cause conflicts with other Scripts.
 +
{{Warning|For Mafia Definitive Edition, use [[MDE ScriptHook: Script Table|ScriptHook.CurrentScript()]] instead.}}
 +
== Script() in Action ==
 +
This example illustrates how you can avoid using global variables & functions and how the Script() function can be used instead. '''Syntax:'''
 +
<syntaxhighlight lang="lua">
 +
Script()
 +
</syntaxhighlight>
 +
==== main.lua ====
 +
<syntaxhighlight lang="lua">
 +
local script = Script()
 +
script.my_variable = 1234
  
<noinclude>
+
function script:PrintMyVariable()
 +
print(self.my_variable)
 +
end
  
 +
include("test.lua")
 +
</syntaxhighlight>
 +
 +
==== test.lua ====
 +
<syntaxhighlight lang="lua">
 +
-- Prints 1234 twice
 +
print(Script().my_variable)
 +
print(Script():PrintMyVariable())
 +
</syntaxhighlight>
 +
 +
== Event Handlers ==
 +
Check out [[WD2 ScriptHook: Events|this guide]] about Event Handling.
 +
 +
== Related Pages ==
 +
* [[ScriptHook: Script]]
 +
* [[WD2 ScriptHook: Events]]
 +
 +
[[Category:WD2 ScriptHook Lua]]
 
</noinclude>
 
</noinclude>

Latest revision as of 20:31, 9 November 2020

Every Script has its own Script Table. It returns a Lua table that can be used for two things:

  1. Storing variables for the Script
  2. Callback / Event Handlers

Using the Event Handlers is also the only way to properly unregister event listeners from the Game's event system. We strongly recommend not using global variables or functions in Scripts, since these won't be properly garbage collected and could cause conflicts with other Scripts.

For Mafia Definitive Edition, use ScriptHook.CurrentScript() instead.

Script() in Action

This example illustrates how you can avoid using global variables & functions and how the Script() function can be used instead. Syntax:

Script()

main.lua

local script = Script()
script.my_variable = 1234

function script:PrintMyVariable()
	print(self.my_variable)
end

include("test.lua")

test.lua

-- Prints 1234 twice
print(Script().my_variable)
print(Script():PrintMyVariable())

Event Handlers

Check out this guide about Event Handling.

Related Pages