lua-users home
lua-l archive

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


On Mar 5, 2017, at 6:57 PM, "Soni L." <fakedme@gmail.com> wrote:
> Write a modding API where you use
> 
> get_inventory_item(something, slot) -- aka a "static non-replaceable function"
> 
> And "get_inventory_item" cannot be replaced and you don't call a function from "something" through it. So e.g. no
> 
> function get_inventory_item(something, slot)
>  return something.get_inventory_item(something, slot) -- aka an "object function"
> end
> 
> And no having a lookup table to map objects to functions.


function get_inventory_item(obj, slot)
  if obj.inv then
    return obj.inv[slot]
  end
end

Sorry couldn't resist :)

You probably want opaque objects (don't want the inventory table to be directly accessible on user side):

local inventories = {}

function get_inventory_item(obj, slot)
  local inv = inventories[obj]
  if inv then
    return inv[slot]
  end
end

:-)
 
Petri