• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Lua] Checking if something is Nil breaks triggers?

Status
Not open for further replies.
Level 2
Joined
Mar 31, 2020
Messages
12
I'm trying to set the Conditions to a trigger (full trigger at bottom, it is not completed). The issue is with this piece of it.
Code:
            if CraftingRecipes[i][1] == nil then
                print("Spell Cast Not Craft Spell")
                return false
            end
I am trying to use this section to detect when a table has ended.

if CraftingRecipes[1] is actually nil though, the trigger breaks and breaks hard. The trigger exits completely out of the while loop and never hits the next line of code or return false. Is this normal behavior? Is there an easier way to see how long a table is? (Google tells me no, Lua has no built-in ability to check the length of an array)

I wasn't planning on this table being dynamic, but I wanted to set it up in such a way that I wouldn't have to change a variable all the time to know the size of the table, or make an overly large table and go through the blank entries while processing spells that aren't on the table.

If the spell is on the table, it reaches the Actions and stops at print("Doing Actions") (haven't really crafted that yet, actions is still in puesdocode stage at this point)

Full Trigger
Code:
function CraftItemInitalize()
    local i = 0
    local trigger = CreateTrigger()
 
    local TriggerConditions = function()
        print("Spell Cast!")
        local i = 1
        local spell = GetSpellAbilityId()
        print("Spell Ability ID Get?")
        print("SpellID: "..spell)
        while true do
            if CraftingRecipes[i][1] == nil then
                print("Spell Cast Not Craft Spell")
                return false
            end
            print(i)
            if CraftingRecipes[i][1] == spell then
                print("Crafting Recipe "..i.." Cast")
                return true
            end
            i = i + 1
        end
        print("Something Went Wrong")
        return false
    end
    local TriggerActions = function()
        local i = 1
        local unit = GetTriggerUnit()
        local player = GetOwningPlayer(unit)
        local recipe = {}
        print("Doing Actions")
        while true do
            if CraftingRecipes[i][1] == nil then
                print("Something Went Terribly Wrong")
                recipe = CraftingRecipes[1]
                break
            end
            if CraftingRecipes[i][1] == spell then
                print("Crafting Recipe "..i.." Casting now")
                recipe = CraftingRecipes[i]
                break
            end
            i = i + 1
        end
    end
 
Last edited:
You can check if the table is nil before trying to access it's index.

Lua:
if CraftingRecipes[i] ~= nil and #CraftingRecipes[i] >= 1 and CraftingRecipes[i][1] == spell then
    print("Spell Cast Not Craft Spell")
    return false
end
However if you plan on only having a single spell associated with a recipe you might want to use the ability id as the table index for O(1) lookup.

if CraftingRecipes[1] is actually nil though, the trigger breaks and breaks hard. The trigger exits completely out of the while loop and never hits the next line of code or return false. Is this normal behavior?

If you want more specific info about why a piece of code is failing you can wrap it in an xpcall and catch the error in a handler function.

Is there an easier way to see how long a table is? (Google tells me no, Lua has no built-in ability to check the length of an array)

You can check the length with the # operator.

Lua:
print(#CraftingRecipes)
 
Level 2
Joined
Mar 31, 2020
Messages
12
Alright thanks! I should be able to design a decent system if I can reference the table length. I didn't know about xpcall, that should help a lot with future troubleshooting ^^. So far I've just been pushing out print statements to detect when the code crashes.

I'm not so concerned about keep sanity checks in the code once its working correctly. Maybe not best practice but WC3 is already not the most optimized system to be working in and I don't want to slow down with unnecessary calls :p

Hmm, indexing by the spell id may make this a lot more efficient but I need to know if the entry is nil first.
 
Last edited:
Status
Not open for further replies.
Top