How to skip the first element of the array

Godot Version

I use Godot 4.2.1

Question

In my game, I use code to change the sprite using a sprite sheet through an index. I would like to leave a different index for the first element of the snake, so that when changing the sprite (pressing the space bar). All elements were changed except the first one. How can I implement this?

snake code example:

extends Panel
static var index : int = 0
func _process(_delta):

variate_texture()
func variate_texture():

$Sprite2D.region_rect.position.x = index * $Sprite2D.region_rect.size.x

main code example:

func _process(_delta):
if Input.is_action_just_pressed(“select_color”):
snake[0].index += 1
if snake[0].index > 2:
snake[0].index = 0

https://github.com/ScelCoding/Question/tree/main - a link to the github project with my question.

0 is the first element, if you start with 1 then you will have skipped the first element.

Or do you mean sprite[0] isn’t your first sprite image? If so you could use a constant integer array to keep the order (or just reorder them).

I’d also recommend you use a better variable name than just ‘index’ E.g. SpiteIndex. You might know what that’s the index of now, but in a few weeks/months/years? Even if this isn’t a project you plan on looking at again, it’s a good habit to get into.

1 Like