Go.get() on sprite constants

When I use go.get() to fetch a fragment constant from a sprite, it will always return the default vector set in the editor, even if I change it at runtime. However, if I fetch the vector components individually, the engine will return the correct values.

print(go.get("#sprite", "tint"))
-- ==> vmath.vector4(1, 1, 1, 1)
sprite.set_constant("#sprite", "tint", vmath.vector4(0.0))
print(go.get("#sprite", "tint"))
-- ==> vmath.vector4(1, 1, 1, 1) 
print(go.get("#sprite", "tint.x"))
-- ==> 0 
print(go.get("#sprite", "tint.y"))
-- ==> 0

Any idea why the engine would return updated components but not vectors?

1 Like

Hmm, sounds strange. @sven or @jhonny.goransson might know?

1 Like

Hi! We did some digging into this today and the TL;DR version is that getting the property of a sprite (or label) won’t return the actual component property in this case because of how sprites are stored in memory.

All sprite components are stored in a large continuous block of memory which is optimal for performance. The sprite component items in this memory space can be swapped around when sprites are deleted and created, so you can’t rely on physical addresses directly due to this memory rearrangement. In the vector4 case of go.get, the internal getproperty function for a sprite won’t return the actual component constant data since we can’t safely expose a pointer to the value so that’s why we return the material constant value since this will always be present during the lifetime of a game.

Now, in the single value case, the property system can safely return the actual data since its a single value and not reliant on any pointers. We’ve classified this as bug and I’ll add it to our tracker, but it’s not necessarily an easy fix :frowning: For now, a workaround would be to either use the “.x” constructs or keep track of the data outside of the property system…

5 Likes

Interesting. Though if the single value case works, maybe go.get() could just return a new vector4 instance with the individual components copied into it?

You could make a wrapper function for this use case yourself too.

2 Likes