It looks like you're new here. If you want to get involved, click one of these buttons!
Hello all,
I'm dealing with quite a strange problem here. I'm trying to add a function to vec4()
, but Codea's giving me a hard time with naming the function. Here is an example:
function setup()
m = getmetatable(vec4())
m.rotate = function(v,n)
return n*(v.x+v.y+v.z+v.w)
end
p = vec4(1,2,3,4)
print(p:rotate(5))
end
This gives the error "attempt to call method 'rotate' (a number value)". But when I try the name 'trace', it works fine. Other names that give the same error are 'gaga', 'rot' and 'rotplane', while the names 'haha' and my own name 'kjell' both work fine. This seems like random behavior to me, am I overseeing something? All these function names should work, shouldn't they?
Comments
I don't think you need to fiddle with the metatable values for this. Have you tried something like this?
@SkyTheCoder actually @Kjell is right in his assumption to use
getmetatable
Here's a similar question and an answer: http://codea.io/talk/discussion/comment/19708/#Comment_19708You can't use a method name beginning with an
r
for avec4
. Norb
,g
, ora
. It's becausevec4
s are alsocolor
s sor
,g
,b
, anda
are reserved for the four components of thevec4
when thought of as a color. Due to how they are implemented underneath, any method that simply begins with anr
is interpreted asr
. I guess it's a speed thing.@LoopSpace nice observation and good point
@LoopSpace Ah, wow, I never thought of that, although I read about it in the reference a couple of days ago. It's supposed to double as a color vector, right? Anyway, thank you very much for making this clear!