Schilcote
Schilcote

Reputation: 2394

Attempt to perform arithmetic on table value

I'm maintaining someone else's Lua code, and Lua is not my preferred language. This is probably a complete noob question, but I can't seem to find the answer on Google or SO...

The following code

if !(v.LastHealth == v:Health()) then
    local newColor = {}
    newColor.r = v.orgColor.r - (v.orgColor.r - curColor.r) --This is the line the error occurs on
    newColor.g = v.orgColor.g - (v.orgColor.g * clrPercent)
    newColor.b = v.orgColor.b - (v.orgColor.b * clrPercent)
    newColor.a = v.orgColor.a - (v.orgColor.a - curColor.a)

    v:SetColor( newColor )

produces the error

 attempt to perform arithmetic on field 'r' (a table value)

orgColor (maybe, not totally certain- v.orgColor may be an outdated thing) and curColor are tables that have entries (Uh. I think. bla.x is the same as bla[x] in Lua, right?) r, g, b, and a. Apparently I can't do math on things that come from tables? Should I stow all these values in local variables before working with them? That doesn't seem right.

EDIT:

Printing v.orgColor gives table: 0x40390080, which I assume means it exists and is a table. What's odd is, v.orgColor.r gives another table! That sounds like the cause.

Upvotes: 1

Views: 7831

Answers (1)

Schilcote
Schilcote

Reputation: 2394

As it turns out, v.orgColor was not, as I had presumed, set by the host program, but was set by the same script as the code sample is from. There was an API change that made a function that used to return four RGBA values instead return a table of those same values; the old code set orgColor.r to the table containing those values, causing the error.

Moral of the story, I suppose, is that you should always make sure you know what's setting the variables you're working with.

Upvotes: 1

Related Questions