• Hail, we're RedGuides. We want you to play six (yes, 6!) EverQuest characters at once. We have scripts and a friendly community to help, please join our playstyle and say hello! 👋
  • We've added LLM sections to our Terms & Rules and Resource Rules, for everyone's sanity & in-game safety.
    Read more
    a. Do not replace your voice with an LLM. This goes for your posts and your scripts. Drafting and editing with a tool is fine, but don't take you out of what you share.
    b. Do not replace your EQ character's voice with an LLM. We don't want scripts that speak over /ooc, /shout or other public channels. This has been an unwritten rule that's kept casino scripts off the site for decades, but we're now compelled to make it clear: keep automated communication private.

    Why Does RG Review Scripts?
    Nearly every LLM-written Lua script we see is making your character do impossible things, and rarely in a way that's beneficial to you. We've seen heavy server hammering, /target over impossible distances when /nav spawn would passively accomplish the same thing, a spawn search that fires 20+ times per second, and other pointless actions that are likely annoying GMs.

    You are welcome to use LLMs as a tool to structure, draft, and edit your work, but please read critically before hitting /lua run

Question - Lua Programming Question

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:
Declare / Set table data
Lua:
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"
}
Now, I'm wanting to execute the changes iteratively:
Currently I have:
Execute change
Lua:
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
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.
ComboBox Example
Lua:
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
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:
Test Referencing Table Keys using variables
Lua:
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)
Results in
Results
Code:
true
true
OptionsWindow
This should solve my issue as I can then iterate through the 1st level of the able to key the keys
Iterate through table
Lua:
for key in pairs(settings) do
    print(key)
end
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