This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
mhiekuru's profile picture

cant access other table inside table prototype

Started by mhiekuru, 15 February 2016 - 04:10 PM
mhiekuru #1
Posted 15 February 2016 - 05:10 PM
I'm just new on using metatables and methods, can anyone tell me what I'm doing wrong here?
 object.prototype.body.x = test.x+5
this code at line 12, always returning an error saying test.x is nil
I'm guessing it cannot read the test table in the object.prototype. but whenever I print test.x it prints 4 and not returning nil


--should always be empty to always run newindex and index metamethods
object = {}
--proxy table
test = {}
-- create the prototype with default values
object.prototype = {}
object.prototype.x = 0
object.prototype.y = 0
object.prototype.width = 64
object.prototype.height = 64
object.prototype.body = {}
object.prototype.body.x = 2*test.x
--metatable
object.mt = {}
--if the index is in test return test else return prototype
object.mt.__index = function (table, key)
	if test[key] ~= nil then
		return test[key]
	else
		return object.prototype[key]
	end
end
--send value to proxy
object.mt.__newindex = function (self, index, value)
	test[index] = value
end

function new()
	function addMt (o)
		setmetatable(o, object.mt)
		return o
	end
	return addMt({})
end

w = new()
w.x = 4 --test.x = 4
print(w.body.x) --nil, suppose to return prototype value which should return 8 (test.x*2)

It's suppose to print 8 but its not working :(/>
KingofGamesYami #2
Posted 15 February 2016 - 07:07 PM
Let's break your program down, to explain what it's actually doing.


object = {} --#make a globally accessible table named object (hint: Global Variables = Bad) 
test = {} --#make another globally accessible table named test
object.prototype = {} --#make a subtable of object named prototype
object.prototype.x = 0 --#set x and y of this prototype to 0
object.prototype.y = 0
object.prototype.width = 64 --#set width and height to 64
object.prototype.height = 64
object.prototype.body = {} --#set body equal to a new table
object.prototype.body.x = 2*test.x --#set x equal to 2 * nil (hint: this causes an error)
--metatable
object.mt = {} --#make a table named mt
--if the index is in test return test else return prototype
object.mt.__index = function (table, key) --#set the index metamethod to this function
        if test[key] ~= nil then --#if the test table contains the key, return that value
                return test[key]
        else --#otherwise return object.prototype's value
                return object.prototype[key]
        end
end
--send value to proxy
object.mt.__newindex = function (self, index, value) --#when a value is set
        test[index] = value --#set that value in the test table
end

function new()
        function addMt (o)
                setmetatable(o, object.mt)
                return o
        end
        return addMt({})
end

w = new()
w.x = 4 --test.x = 4
print(w.body.x) --nil, suppose to return prototype value which should return 8 (test.x*2)

So where in all that did you ever reference the body table? When you initially set it equal to 2 * nil, and when you try to access it. It won't change when test.x changes. Ever.