Skip to content

Gotchas

Ruin0x11 edited this page Feb 18, 2021 · 3 revisions

Due to the nature of Lua as a dynamic programming language, there are a lot of errors you could cause by subtle bugs. This page collects a few of them.

Shadowing methods on an object

This is really easy to do if you're not careful.

local player = Chara.player()

player.mod = "bionics.infared_mod"

-- ...

player:mod("pv", 50, "add") -- error!

Here, mod is predefined as a part of the IModdable interface, which IChara uses, but by assigning a concrete property with the name mod onto the object, that new field will be used instead.

This is both a limitation of Lua and the way object-oriented programming is built on top of it using __index, and additionally how OpenNefia uses prototypes instead of typical class-based inheritance.

This kind of error could be mitigated by using something like tl, which would typecheck everything and detect if you attempt to reassign a known method on the object. However, at the time of writing tl doesn't yet support a lot of the things like first-class types, separate integer and float types, optional types, and functions that dynamically index tables by strings that would be necessary to use adopt for the engine.

Accidentally mutating table references

Consider this code.

data:add {
   _type = "putit_color",
   _id = "flaming_hot",
   
   color = { 230, 130, 130 }
}

local putit = Chara.create("elona.putit")
putit.color = data["my_mod.putit_color"]["my_mod.flaming_hot"].color

putit.color[1] = 255

In the future, entries in data could become read-only after they're defined.