Reputation: 766
I'm very new to lua, so please don't mind the novice question. I wasn't able to find my exact question here because of the weird wording.
I have the following table:
local DialogueMode = {
uniqueDialogue = {0, 3},
general = {1, 2},
job = {3, 5},
rumors = {4, 3},
annoyed = {5, 0},
pissed = {6, 0},
ignore = {7, 0}
}
And the following loop:
for k,v in pairs(DialogueMode) do
print(k .. ' ' .. tostring(isDialogueModeCompatible(playerTools, npc, k)))
end
And here's the if function:
local function isDialogueModeCompatible(playerTools, npc, dialogueMode)
print(tostring(dialogueMode) .. " " .. tostring(DialogueMode.uniqueDialogue) .. " " .. tostring(dialogueMode == DialogueMode.uniqueDialogue))
end
For uniqueDialogue, it prints out:
uniqueDialogue table: 4df7e3ad false
Why isn't dialogueMode == DialogueMode.uniqueDialogue working as I expect it should, and how do I make it work how I expect it? Where I'm checking if the dialogueMode is equal to the key in the table. I'm effectively trying to use the table as an enumerator, but this is giving me problems.
Thanks in advance!
Upvotes: 1
Views: 527
Reputation: 337
Again I'm not 100% sure what you're looking for, but maybe you can modify this code and work from there. This code loops through and checks to see if it can find an identical table within DialogueMode to the one it's currently printing. If the table isn't the same it won't print "true" next to the table.
Anyways, here's the code, I hope it'll be helpful to you in some way. Feel free to delete this answer if it isn't:
local DialogueMode = {
uniqueDialogue = {0, 3},
general = {1, 2},
job = {3, 5},
rumors = {4, 3},
annoyed = {5, 0},
pissed = {6, 0},
ignore = {7, 0}
}
local function isDialogueModeCompatible(playerTools, npc, dialogueMode)
for v, x in pairs(DialogueMode) do
if (DialogueMode[v] == dialogueMode) then
bool = true;
int = v;
break;
end
end
return(tostring(dialogueMode)..". Match Found: "..tostring(bool));
end
for k,v in pairs(DialogueMode) do
print(k .. ' ' .. tostring(isDialogueModeCompatible(playerTools, npc, v)));
end
Upvotes: 1
Reputation: 337
The question here is, what are you trying to print? Do you want it to just dump the table? If you want the function to recursively search through the table and print it out, that's more complicated than what you've got here, simply because of the nature of recursion.
Additionally, the reason dialogueMode == Dialoguemode.uniqueDialog
is returning false is because dialogueMode
was assigned the value k
, which is the string "uniqueDialog"
.
Dialoguemode.uniqueDialog
is the entire uniqueDialog
table, whereas dialogueMode
is just the key, the name of the table, converted to a string.
The code could look like:
--Not my code, credit here: https://gist.github.com/hashmal/874792
function tprint(tbl, indent)
if not indent then indent = 0 end
for k, v in pairs(tbl) do
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
tprint(v, indent+1)
else
print(formatting .. v)
end
end
end
tprint(DialogueMode)
Note that this might not work for all cases but for your table it does dump the entire table successfully. If you run into errors, check that github for a fix (or fix it yourself :D)
Here's the output I get when using your DialogueMode table:
pissed:
1: 6
2: 0
rumors:
1: 4
2: 3
general:
1: 1
2: 2
ignore:
1: 7
2: 0
annoyed:
1: 5
2: 0
job:
1: 3
2: 5
uniqueDialogue:
1: 0
2: 3
EDIT: Not sure what you're looking for exactly, this might not be it but hopefully it's helpful to you in some way.
Upvotes: 1
Reputation: 766
Apparently v stores the correct comparison.
I.E.
for mode, value in pairs(DialogueMode) do
print(tostring(value) .. " " .. tostring(DialogueMode.uniqueDialogue) .. " ")
end
Prints:
table: 140b90e8 table: 140b90e8
Upvotes: 0