lua-users home
lua-l archive

Re: Colon notation in object-oriented programming

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


On 05/03/17 02:09 PM, Petri Häkkinen wrote:
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
This is assuming all inventories store data as an array of slots.
What if an inventory wants to be a map of item type to item amount, and provide a slot-based interface for accessing those items? (No, we're not talking hypotheticals here. These are very much a thing in the world of Modded Minecraft.)
--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.

AltStyle によって変換されたページ (->オリジナル) /