Page 1 of 1

Problem printing table with metatable

Posted: Wed Feb 05, 2014 12:44 am
by 20047m
So, I created a string library called Twine:
https://github.com/kurtjarvi/twine

Everything seems to work fine, but when I do this:

Code: Select all

a = twine('Hello, World!')

function love.draw()
	love.graphics.print(a,0,0)
end
'a' is a table. Now, I know that you can't print tables in lua, but I have a '__tostring' metamethod that allows me to print it. So, if I say print(a), it works, but if I tried love.graphics.print, it errors out. Can anyone help me with this? It should be working, as far as I can tell.

Re: Problem printing table with metatable

Posted: Wed Feb 05, 2014 12:50 am
by slime
This should work:

Code: Select all

function love.draw()
   love.graphics.print(tostring(a),0,0)
end

Re: Problem printing table with metatable

Posted: Wed Feb 05, 2014 12:53 am
by Kingdaro
I'm not sure of all what arguments can be passed to graphics.print (numbers and strings have worked for me, tables and nil haven't, which makes sense). You're probably better off just tostring()ing it before printing.

Also, for the record, a lot of lua's string methods can be performed on strings directly using the colon syntax:

Code: Select all

a = "some string"

a:sub(1,4) --> some
a:upper() --> SOME STRING
a:rep(10) --> some stringsome stringsome stringsome ...

Re: Problem printing table with metatable

Posted: Wed Feb 05, 2014 1:06 am
by ejmr
Kingdaro wrote:I'm not sure of all what arguments can be passed to graphics.print (numbers and strings have worked for me, tables and nil haven't, which makes sense)
IIRC only strings and numbers work since love.graphics.print() ultimately calls the C function lua_tolstring(), and that only accepts strings and numbers.

Re: Problem printing table with metatable

Posted: Wed Feb 05, 2014 2:38 am
by 20047m
Kingdaro wrote: Also, for the record, a lot of lua's string methods can be performed on strings directly using the colon syntax:
]
Yes, I knew that! Thanks :) I know it is redundant, but why include only the JavaScript string functions, why not include them all?

Also, I have just tried making a ".val()" function so "a.val()" would return it's real value, but then I get a stack overflow error. Any idea? tostring(a), like smile suggested, also gives me a stack overflow error:
Image

Re: Problem printing table with metatable

Posted: Wed Feb 05, 2014 3:40 am
by ejmr
20047m wrote:Also, I have just tried making a ".val()" function so "a.val()" would return it's real value, but then I get a stack overflow error. Any idea? tostring(a), like smile suggested, also gives me a stack overflow error:
The __index() metamethod access 'self.value', which invokes the __index() metamethod, which accesses 'self.value', which invokes... And so on. You can instead use rawget(self, "value") to access 'self.value' without triggering any metamethod.

Re: Problem printing table with metatable

Posted: Wed Feb 05, 2014 4:06 am
by 20047m
Woops, I didn't think about that! Thanks!