Godot Version
4.2.1 stable official
Question
I’m stumped five ways to Friday.
My typed array [String] is not resizing as I add data, evident by an Invalid Set Index error when setting any index not explicitly defined upon declaration. So for example, I’ve defined index 0 to “null”; index 1 onwards will throw the error.
Here’s the relevant bit of source code. Sorry about the long names:
var obstaclePathStart: String = “res://resource/graphics/obstacle/”
PLACEHOLDER
var obstaclePathPlaceholder: String = “placeholder/”
var obstacleStringPlaceholder: Array[String] = [“null”]
var obstacleTexturePlaceholder: Array[Texture2D] = [null]
var obstacleBitmapPlaceholder: Array[Object] = [null]
var obstacleCountPlaceholder: int = 0func loadAssets() → void:
obstacleCountPlaceholder = scanFolder(obstaclePathStart + obstaclePathPlaceholder, obstacleStringPlaceholder)
assignTexture(obstacleStringPlaceholder, obstacleTexturePlaceholder, obstaclePathStart + obstaclePathPlaceholder, obstacleCountPlaceholder)
assignBitmap()func scanFolder(path: String, fillStringArray: Array) → int:
var directory: Object = DirAccess.open(path)
var count: int = 0
if directory:
directory.list_dir_begin()
var fileName: String = directory.get_next()
while fileName != “”:
if directory.current_is_dir(): # Skips any sub directories
print("Loading from: " + fileName)
else:
if fileName.get_extension() == “png”:
print(fileName)
fillStringArray[count] = fileName
count += 1
fileName = directory.get_next()
return countfunc assignTexture(from: Array, to: Array, path: String, items: int) → void:
for i in items:
to[i] = load(path + from[i])
print(path + from[i])func assignBitmap() → void:
pass
What I’ve tried:
-
Using a dynamic instead of typed array (same errors)
-
Manually adding each element i.e.
var obstacleStringPlaceholder: Array[String] = [“null”, “null”, “null”, “null”, “null”, “null”]
(Actually works, but is orcish and should not be the way to do this)
-
Using the array itself instead of passing it to the function (same error)
-
Using the resize() function on the array to make sure those indices exist i.e.
var obstacleStringPlaceholder: Array[String] = [“null”]
obstacleStringPlaceholder.resize(100)
But it throws the following error: Unexpected “Identifier” in class body.
What on earth is going on? I must be missing something obvious.
Edit for clarity: both scanFolder() & assignTexture() are having this Set Index Error problem when trying to load the obstacleStringPlaceholder and obstacleTexturePlaceholder arrays respectively. They’re not dynamically resizing, which, okay whatever fine (probably not performant anyway), but as you can see above .resize() is not working for me either.