Pull a filename's index (int) from an array (SOLVED)

Godot Version

Godot 3.5


Hello there. I am trying to get the index of a filename in an array as my script uses it to determine what rooms to load next. I have tried find and bsearch and my array step never seems to go past one. I can’t figure out what I am doing wrong. I have a global function/singleton sorting filepaths into an array and then the scenes check themselves to see where they are in the array. So I need to find an occurrence of a filepath in an array and its position as an integer.

UPDATE: I figured it out. Just needed to compress my function a bit.

extends Node2D

var _filepath : String ## holder for filepath, can't fetch until ready
var _pos : int ## tracks current place in array
var _leftDestination : String ## left door next scene
var _rightDestination : String ## right door next scene

func _ready(): ## populate variables at enter
	print("Current position:"+str(_pos)) ## debugging command

func _population(): 
	_filepath = get_tree().current_scene.filename ## look for scene's filepath
	var _leftDoor = get_node("LeftDoorFinal") ## get doors
	var _rightDoor = get_node("RightDoorFinal")
	_pos = DirectorNode._finalRoomsArray.find(_filepath,0) ## find scene's filepath index in array
	var _leftpos = _pos-1 ## left door goes to last door in array
	var _rightpos = _pos+1 ## right door goes to next door in array

	if _leftpos > -1: ## if at beginning of array, left door goes to fixed start
		_leftDoor._myDestination = DirectorNode._finalRoomsArray[_leftpos]
	if _leftpos <= -1:
		_leftDoor._myDestination = "res://scenes/final_area/Final_Start.tscn"

	if _rightpos < 11: ## if at end or array, right door goes to fixed end
		_rightDoor._myDestination = DirectorNode._finalRoomsArray[_rightpos]
	if _rightpos >= 11:
		_rightDoor._myDestination = "res://scenes/final_area/Final_End.tscn"

did you verify the items in _finalRoomsArray are correct? below i verify m_array by printing the contents of the array.

extends Node2D
var m_array : Array = ["file1", "file2", "file3", "file4", "file5"]
func _ready():
	print (m_array)

I did, actually and the “place” in array. That was my first guess. But I did figure it out. My load order was wrong on the functions so I compressed them into one and that did it. Just a reminder not to ram so much into the ready() function. Thanks for responding!

1 Like