Difference between revisions of "MDE ScriptHook: Script Table"

From Nomad DB
Line 36: Line 36:
  
 
[[Category:MDE]]
 
[[Category:MDE]]
[[Category:ScriptHook]]
 
 
[[Category:MDE ScriptHook]]
 
[[Category:MDE ScriptHook]]

Revision as of 12:50, 6 October 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 Watch Dogs 2 ScriptHook, use Script() instead.

ScriptHook.CurrentScript() in Action

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

ScriptHook.CurrentScript()

main.lua

local script = ScriptHook.CurrentScript()
script.my_variable = 1234

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

include("test.lua")

test.lua

-- Prints 1234 twice
print(ScriptHook.CurrentScript().my_variable)
print(ScriptHook.CurrentScript():PrintMyVariable())

Event Handlers

Check out this guide about Event Handling.

Related Pages