
```ini
; the main part of a motto
[MeterString]
DynamicVariables = 1
Meter = String
; you can regard measure section as a function and it will return a string value
; [] denotes a bang which is a kind of function represented by section
; & denotes the section is a customized section but a built-in section
Text = [&Script:GetText()]
; a meter has different types, and a type of meter has different styles
; however, the MeterStyle is customizable, and it is represented by a section
MeterStyle = TextStyle
FontSize = 17*#Scale#
; to decide the position of the meter
X= 0R
Y= (8*#Scale#)R
LeftMouseUpAction = !CommandMeasure Script "MyUpdate()"
; the secondary of a motto
[MeterSecondaryString]
DynamicVariables = 1
Meter = String
Text = [&Script:GetSecondaryText()]
FontSize = (14*#Scale#)
MeterStyle = TextStyle
X = 0R
Y = (-15*#Scale#)R
LeftMouseUpAction = !CommandMeasure Script "MyUpdate()"
```
```lua
function GetText()
return JSON[ID].Text
end
function GetSecondaryText()
return JSON[ID].SecondaryText
end
MyUpdate = function()
ID = math.random(1, #JSON)
end
function Initialize()
-- *** retrieve the content of a file ***
-- io is a class in lua and read funtion in it creats a instance as return value
local relativePath = "motto.json"
-- io.open could not directly resolve to relative path and you have use
-- SKIN:MakePathAbsolute to convert the relative to the absolute.
-- through colon we can infer that SKIN is an object and the object has to be a parameter
-- in the function and the SKIN is an unique object representing current skin
local absolutePath = SKIN:MakePathAbsolute(relativePath)
local file = io.open(absolutePath, "r")
if file then
-- if you need to have a object parameter, then use colon
-- if you need not to have a object parameter, then use dot
-- string "*a" is a kind of control parameters and means to
-- read whole content of the file
-- beaware that a local variable has its scope limited to the
-- block where it is declared. a block is the body of a control structure
Content = file:read("*a")
-- like c++, a opened file needs closing after using
file:close()
-- the second parameter's type is number and means the error's level
-- if it is 2, the error output will include the number column where
-- the error occurs.
else error("The file doesn't exits.", 2)
end
-- *** parse the content of json ***
-- dofile is to include external library or lua snippet into the current script
-- GetVariable is a function of skin aiming to retrieve variables in the skin
-- @ represents the directory of current skin resource folder
-- the return value is an object indicating a lua file
cjson = dofile(SKIN:GetVariable("@")..'Script\\json.lua')
-- decode is a function in the json.lua file
JSON = cjson.decode(Content)
-- *** Initialize which motto is chosen ***
-- math is a object and you do not need to use data in math object so
ID = math.random(1, #JSON)
end
```
Statistics: Posted by zIa1yBX5 — Today, 4:23 pm — Replies 0 — Views 9