Jump to content




Code Not Working(Attempt To Index A Nil Value)


  • You cannot reply to this topic
5 replies to this topic

#1 jay5476

  • Members
  • 289 posts

Posted 02 September 2013 - 01:59 AM

hi guys, im wondering if it is possible to do something
in my button api
code can be found here: http://pastebin.com/WBGQ1PFa
I want to make it so the user does not have t set the metatable of there button to mainButton

edit: changed my question title now irrelevant

Edited by jay5476, 02 September 2013 - 02:35 AM.


#2 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 02 September 2013 - 03:06 AM

What line does it occur on?

e.g: program:52:attempt to index ? (a nil value)

#3 jay5476

  • Members
  • 289 posts

Posted 02 September 2013 - 03:43 AM

it doesn't anymore I changed my question

View Postjay5476, on 02 September 2013 - 01:59 AM, said:

edit: changed my question title now irrelevant
I also have solved this myself now


#4 Yevano

  • Members
  • 376 posts
  • LocationUSA

Posted 02 September 2013 - 09:49 AM

View Postjay5476, on 02 September 2013 - 01:59 AM, said:

I want to make it so the user does not have t set the metatable of there button to mainButton

You could make a function which creates a new class for you given your own constructor. Example usage of such a function:

local myButtonSubclass = class(mainButton, function(self, x, y, w, h)
	mainButton.init(self, x, x + w, y, y + h)
end)

It could be implemented as

function class(extend, constructor)
	local newClass = { }
	newClass.init = constructor
	setmetatable(newClass, extend)
	function newClass.new(self, ...)
		local self = setmetatable({ }, {__index = newClass})
		constructor(self, ...)
		return self
	end
	return newClass
end

I do something similar in my SEE runtime. You can of course implement this in different ways to suite your needs.

#5 jay5476

  • Members
  • 289 posts

Posted 03 September 2013 - 04:20 AM

thanks for help anyways I might release this soon the soloution I found was to do this
in api:
function create(buttonTable)
return setmetatable(buttonTable,mainButton)
end
and in my test program
os.loadAPI("button")
theButtonIUse = {}
button.create(theButtonIUse)
it seems to work fine as long as buttonTable is a legitimate table

#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 03 September 2013 - 04:51 AM

View Postjay5476, on 03 September 2013 - 04:20 AM, said:

it seems to work fine as long as buttonTable is a legitimate table
Make it
function create( t )
  return setmetatable( type(t) == "table" and t or {}, mainButton )
end
that way if they supply a table it's used, or else a fresh table is returned





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users