· 5 years ago · Aug 29, 2020, 06:00 AM
1--[[ Lua code. See documentation: https://api.tabletopsimulator.com/ --]]
2
3--[[ The onLoad event is called after the game save finishes loading. --]]
4function onLoad()
5
6 --defining a table
7 table1 = {
8
9 --the string "value1" is a key, integer 3 is the value
10 ["value1"] = 3,
11 --the string "value2" is a key, integer 5 is the value
12 ["value2"] = 5,
13 -- the key is integer 8 is the value
14 [8] = "hello"
15 }
16
17
18 log(table1.value1)
19
20 --[[
21 line 17 means look in table1 and find the string key "value1" and return the value.
22 That value is then sent into the log function which prints it to the console
23 so in this case would log "3"
24
25 when you use dot syntax in lua it assumes it's looking for a string key as far as I'm aware.
26 If you want to specify the sort of key it's looking for. You use the square brackets
27
28 ]]--
29
30 log(table1["value1"]) --would log the exact same thing
31 log(table1[8]) --would log "hello" since it's looking for the thing keyed under the integer 8
32
33--[[
34 But since everything in lua is tables. You need to make intricate tables to organise a lot of data.
35 In fact it's common to have tables where the value of each keyed thing is another table. Tables in tables.
36
37 defined in code it'd look like
38]]--
39 tablesInTables = {
40
41 ["table1"] = {
42 ["value1"] = 3,
43 ["value2"] = 5,
44 [8] = "hello"
45 },
46 ["table2"] = {
47 ["value1"] = 9,
48 ["value2"] = 8,
49 [8] = "no",
50 ["function1"] = function() return "echo" end
51 }
52 }
53
54 --and to reference anything in this table you can think of it as any nested folder structure starting with the outmost layer
55
56 log(tablesInTables["table2"]["value2"]) --logs 8
57 log(tablesInTables["table1"][8]) -- logs "hello"
58 log(tablesInTables["table2"]["function1"]) --gets the function as data
59 log(tablesInTables["table2"].function1()) --and you can use the dot syntax to actually call functions if they are named.
60 --This function returns "echo" so the console would log echo
61
62--[[
63 so when we think of any tabletop simulator "class"/table it's just about working through the layers
64 in the tabletop player table player is the top layer, then within that is a table for each color keyed by their color name.
65 And within that table theres is a whole bunch of things stored all listed on the Api
66]]--
67
68 log(Player) --in tabletop this actually logs <LuaGlobalPlayer> since tts recognises that you're logging their Player object
69 log(Player["White"]) --logs <LuaPlayer>, but we know we can treat it like a table of properties
70 log(Player["White"]["host"]) --logs true if white is host
71
72
73 --this maybe could be visualised as
74
75 player = {
76 ["White"] = {
77 ["host"] = true,
78 --but of course not forgetting all of the other properties associated in whites table
79 ["seated"] = false --etc
80 },
81 -- or all the other player colors with similar tables
82 ["Blue"] = {
83
84 ["host"] = true,
85 ["seated"] = false --etc
86 }
87
88 --etc
89 }
90
91 --and with strings lua dosen't care if you use dot syntax or the square brackets
92 log(Player.White.host) --logs true if white is host
93
94 --in fact the APIs examples use a mix
95
96 log(Player["White"].host) --logs true if white is host
97
98 --[[
99 maybe this is easiest to understand and most in line with how the object is represented in the code they used to build the game
100 also using the dot syntax makes a lot of sense if half the things you're grabbing from it are functions you want to call which you need the dot for see line like line 59
101 ]]--
102
103 --most the "chapters/pages" on the scripting api are named the name of the table it wants you to look into.
104 --Obviously the most common are things like Object which is literally showing you the table returned whenever you have get a object reference like when you use getObjectFromGUID() https://api.tabletopsimulator.com/object/
105 --but then some stuff is randomly organised into it's own table like the timer class https://api.tabletopsimulator.com/timer/
106end