How to go through an array of images one after another

Viewed 26

I'm quite new to programming and GDScript and wondering how to do something that I did think would be quite simple, but I'm struggling a bit!

I've loaded an array of images and I want it to go through each of these images one after the other each time a button is clicked and replace a sprite texture with that particular image.

Right now I can successfully get the sprite to change to any of the images if I put its array number in e.g. new_texture[0] or new_texture[3] etc., but I would like it to go through each of the images one after the other every time the user clicks the button. (And once it's got to the last image, go back to the first image again)

What am I missing?

Here is the code:

   extends Node

onready var my_sprite = get_node("/root/Game/Picture/Sprite")
var new_texture = [
    load("res://Art/Picture1.png"),
    load("res://Art/Picture2.png"),
    load("res://Art/Picture3.png"),
    load("res://Art/Picture4.png"),
    load("res://Art/Picture5.png"),
    load("res://Art/Picture6.png")
    ]

func _on_Arrow_pressed() -> void:
    my_sprite.texture = new_texture[0]
    
1 Answers

As you know, you can get the first image of the array like this:

new_texture[0]

Where new_texture is a variable that holds the array, and 0 is a constant (a literal, to be more precise) that indicates the position on the array.

So you can change the constant to get a different image, for example the fourth element is:

new_texture[3]

Now, that is fine if you want to change which element you get form one version to another of the game, since it requires to modify the code…


But you want which element from the array you get to change during the execution of the program. In other words, you don't want it to be constant, but variable.

So declare a variable that will hold which element you get:

var texture_index := 0

Use it instead of the constant:

func _on_Arrow_pressed() -> void:
    my_sprite.texture = new_texture[texture_index]

And now you can change which element from the array you get by changing the value of the variable, for example:

func _on_Arrow_pressed() -> void:
    my_sprite.texture = new_texture[texture_index]
    texture_index += 1

Here we are changing the value of texture_index so the next time this code executes it gets the next position.

But be careful to keep the value of the variable inside the bounds of the array to avoid errors. So, what do you want to do after it reaches the last element? Do you want it to loop? Well, we can check if we reached the last element with an if, and set a different value to the variable, like this:

func _on_Arrow_pressed() -> void:
    my_sprite.texture = new_texture[texture_index]
    texture_index += 1
    if texture_index == new_texture.size():
        texture_index = 0

I remind you that elements of the array go from the position 0 to the position array.size() - 1. For example, if the array has 10 elements, they are in the positions 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (from 1 to 9, there are 9 numbers, plus the 0 you have 10 numbers). So an array with 10 elements does not have an element in the position 10. So there is no element in the position array.size(), thus, when the index reaches that value it is out of bounds of the array, and we reset it to the first element (position 0).

There are, of course, variation of this approach.

Related