Data Scripting

From Off Grid Wiki
Jump to: navigation, search
Child Page(s)

Data Scripting

We saw in Mission Scripting that Data can have a script attached. Let's have a brief look at what can be done with this.

We have the following globals available:

Name Description
DeviceName The name of the device, to be used with Devices Lua API
macAddress The MAC address of the device
RunMissionCommand The function enabling access to mission script functionality

So for instance, let's say we want to set the Device state to Amok when the data is received:

10 Devices.SetAmok(deviceName, true);

or trigger an objective to be complete?

14 RunMissionCommand('Mission.CompleteObjective(mission.objectives.ourObjective)')


But what if we wanted to do something... naughtier?

The data script runs on the same Lua virtual machine as the device. Which means it has access to everything scripted in the device. So we could, for instance, alter the gui

23 if device ~= nil then
24 	if device.gui ~= nil then
25 		if device.gui.buttons ~= nil then
26 			device.gui.buttons["hax"] =
27 			{
28 				name = "Hax",
29 				desc = "Hacky McHackface",
30 		 		onClick = function()
31 		 			print("This works")
32 		 		end,
33 		 		subButtons = {
34 		 			hackButton = {
35 		 				name = "Enable",
36 						desc = "",
37 						onClick = function()
38 							Devices.SetAmok(DeviceName, true)
39 						end,
40 		 			},
41 				}
42 			}
43 		end
44 	end
45 end

A few things to note here. Before any variable is referenced, it must be checked against nil. This is known as defensive programming. The reason for this is that, in Off Grid, data can be sent to any device. So writing a piece of data that specifically attacks one device will likely break if sent to another (and not in a fun way). The other point is that it is much safer to write code that adds things, than it is to write code that changes things; and writing code that removes things is riskier still. Be sure you've thought through what will happen to other parts of the game when you're writing data scripts.

Remember... Data is your most powerful weapon!