Invalid get index 'clothing variant' (on base: null instance') crashing

witterincwitterinc Posts: 2Member

I'm new to programming and to godot. I'm was creating my first simple game for a game jam but hit a wall because I was getting a crash that would happen at random. I'm spawning a collection of "babies" into a scene. Each baby will randomly select a clothing item to have on. I'm then creating an array of the babies by looking for the group name babies. I then have three nodes that are supposed to spawn a clothing and set the value of that clothing to match the next baby in the array. When that clothing item is dragged onto a baby it will queue_free() both of the baby and clothing item. While play testing I find that if I switching between selecting different clothing I will accidentally crash the game and i'm not sure why. Below is the code i'm using. Thanks to anyone who can help guide me.

  1. extends YSort
  2. onready var level = 7
  3. const BabyScene = preload("res://Babies/Baby.tscn")
  4. const ClothingItemScene = preload("res://ClothingItem.tscn")
  5. onready var ClothingNode1 = $Control/Clothing_Drag_Frame/Item1
  6. onready var ClothingNode2 = $Control/Clothing_Drag_Frame/Item2
  7. onready var ClothingNode3 = $Control/Clothing_Drag_Frame/Item3
  8. onready var SpawnRangeMinX = 260
  9. onready var SpawnRangeMaxX = 1800
  10. onready var SpawnRangeMinY = 450
  11. onready var SpawnRangeMaxY = 900
  12. onready var BabySpawnYsort = $Babies/YSort
  13. onready var Rand = RandomNumberGenerator.new()
  14. onready var SpawnedBabies = []
  15. onready var ClothingSpawnArray = []
  16. func _ready():
  17. Spawn_The_Babies()
  18. SpawnedBabies = get_tree().get_nodes_in_group("Baby")
  19. ClothingSpawnArray = SpawnedBabies
  20. clothing_spawn()
  21. func _process(delta):
  22. SpawnedBabies = get_tree().get_nodes_in_group("Baby")
  23. clothing_spawn()
  24. func Spawn_The_Babies():
  25. for i in range(0, level):
  26. var Baby = BabyScene.instance()
  27. Rand.randomize()
  28. var x = Rand.randf_range(SpawnRangeMinX, SpawnRangeMaxX)
  29. Rand.randomize()
  30. var y = Rand.randf_range(SpawnRangeMinY, SpawnRangeMaxY)
  31. Baby.position.x = x
  32. Baby.position.y = y
  33. BabySpawnYsort.add_child(Baby)
  34. SpawnedBabies = get_tree().get_nodes_in_group("Baby")
  35. func clothing_spawn():
  36. if ClothingNode1.get_child_count() == 0 and ClothingSpawnArray.size() !=0:
  37. #yield(get_tree().create_timer(0.4), "timeout")
  38. var SpawnItem1 = ClothingItemScene.instance()
  39. SpawnItem1.clothingItemVariant = ClothingSpawnArray[0].clothingVariant
  40. ClothingSpawnArray.remove(0)
  41. ClothingNode1.add_child(SpawnItem1)
  42. if ClothingNode2.get_child_count() == 0 and ClothingSpawnArray.size() !=0:
  43. #yield(get_tree().create_timer(0.4), "timeout")
  44. var SpawnItem2 = ClothingItemScene.instance()
  45. SpawnItem2.clothingItemVariant = ClothingSpawnArray[0].clothingVariant
  46. ClothingSpawnArray.remove(0)
  47. ClothingNode2.add_child(SpawnItem2)
  48. if ClothingNode3.get_child_count() == 0 and ClothingSpawnArray.size() !=0:
  49. #yield(get_tree().create_timer(0.4), "timeout")
  50. var SpawnItem3 = ClothingItemScene.instance()
  51. SpawnItem3.clothingItemVariant = ClothingSpawnArray[0].clothingVariant
  52. ClothingSpawnArray.remove(0)
  53. ClothingNode3.add_child(SpawnItem3)

Comments

  • MegalomaniakMegalomaniak Posts: 2,740Admin

    Just to make it a little more readable I'll repost the code with formatting.

    extends YSort
    
    onready var level = 7
    const BabyScene = preload("res://Babies/Baby.tscn")
    const ClothingItemScene = preload("res://ClothingItem.tscn")
    onready var ClothingNode1 = $Control/Clothing_Drag_Frame/Item1
    onready var ClothingNode2 = $Control/Clothing_Drag_Frame/Item2
    onready var ClothingNode3 = $Control/Clothing_Drag_Frame/Item3
    onready var SpawnRangeMinX = 260
    onready var SpawnRangeMaxX = 1800
    onready var SpawnRangeMinY = 450
    onready var SpawnRangeMaxY = 900
    onready var BabySpawnYsort = $Babies/YSort
    onready var Rand = RandomNumberGenerator.new()
    onready var SpawnedBabies = []
    onready var ClothingSpawnArray = []
    
    func _ready():
        Spawn_The_Babies()
        SpawnedBabies = get_tree().get_nodes_in_group("Baby")
        ClothingSpawnArray = SpawnedBabies
        clothing_spawn()
    
    func _process(delta):
        SpawnedBabies = get_tree().get_nodes_in_group("Baby")
        clothing_spawn()
    
    func Spawn_The_Babies():
        for i in range(0, level):
            var Baby = BabyScene.instance()
            Rand.randomize()
            var x = Rand.randf_range(SpawnRangeMinX, SpawnRangeMaxX)
            Rand.randomize()
            var y = Rand.randf_range(SpawnRangeMinY, SpawnRangeMaxY)
            Baby.position.x = x
            Baby.position.y = y
            BabySpawnYsort.add_child(Baby)
        SpawnedBabies = get_tree().get_nodes_in_group("Baby")
    
    func clothing_spawn():
        if ClothingNode1.get_child_count() == 0 and ClothingSpawnArray.size() !=0:
            #yield(get_tree().create_timer(0.4), "timeout")
            var SpawnItem1 = ClothingItemScene.instance()
            SpawnItem1.clothingItemVariant = ClothingSpawnArray[0].clothingVariant
            ClothingSpawnArray.remove(0)
            ClothingNode1.add_child(SpawnItem1)
    
        if ClothingNode2.get_child_count() == 0 and ClothingSpawnArray.size() !=0:
            #yield(get_tree().create_timer(0.4), "timeout")
            var SpawnItem2 = ClothingItemScene.instance()
            SpawnItem2.clothingItemVariant = ClothingSpawnArray[0].clothingVariant
            ClothingSpawnArray.remove(0)
            ClothingNode2.add_child(SpawnItem2)
    
        if ClothingNode3.get_child_count() == 0 and ClothingSpawnArray.size() !=0:
            #yield(get_tree().create_timer(0.4), "timeout")
            var SpawnItem3 = ClothingItemScene.instance()
            SpawnItem3.clothingItemVariant = ClothingSpawnArray[0].clothingVariant
            ClothingSpawnArray.remove(0)
            ClothingNode3.add_child(SpawnItem3)
    
  • witterincwitterinc Posts: 2Member

    Thank you.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file