Code: Select all
list = {}
item = {
new = function(__self, id)
self.id = id
table.insert(list, __self.id())
end;
spawn = function()
-- spawn code
end
}
Is this possible? I want to make my system as flexible as I can.
Code: Select all
list = {}
item = {
new = function(__self, id)
self.id = id
table.insert(list, __self.id())
end;
spawn = function()
-- spawn code
end
}
Code: Select all
local Item = {}
local item_metatable = { __index = Item}
Item.new = function(data)
local instance = data
return setmetatable(instance, item_metatable)
end
Item:use = function()
self.count = self.count - 1
end
Code: Select all
local Item = require 'item'
local heart = Item.new({count = 3})
heat:use()
Code: Select all
local Factory = {
['switch'] = {
['item'] = function( data )
local instance = Item.new(data)
return instance
end
}
}
function Factory.new( entity, data )
if Factory.switch[entity] then
return Factory.switch[entity]( data )
end
end
return Factory
Code: Select all
Factory = require 'factory.lua'
list = {}
table.insert(list, Factory.new('item', {id = 'heart'})
Code: Select all
list[id] = item
Code: Select all
list = {}
item = {}
item.__index = item --The index metamethod is defined for our item class. This means that any table whose metatable is the item class will now be able to "inherit" it's methods.
function item:new(id)
local item_object = setmetatable({}, item) --This creates a table and sets up the item class as the parent to our newly instanced item_object.
item_object.id = id
table.insert(list, item_object)
return item_object
end
function item:spawn()
--spawn code
end
heart = item:new(1)
Code: Select all
item_mt = {}
--This sets up item_mt as a lookup.
--__index is a metafunction which is only invoked if an index does NOT exist in the main table.
item_mt.__index = item_mt
--We can define a function on the base.
function item_mt:base_cool_function()
print("That was awesome!")
end
--Think of this as a sort of "item manager"
item = {
--We'll use this table to hold registered items for later instancing.
registered = {},
new = function(id)
assert(not item.registered[id], "This item has already been registered!")
--Create a new table supplied with the item id and point __index to itself.
local obj = setmetatable({id = id}, item_mt)
obj.__index = obj
item.registered[id] = obj
return obj
end,
spawn = function(id)
local item_class_mt = assert(item.registered[id], "This item does not exist!")
local obj = setmetatable({}, item_class_mt)
return obj
end
}
--Register a basic item.
test_item = item.new("basic_item")
--Give it some base functionality. This works because of the use of __index.
function test_item:cool_function()
print("Hey! That's pretty good!")
end
--We can now spawn a new item instance and call that function we defined.
local my_test_item = item.spawn("basic_item")
my_test_item:cool_function()
--We can call a function further up the chain of inheritance..
my_test_item:base_cool_function()
local their_test_item = item.spawn("basic_item")
--These are not the same instances.
print(their_test_item == my_test_item)
Code: Select all
-- items.txt
heart
circle
square
triangle
star
-- main.lua
items = {}
function love.load()
file = "items.txt"
if love.filesystem.isFile(file) then
for line in love.filesystem.lines(file) do
local object = item(line)
table.insert(items, object)
end
end
end
Code: Select all
item = {}
item.__index = item
function item:new(id)
local item_object = setmetatable({}, item)
[...]
return item_object
end
Code: Select all
item = {} -- interface
itemMT = { __index = item } -- metatable (falls back to the interface)
function item:new(id)
local item_instance = setmetatable({}, itemMT) -- this is where the magic happens
[...]
return item_instance
end
Given your code, you only need to change 2 lines to do what you want:LeNitrous wrote: EDIT: I can use a separate text file to append all items inside it. Now I need to know how to call a specific item. I can do it via items[1]:use() yet I need it to be like items["heart"]:use(). Any way how?
Code: Select all
-- main.lua
items = {}
function love.load()
file = "items.txt"
if love.filesystem.isFile(file) then
for line in love.filesystem.lines(file) do
local object = item(line) -- object is the ID for the item
items[object] = object -- this will put on the table, a key 'heart', and a value 'heart'
end
end
end
Code: Select all
items[object] = { count = 5 }
-- so you can do
if items.heart.counter > 0 then
---use
end
Users browsing this forum: Ahrefs [Bot] and 4 guests