Image question

When one does a tostring(image), you get something that look like this:

"Image: width = 100, height = 100 (raw_width = 100, raw_height = 100)"

What do the “raw” values refer to?

(Incidentally, what’s the markup for delimiting inline code like the tostring() above?)

Raw values refer to the actual image size (@2x images for retina devices are resized to behave like non-retina images). Also

Text blah blah `code`

Makes:

Text blah blah code

Thanks.

To make an object display custom text when tostring() is called on it, that object must have a metatable with the __tostring metamethod implemented. Incidentally, since Codea classes are metatables for their instances, you can implement __tostring on your class, like so:


MyClass = class()

function MyClass:init(value)
    self.value = value
end

function MyClass:__tostring()
    return "MyClass.value: " .. tostring(self.value)
end

local inst = MyClass(10)
print(inst)

@toadkick, I see I need to learn more about metatables and metamethods. But don’t all Codea usertypes have metatables with them implementing __tostring? I wrote myself a table prettyprinter and would like to create suitable representations of the Codea types, but I’m not even sure what all of them are. I don’t want to overwrite each Codea type’s “__tostring” functions but did want “condensed” representations that’ll fit in the console window.

@starblue:

“But don’t all Codea usertypes have metatables with them implementing __tostring?”

As far as I can tell, color, vec2, vec3, vec4, touch, and matrix have metatables that implement __tostring; that’s exactly how they’re able to customize their output when passed as arguments to the tostring/print functions. Other types may too, but those are the only ones I’ve tested so far.

“I don’t want to overwrite each Codea type’s ‘__tostring’ functions but did want ‘condensed’ representations that’ll fit in the console window.”

Well, overwriting those metamethods is really the only way to achieve that. Frankly it should be okay…the only real risk you run I think is if you are using someone else’s code who may be doing exactly the same thing. I can’t imagine many people (if any) actually do that though, at least not in code they share/distribute.