• You've discovered RedGuides, an EverQuest multi-boxing and scripting community 🧙‍♀️⚙️. We want you to play several EQ characters at once, come join us and say hello! 👋

  • A TLP without truebox has thawed (Very Vanilla ready)
    Frostreaver

Question - Lua Programming Question

Joined
May 31, 2022
RedCents
4,210¢
Hi,

I'm writing a program to configure display options automatically. I need help with how I'm organizing my data, and how I'm using it.

The data I'm using is saved in two Lua tables; data for static values, and settings for user configured (can be changed, saved and loaded).
Not all settings are Boolean, I'll come back to this in the second part of the question.

The format is like this:
[CODE lang="Lua" title="Declare / Set table data"]local data = {}
local settings = {}

settings.showPCNames = {
foreground = true,
background = false
}
settings.showNPCNames = {
foreground = true,
background = false
}

[BGCOLOR=initial]data.showPCNames = {[/BGCOLOR]
window = "OptionsWindow",
child = "ODP_PCNamesCheckBox"
}
data.showNPCNames = {
window = "OptionsWindow",
child = "ODP_NPCNamesCheckBox"
}
[/CODE]
Now, I'm wanting to execute the changes iteratively:
Currently I have:
[CODE lang="Lua" title="Execute change"]mq.TLO.Window('OptionsWindow').DoOpen()

if (mq.TLO.Window(data.showPCNames.window).Child(data.showPCNames.child).Checked() ~= settings.showPCNames.foreground) then
mq.TLO.Window(data.showPCNames.window).Child(data.showPCNames.child).LeftMouseUp()
end[/CODE]
I've done iteration with key value pairs previously, but that doesn't work for this.
Basically, I'm trying to iterate through the tables ensuring data.value and settings.value are synchronised.

Part two, some settings and data are drop downs, so I'd need to separate these from the table and place them in their own iteration.
[CODE lang="Lua" title="ComboBox Example"]settings.loadScreenCombobox = {
foreground = 1,
background = 0
}

data.loadScreenCombobox = {
window = "OptionsWindow",
child = "ODP_LoadScreenCombobox"
}

if (mq.TLO.Window(data.loadScreenCombobox.window).Child(data.loadScreenCombobox.child).GetCurSel() ~= settings.loadScreenCombobox.foreground) then
mq.TLO.Window(data.loadScreenCombobox.window).Child(data.loadScreenCombobox.child).Select(settings.loadScreenCombobox.foreground)
end
[/CODE]
I cant currently see a way through it that isnt long winded; add an index to both tables, merge the tables into a fresh table at runtime and iterate through referencing essentially this.key.value for each iteration of the code block.

Or, have I got myself into a cul-de-sac and need to redo the data structures?
If this were another language, I'd approach it using either arrays of objects, or multi-dimensional arrays that can be iterated in a consistent order.

Open to any discussion that will increase my understanding of Lua to solve this problem.
 
Last edited:
Update for those following along at home ...

Some progress; been looking through documentation and stackoverflow for insights around nested tables and dynamically referencing keys.
I did find this post Using a variable as a key to call a value from a table, which led me to test this:
[CODE lang="Lua" title="Test Referencing Table Keys using variables"]local data = {}
local settings = {}

settings.showPCNames = {
foreground = true,
background = false
}

data.showPCNames = {
window = "OptionsWindow",
child = "ODP_PCNamesCheckBox"
}

local state = 'foreground'
local key = 'showPCNames'

print(settings.showPCNames[state])
print(settings[key][state])
print(data[key].window)[/CODE]
Results in
[CODE title="Results"]true
true
OptionsWindow[/CODE]
This should solve my issue as I can then iterate through the 1st level of the able to key the keys
[CODE lang="Lua" title="Iterate through table"]for key in pairs(settings) do
print(key)
end
[/CODE]
Which results in the output of the first level keys from the nested table. I can also use the key variable as a reference to retrieve data as above.
 
actually, pairs and ipairs return keys/values

pairs works on associative tables (dict/map format):
Lua:
local keyValue = {
    a = 1,
    b = 2,
    c = 3
}

for key, value in pairs(settings) do
    printf("%s: %s", key, value)
end

ipairs works on array tables:
Lua:
local arrayTable = { 1, 2, 3 }

for index, value in ipairs(arrayTable) do
    print("%d: %s", index, value)
end
 
Last edited by a moderator:
also, you can access a table two ways:

Lua:
local data = {
    a = 1,
    b = 2
}

print(data.a) -- prints 1
print(data["a"]) -- also prints 1

-- therefore you can do:
local key = "a"
print(data[key])
 
Last edited by a moderator:
actually, pairs and ipairs return keys/values

pairs works on associative tables (dict/map format):
Code:
local keyValue = {
    a = 1,
    b = 2,
    c = 3
}

for key, value in pairs(settings) do
    printf("%s: %s", key, value)
end

ipairs works on array tables:
Code:
local arrayTable = { 1, 2, 3 }

for index, value in ipairs(arrayTable) do
    print("%d: %s", index, value)
end
Yes, that's the thing I'm after.
Using pairs to return keys from the first level of a nested table, which I then treat (cognitively) as a property to reference the second level of key value pairs of multiple tables.

pairs is working for me to get the first level keys, whereas ipairs was not.
I appreciate the explanation as to the why of the behaviour.
 
Question - Lua Programming Question

Users who are viewing this thread

Back
Top
Cart