#Weird bug on Sprite2D(?)

1 messages · Page 1 of 1 (latest)

dusk wind
#

i'm currently making the old snake game, and using Sprite2D and placing them in each tile to represent the snake's body. but when the snake gets longer, the sprites that taking a turn is bugging (for example, sprite that supposed to show turn from down to right, showing turn from down to left), and this only happened at the snake's turning point, never in the straight parts

at first i suspected there's something wrong with my code, i've checked, there's nothing wrong, but i decided to change it anyways, until 5 times, but still there's no change, still the same problems/bug, i'm really lost and not sure what caused this. is it a bug from the Sprite2D itself? i'm not really sure

if anyone has some lead at all, please, it would be hugely appreciated

hybrid holly
#

It's a bug in your code, but nobody else but you can solve it under the current circumstances.

#

(what would anyone else be able to say based on what you've provided?)

#

You could add print statements around the code you're nervous about and check that the values it sees are the ones you think it should have

#

That could let you work backwards to find the bug?

dusk wind
#

i can send it here if you want to see, i've checked multiple times, i just can't find anything wrong

#

i just tried to send it but i couldn't, maybe it's too long

#

that's part of the code i think relevant

hybrid holly
#

It doesn't include how you maintain the variables you're using to derive the directions, so there could be a bug there. Did you try my suggestion of printing the relevant data so you can review its work and seeing where it makes a different decision than you want it to?

dusk wind
#

i just did another check, and here's the result, the body turn 1.png is already correct, but the sprite shows something else, that's what i meant

#

this is what body turn 1 actually looks like

hybrid holly
#

Are those black edge backgrounds actually black, or transparent? Is it possible you're allowing snake overlap, and a later (or earlier...) node is drawn over a different one?

dusk wind
#

the background is tilemap layer

hybrid holly
#

Same question. You posted a standalone blue curve with a black background just now. Is the black background black, or is that an artifact of discord image posting and it's actually clear?

dusk wind
hybrid holly
#

That cannot answer a question about what the texture you're using is actually drawn as art

dusk wind
dusk wind
hybrid holly
#

It's ok, you answered it just now!

dusk wind
#

okay, so,, do you have like any idea at all..? i'm actually lost, but if you want to see the full code i can also send it here for more perspective

hybrid holly
#

I think it's worth printing the positions of the snake parts along with their image textures in that debug print; heck, all of their variables

#

Because I'm still somewhat convinced the problem is an "overwritey" one, where yes, you're doing the right thing, but you're also somehow doing the wrong thing; the graphics from the wrong thing are hiding the value you expect to see. I assume there is snake overlap, so proving there isn't would help

#

Or perhaps the wrong image is loaded, or a sprite is rotated, ...? (So: printing things to establish what's actually happening will help will narrow it down!)

dusk wind
hybrid holly
#

Path is fine

#

(though I suppose you should audit the paths to make sure, if you're concerned about that!)

dusk wind
#

oh yeah i've tried printing the position stored in array (in form of a tile), and the position is just that tile multiplied by TILE_SIZE, but i've never thought of printing the Sprite2D's position, so i will try it now

hybrid holly
#

Because of Godot's slightly wacky system, it can get confusing to talk about directions sometimes ("down" is positive y, whaat?!) so art and logic might drift if you do them with different attitudes (in art, down is not positive y...). So auditing the art and double checking that the art gets "down" in the same direction as the logic gets "down" is a good idea

dusk wind
#

okay so here's the result, the bug happened on the 6th and 12th

#

both are body turn 1.png but the visual shows body turn 2.png

#

another common thing i see is that sometimes the bug happening on sprites on multiples of 3, maybe coincidence, idk

hybrid holly
#

It seems to always be turn 1 that's wrong in every example so far iiuc?

#

It always draws turn 1 as turn 2 it seems?

#

Or sorry, frequently draws 1 as 2

dusk wind
hybrid holly
#

But it only seems to screw up 1s

dusk wind
#

this time it's 3 and 7

#

usually it's on multiples of 3 but like this it also hits 7

#

on number 3, it's body turn 4.png, but instead showing body turn 3.png

#

on 7 it's supposed to be turn 2 but shown turn 3 instead

hybrid holly
#

Where is this printing from, btw? Before the update, after the update, ...?

dusk wind
#

will it help to see the full code?

hybrid holly
#

Sure, go for it. Please post it in a paste bin or smth I'm on mobile

dusk wind
#

ohh yeah okay

hybrid holly
#

Line 129 updates the self tile, prev, and next tiles in order

#

That means it's using stale data since only some of the snake segments are updated (from head towards tail) when it runs

#

Go through and update all of the positions first

#

Then go through and derive all of the graphics from the new (stable) positions

dusk wind
hybrid holly
#

No, your bug is structural, there's no one line that's wrong. Line 129 is just where I noticed it 😄 Your algorithm goes snake section by snake section and updates the section, then picks the associated art by its neighbors

#

But this algorithm is wrong.

#

Because, since it goes section by section, it is using a stale representation of all sections that are more tail wards

#

Oh wait maybe I'm wrong

#

Since set_visuals doesn't take a parameter, so it's gotta be doing it's own loop, after walks loop is concluded

dusk wind
#

the update visual function is being called at the very end of the function, so all the data is already stable right?

#

will watching the gameplay recording help you understand more?

hybrid holly
#

No

#

(but it might help someone else! So if you've got it might as well post it!)

dusk wind
#

coz it feels like glitching than bugging imo, like it's really weird

#

okay i'll record it

hybrid holly
#

Silly question on line 174, shouldn't the name just be get_child_count or maybe that +1?

dusk wind
#

yeah lol i didn't realize i can use that

dusk wind
#

guess i have to treat this as happy little accident and start thinking about workaround instead, thanks lackhand for helping!