Using matches[x] to reference a table

Post Reply
Rx-18-67
Posts: 1
Joined: Tue Mar 30, 2021 6:57 pm

Using matches[x] to reference a table

Post by Rx-18-67 »

I'm trying to make a script to keep track of my opponents' afflictions and what they're curing.

The basic script works fine. It goes through the list of afflictions until it finds the highest priority affliction the target currently has and removes it.

Code: Select all

local cure_priority = 1

function target_usedcure(cure)
local aff_check = opiate[cure_priority]
if
table.contains(target_affs, aff_check) == true
then 
table.remove(target_affs, tonumber(table.index_of(target_affs, aff_check)))
cure_priority = 1
else
cure_priority = cure_priority + 1
target_usedcure()

end
end

The problem is this part:

Code: Select all


function target_usedcure()
local aff_check = [b]opiate[/b][cure_priority]

The script only works if I specify which list it has to check in the script itself. Ideally, I'd like to run something like this:

Code: Select all


function target_usedcure(cure)
local aff_check = cure[cure_priority]

So I could make a trigger that runs target_usedcure(matches[4]) and automatically checks the correct table of afflictions. Nothing I've tried so far has worked. Either the alias I'm using to test the script doesn't work or the debug gives me an error message that Mudlet expected a table and got a string instead. Does anyone know if there's anything I can do to make the variable register as a table reference?

Delra
Posts: 1
Joined: Thu Apr 22, 2021 7:51 pm

Re: Using matches[x] to reference a table

Post by Delra »

Hey there! So, the trick is to create a nested table; for example:
Code: [show] | [select all] lua
cures = {
  opiate = {"paresis", etc},
  steroid = {"limpveins", etc},
  etc,
}
and then you can check
Code: [show] | [select all] lua
 cures[cure] 
to get your cure order table and go from there!


... that being said, with a for loop, your usecase could be as simple as:
Code: [show] | [select all] lua
for _, aff in ipairs(cures[cure]) do
  if table.contains(target_affs, aff) then
    local n = table.index_of(target_affs, aff)
    table.remove(target_affs, n)
    return
  end
end

(adjust appropriately but I hope that makes sense, please ask if you need more help!)
Last edited by Delra on Thu Apr 22, 2021 8:03 pm, edited 2 times in total.

Post Reply