Vectors always equal?! (SOLVED)

Hey!

I’ve set a simplest check, that if coordinates have changed, i.e. oldCoords ~= newCoords (both vector3), then run a function. So I’ve set oldCoords = newCoords on click, and after that click they are always equal even though newCoords depend on mouse cursor movement and oldCoords are changing only onClick.

Is it normal and once equal vectors are always equal, or something wrong with my code? O_o

Everything works with separate oldCoordsx and oldCoordsy.

1 Like

How are you setting the oldCoords vector? Make sure you do something like

oldCoords = vmath.vector3(newCoords)

This will create a new vector. Otherwise the value will be passed as a reference so will be the same.

6 Likes

Thanks! Seems like that’s the problem.

Maybe it’s some syntax rule? Cause it’s the second time when function like that (reference instead of value) makes me spend hours on figuring out issues I don’t understand. 8(

1 Like

No, no syntax rule. It’s the way Lua works. A vector is an object (userdata) and if you assign it to a variable it’s not going to be a copy but a reference to the same object. Many many languages behave exactly like this. Lua primitive data types (number, boolean, string) gets copied though, but that is also in line with most languages, at least for number and boolean.

3 Likes

Oh got it. I.e. number = otherNumber works, but table = otherTable should be made with a function.

Thanks! That’s definitely an important thing to know for newcomers.

Yeah, everyone learns this sooner or later. The Defold lua manual does mention it, but not in a very obvious, beginner-friendly way. (Actually it doesn’t mention it for vectors, just for tables and functions.)

The way to avoid wasting hours fighting with issues like this is to learn the basics first. Reading through Programming in Lua and trying out the example code yourself may sound boring but it will save you a lot of time and frustration. See the chapter on tables.

3 Likes

That’s… quite some reading to be made. :astonished: Much appreciated! Kind of rushed into Defold without knowing a thing about LUA.

Still, the progress made in only a month with Defold totally compensates those small obstacles.

4 Likes

I mean, what @ross.grams said is obviously true, but some time ago I also rushed into Defold without knowing anything about LUA and it worked out in the end, so it can be done. There were a few pitfalls I could have avoided, but yeah.

2 Likes

Oh yeah, I did it the same way! :smiley: And I got confused over exactly the same issue. Only now do I realize my mistake.

I don’t think anyone wants to learn the fastest, easiest way, it’s usually not very appealing. That’s 90% of the reason for teachers to exist: to make you do what you should do, but don’t want to!

2 Likes

I also jumped into defold the same way, and it took me 5 months to understand this very thing. :wink:
(Hey! Better late than never :stuck_out_tongue_closed_eyes:)

This is exactly what my physics teacher says :joy:

2 Likes

As a side note - this applies to LUA and Defold itself. I certainly don’t recommend jumping into fiddling with shaders like this, as I’m just finding out =D

1 Like