How to cycle between three different resources upon button press?

Godot Version

4.5.1

Question

I have three different character speeds as .tres that i want to be loaded into a .gd upon a button press, cycling between them. I currently have my code set up to go from my ‘default’ movement speed to a ‘faster’ movement speed upon button press, but i’m not sure how to have it check which one is currently loaded and cycle in a specific order after that.

this is my default PlayerMovement.gd:

class_name PlayerMovementData
extends Resource

@export var speed = 100.0
@export var acceleration = 800.0
@export var friction = 1000.0
@export var jump_velocity = -300.0
@export var gravity_scale = 1.0
@export var air_resistance = 200.0

then this is in my player.gd script:

@export var movement_data : PlayerMovementData

	if Input.is_action_just_pressed("character_swap"):
		movement_data = load("res://FasterMovementData.tres")

Hi,

You first want to store your resources inside an array, allowing you to cycle between them. Just change your movement_data variable to an array, and change the loading code to load all 3 resources, in the order you want them.

Then, add an int variable that will act as the currently selected resource index. If an array has 3 elements, that index can be either 0, 1, or 2. When selecting the next element, increment that index by 1, and if the new index is higher than 2 (the maximum index in the array), then set it to 0 so that it cycles back to the first element.
Same for selection the previous element: decrease the index by 1, and check if its new value is less than 0. If it is, set it to the maximum array index (here, 2).

Let me know if you need more info or a code example.