Problem printing table with metatable

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
20047m
Prole
Posts: 13
Joined: Sun Nov 03, 2013 5:44 am

Problem printing table with metatable

Post 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.
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Problem printing table with metatable

Post by slime »

This should work:

Code: Select all

function love.draw()
   love.graphics.print(tostring(a),0,0)
end
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Problem printing table with metatable

Post 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 ...
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Problem printing table with metatable

Post 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.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
20047m
Prole
Posts: 13
Joined: Sun Nov 03, 2013 5:44 am

Re: Problem printing table with metatable

Post 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
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Problem printing table with metatable

Post 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.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
20047m
Prole
Posts: 13
Joined: Sun Nov 03, 2013 5:44 am

Re: Problem printing table with metatable

Post by 20047m »

Woops, I didn't think about that! Thanks!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests