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

[Metatables] Dynamically generating functions

Started by Lupus590, 01 January 2017 - 12:53 AM
Lupus590 #1
Posted 01 January 2017 - 01:53 AM
Basically, I'm trying to pseudo-dynamically recreate the turtle API for when you can't run code on a turtle.

Here's a link to the fake API creator:
https://github.com/CC-Hive/Toetle/commit/2b4fcd311e082c18904d2554f72429a686b1de6f

My test code is this:

turtle.toetle.suck(true)
print(turtle.suck())

the error: "attempt to call nil" on line 1
which means I can't do it the way I thought I could, so I'd like you guys to give me some pointers on how to do this

The todo section of the readme may help if you don't understand what I'm trying to achieve: https://github.com/C...-implementation

I want to avoid a big init function.
Edited on 02 January 2017 - 12:38 PM
valithor #2
Posted 01 January 2017 - 03:15 AM
Return a blank function from your toetle index metamethod: return function() end

When you do turtle.toetle.suck(true) nil is returned by the metamethod, so it is calling nil.

edit:

1000 posts :D/>
Edited on 01 January 2017 - 02:17 AM
H4X0RZ #3
Posted 01 January 2017 - 07:21 AM
also, why is newindex telling the user that the function is not initalized? newindex is not called when reading a field which isn't there; newindex only gets called when setting a new value in the table.
Lupus590 #4
Posted 02 January 2017 - 11:33 PM
I think I've done what you have said but I'm still not getting it to work, same error on the same line

edited code, only change to test code is dofile on the 'API' (last time 'API' was called through shell)
https://github.com/CC-Hive/Toetle/commit/1701badfa23d524435f7d0ebe0986eca16daedd5