Mucker
Mucker

Reputation: 387

LUA executing code in wrong order - Could be Tabletop Simulator related

I am fairly new to LUA and still learning. So far I haven't came across any async features, but my code seems to be behaving this way. It is really bizarre. I have the following function:

function chgAttacker(attacker)
    local stats = getModelstats(attacker)
    Attacker = stats
    local WS = stripTextFormatting(stats[2])
    local BS = stripTextFormatting(stats[3])
    local Attack = stats[7]
    local weaponStrength, armorPen =  getWeapons(attacker)
    UI.setAttribute("model_BS", "text", BS) -- set BS skill on UI
    UI.setAttribute("model_WS", "text", WS) -- set WS skill on UI
    UI.setAttribute("model_attacks", "text", Attack) -- set attacks skill on UI

end

The function works as you might expect if I remove the line which has getWeapons(attacker) (which I'll explain next). It is very basic, it just gets a string, parses it then stores it in the stats variable. The last 3 UI lines then update 3 values in my UI based those stats. You'll have to believe me when I say, that this works perfectly.

Where the problem starts is when I add the getWeapons(attacker) above, it also updates these UI values (well it actually resets them to some defaults). But, the last 3 lines are then obviously meant to overwrite whatever getWeapons(attacker) set. But it doesn't. Let's say the starting UI BS value is 1, and getweapons() updates it to 2, then UI.setAttribute("model_BS", "text", BS) updates it to 3; here is exactly what I observe: it starts as 1, then (very briefly) updates to 3, then updates to 2.

It is as if it is behaving in an async manner. Has anyone seen this before or can advise how I can make LUA wait until the getweapons() function finishes?

EDIT: Just fixed it myself, I was using the builtin wait function...But it seems, it doesn't actually wait. I removed this and now it works. Really strange because I had to add this in the first place because the UI couldn't update quick enough.

Upvotes: 0

Views: 169

Answers (1)

Mucker
Mucker

Reputation: 387

The issue was that I was using a Tabletop Simulator function called wait inside tagetweapons() (not shown). Example of how to use it below:

Wait.frames(
    function()
        print("Hello!")
    end,
    2
)

I had this set for 2 ticks for other reasons - when updating the UI it takes at least one tick, so I needed the above wait. However, this had the adverse affect of making it behaving like an async call. After removing the waits it worked as expected.

Upvotes: 0

Related Questions