lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mar 4, 2017, at 3:24 PM, "Soni L." <fakedme@gmail.com> wrote:
> But can you do
> 
> block_in_world = world:get_block(pos)
> if block_in_world.inventory then
>  block_in_world:inventory.set_item(1, item)
> end
> 
> (for a minecraft-like game)
> 
> Keep in mind each block in the world may store inventory differently (or even store it elsewhere!), so you can't just assume all inventories will behave the same (that is, you can't use a static function unless you're calling a dynamic function, in which case why not call the dynamic function directly?).

Sure, if you really need polymorphism, then you need some sort of dynamic dispatch. Luckily first class functions in Lua make this really simple.

Petri