Array ending error

Godot Version

4.2.2

Question

clone node
if Input.is_action_pressed(“ui_accept”):
future_positions = get_parent().get_node(“player”).past_positions
global_position = future_positions[current_position]
current_position += 1
player node
if Input.is_action_pressed(“Record”):
past_positions.push_back(global_position)

when the clone node reaches the end of the array it says

and there isnt any errors so i dont know why its breaking

what im trying to do is record the players movements and then have the clone retrace those steps when a button is pushed so if you know how to get rid of it when it reaches the end please tell me.

I should mention that this isnt my code. i copied it from someone else who answered a similar question back in 2021

The error means you are indexing an array with a value that is not inside the array boundaries.

E.g. I have an array of 4 elements.

var array = [1,2,3,4]
var value1= array[0] # valid, the first element 
var value4 = array[3] # valid, the fourth element 
var value5 = array[4] # invalid error, the 5th element!?

is there anyway i can stop this?

This basically how you handle if there’s no more future positions in the array, you have to cancel what the player trying to do, currently player can just spam the “ui_accept” to make current positions overshoot the future positions total number of positions in it

one way to do it is by just clamp the current position before you fed it to global_positions

var max_index=clamp(current_position,0,future_positions.size()-1)
global_position = future_positions[max_index]
current_position=max_index+1

replacing it to above codes, it should limit the current_position index and doesnt overpass the future positions index count

that just put the clone on top of the player

ive fiddled with the code and now

var max_index=clamp(current_position,0,future_positions.size())

func _physics_process(_delta):

if Input.is_action_pressed("ui_accept"):

	future_positions = get_parent().get_node("player").past_positions
	global_position = future_positions[current_position]
	current_position += 1

and now it follows the players path

thank you so much this saved me hours

though the record input still breaks everythings

but i think if i do a if statement to check if the clone has reach the final position i might be able to stop it from breaking

ok so for whatever reason the max_index just doesnt work
i dont know why.

ive found that replaceing that with
future_positions[future_positions.size()-1]
and the var max_index is deleted cause its doing nothing

what im doing rn is trying to get the clone to go through the array instead of skipping to the end

well the max_index variable is not actually the max index of the array, i should have named it just index

var max_index=clamp(current_position,0,future_positions.size()-1)

this line of code should be clamping the current_position or index of future_positions to the max of future_positions array size

if you do this, then yeah, of course it’s always the last index

1 Like

ok that makes some sense

but why is this

global_position = future_positions[max_index]
current_position=max_index+1

sending the clone to the final value instead of going through the array?

because of this line
change it to

if Input.is_action_just_pressed("ui_accept"):

that still has the same problems
i think it has something to do with this part

current_position=max_index+1
cause unlike the original code the current position isnt increasing by 1

show me the current code, because it looks like you have been changing so much since i put the max_index one

clone node
var future_positions =
var current_position = 0
var max_index=clamp(current_position,0,future_positions.size()-1)

func _physics_process(_delta):

if Input.is_action_just_pressed("ui_accept"):
	
	future_positions = get_parent().get_node("player").past_positions
	global_position = future_positions[max_index]
	current_position = max_index+1

player node [it did use to have a if Input.is_action_pressed thing but for now it doesnt]
past_positions.push_back(global_position)

that doesnt look right
it should be looking like this instead:

var future_positions = []
var current_position = 0


func _physics_process(_delta):

	if Input.is_action_just_pressed("ui_accept"):
		future_positions = get_parent().get_node("player").past_positions
		var max_index=clamp(current_position,0,future_positions.size()-1)
		global_position = future_positions[max_index]
		current_position = max_index+1
1 Like

i changed it back to
Input.is_action_pressed

cause the just pressed was being weird but it is working now

thank you so much for help

is_action_pressed is not the way though, because when you are holding down the enter button or “ui_accept” it slide through the current array list

1 Like

thats what i want it to do

[though the weird thing that was happening was a me thing so the just_pressed also works]

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.