I am building a section that takes an array of rooms (filepaths) and shuffles them for randomness. My shuffling and arrays are working fine but I would like to find each level's filepath so it can figure out where it is in the array. Essentially, trying to find the index of an entry in an array for sorting. I have been trying bsearch() to no avail. On Version 3.5 currently. The "DirectorNode" is a global that holds and shuffles the data and it's working fine.

Essentially, the index will determine what the next scene is to load for the doors. So I just need that number... but it is not working. For some reason I can't get the index number and anytime I search for filepath I am getting totally off variables. I have also tried find() with no luck.

extends Node2D


var _filepath : String
var _digit : int
var _leftDestination : String
var _rightDestination : String


func _ready():
	_population()
	_filepath = str(self.filename)
	_digit = DirectorNode._finalRoomsArray.bsearch(_filepath)
	print(_digit)


func _population():
	var _leftDoor = get_node("LeftDoorFinal")
	var _rightDoor = get_node("RightDoorFinal")
	
	var _leftDigit = int(_digit-1)
	var _rightDigit = int(_digit+1)


	if _leftDigit > -1:
		_leftDoor._myDestination = DirectorNode._finalRoomsArray[_leftDigit]
	else:
		_leftDoor._myDestination = "res://scenes/final_area/Final_Start.tscn"
	
	if _rightDigit < 11:
		_rightDoor._myDestination = DirectorNode._finalRoomsArray[_rightDigit]
	else:
		_rightDoor._myDestination = "res://scenes/final_area/Final_End.tscn"

    SnapCracklins I figured it out. I guess I was doing too much at the ready because when I put it all in one function, it's working fine now. My friendly reminder to not shove so much into ready(). 😂

    For whoever needs it:

    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
    	_population()
    	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"
    SnapCracklins changed the title to Finding a file's position in an array (SOLVED) .