Unexpected behaviour with sprite user vars
-
Run it first with the lines commented, you see what you'd expect "0.000000" x 2.
spr = createSprite() spr.pos = {} loop clear() printAt( 0, 0, spr.pos.x ) printAt( 0, 1, spr.pos[0] ) // This line causes an "Invalid variable access". // spr.pos.x += 1 // This line causes a hard crash! // spr.pos[0] += 1 update() repeat
If we change the initialisation to;
spr = [ .pos = {} ]
...then it works just fine as expected!
-
I didn’t know you could assign an empty vector?
Does it work if you do spr.pos = {0, 0, 0} instead?
-
I think all trailing zero's can be skipped (so not just the fourth one :) ). I've used {} several times and haven't had issues with it (other than that opacity defaults to 0).
To me this seems like a bug, but I could work around it in the following way:
spr = createSprite() spr.pos = {} loop clear() var pos = spr.pos printAt( 0, 0, pos.x ) printAt( 0, 1, pos[0] ) pos.x += 1 pos[0] += 1 spr.pos = pos update() repeat
It's not ideal, but I hope it helps :)
-
Oh yeah, I'd already worked around it - it was just a bit of a surprise to see the hard crash! :)
-
@pobtastic Thanks for the report. I can confirm that in the next patch the hard crash is fixed but also that for the time being
spr.pos
still cannot be manipulated in either of these ways (It's on our to-do list). PB's workaround is the intended way to do this currently- Pull out the variable from the sprite, manipulate it, and then put it back in the sprite. -
Use
spr.x
andspr.y
maybe ? OrsetSpriteLocation(spr, x, y)
Or am I taking the example a bit too literally? :P
-
Oh yeah, the point is to be able to manipulate a vector other than the sprites own properties. I actually found this ages ago, I just randomly fired up a version of Jet Set Willy I have and found it again. The issue there being that, the characters "walk across the frame" and you only change sprite x once the tracking x reaches
% 8 == 0
.Something like this;
// Work out the current frame based on the tracking "x". game.guardians[id].frame = ( int( game.guardians[id].pos.x ) >> 1 ) & 3 // Only update the sprite "x" on a byte boundary. if game.guardians[id].pos.x % 8 == 0 then game.guardians[id].x = game.guardians[id].pos.x endIf // Change frames based on movement, and not time. setSpriteAnimFrame( game.guardians[id], game.guardians[id].frame )