Difference between revisions of "MDE ScriptHook: Script Table"

From Nomad DB
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{Page_MDE}}
 
Every [[MDE ScriptHook: Script|Script]] has its own ''Script Table''. It returns a '''Lua table''' that can be used for two things:
 
Every [[MDE 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
 +
# Access config options
 
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.
 
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.
 
{{Warning|For Watch Dogs 2 ScriptHook, use [[WD2 ScriptHook: Script Table|Script()]] instead.}}
 
{{Warning|For Watch Dogs 2 ScriptHook, use [[WD2 ScriptHook: Script Table|Script()]] instead.}}
Line 26: Line 28:
 
print(ScriptHook.CurrentScript().my_variable)
 
print(ScriptHook.CurrentScript().my_variable)
 
print(ScriptHook.CurrentScript():PrintMyVariable())
 
print(ScriptHook.CurrentScript():PrintMyVariable())
 +
</syntaxhighlight>
 +
 +
== Config Options ==
 +
With ScriptHook version 1.2, it is possible to save config options into a JSON file. The options are stored persistently, which means that they are still available even after a reboot. The supported Lua types are:
 +
* Numbers
 +
* Booleans
 +
* Strings
 +
 +
'''Usage:'''
 +
<syntaxhighlight lang="lua">
 +
local script = ScriptHook.CurrentScript()
 +
 +
script:SetConfigOption("key", 123)
 +
print(script:GetConfigOption("key")) -- 123
 +
 +
-- Other types
 +
script:SetConfigOption("test-str", "test")
 +
script:SetConfigOption("test-num", 123124)
 +
script:SetConfigOption("test-bool", false)
 +
</syntaxhighlight>
 +
The example above generates a ''user-config.json'' in the scripts directory with the following contents:
 +
<syntaxhighlight lang="json">
 +
{
 +
    "key": 123.0,
 +
    "test-bool": false,
 +
    "test-num": 123124.0,
 +
    "test-str": "test"
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 32: Line 62:
  
 
== Related Pages ==
 
== Related Pages ==
* [[MDE ScriptHook: Scripts]]
+
* [[ScriptHook: Script]]
 
* [[MDE ScriptHook: Events]]
 
* [[MDE ScriptHook: Events]]
  
 
[[Category:MDE]]
 
[[Category:MDE]]
 
[[Category:MDE ScriptHook]]
 
[[Category:MDE ScriptHook]]

Latest revision as of 19: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
  3. Access config options

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())

Config Options

With ScriptHook version 1.2, it is possible to save config options into a JSON file. The options are stored persistently, which means that they are still available even after a reboot. The supported Lua types are:

  • Numbers
  • Booleans
  • Strings

Usage:

local script = ScriptHook.CurrentScript()

script:SetConfigOption("key", 123)
print(script:GetConfigOption("key")) -- 123

-- Other types
script:SetConfigOption("test-str", "test")
script:SetConfigOption("test-num", 123124)
script:SetConfigOption("test-bool", false)

The example above generates a user-config.json in the scripts directory with the following contents:

{
    "key": 123.0,
    "test-bool": false,
    "test-num": 123124.0,
    "test-str": "test"
}

Event Handlers

Check out this guide about Event Handling.

Related Pages