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

table.insert problem ?

Started by Kouksi44, 22 August 2014 - 02:35 PM
Kouksi44 #1
Posted 22 August 2014 - 04:35 PM
Hey Forum,

I am still trying to get my button api running.

I am already able to draw the buttons and let them trigger actions and all the basic stuff.

But inside my button.createButton(…) method I am adding the button in a separate table where all the buttons are stored.

Seems like no big deal but now I´ve come across a problem that I really do not understand:

So far I only added some values inside my buttonlist like this :


  table.insert(button_list,{self.name,self.intx,self.inty,self.width,self.action})

Now I thought it would be easier to simply add the whole button table inside my list like that:

table.insert(button_list,self)

Because self is a table I dont need that {} brackets (well at least I think so?).

Anyway: When I click my button and the function detectClick() tries to find out if a button was clicked, he fails to get the values out of my self table inside of my list.

So thats the code:

function detectClick(posX,posY)
for i=1,#button_list do
if
posX<=button_list[i][2]+#button_list[i][1] and
posX>= button_list[i][2] and
posY <= button_list[i][3]+button_list[i][4] and
posY >=button_list[i][3]
then
return button_list[i][1]
  
end
end
end

At the point where I try to get the length of button_list[1] (the name is stored at the first index) the console tells me"cant get length of nil" .

When I add the values normally like I said at the beginning everything works fine, only if I insert the whole self table it fails ?

It would be a lot easier for me just to add the self table but currently I am clueless why this doesn´t work ?!

Please help me :)/>

Kouksi44
Bomb Bloke #2
Posted 22 August 2014 - 05:23 PM
You're generating a table that has quite a different structure to the original.

Let's say you've got "self", and it looks like this:

self["name"] = "somename"
self["intx"] = 4
self["inty"] = 10
self["width"] = 20
self["action"] = someFunc

If you do this:

newTable = {self.name,self.intx,self.inty,self.width,self.action}

Then the resulting table will have an entirely different structure to the original:

newTable[1] = "somename"
newTable[2] = 4
newTable[3] = 10
newTable[4] = 20
newTable[5] = someFunc

Your "detectClick" function is built to work with that latter structure, not the former. Try changing eg "#button_list[1]" to "#button_list.name".
Edited on 22 August 2014 - 03:24 PM
Kouksi44 #3
Posted 22 August 2014 - 05:47 PM
Thank you thats it !