• Godot Help
  • Invalid get index 'global_position' (on base: 'null instance')

Hi I'm trying to do a game similar to Vampire survivors, and I was doing the code for do the enemies spawn at random positions, but when I start the game to see if is working, this error appears.
This is the code


1. extends Node2D
2. 
3. 
4. @export var spawns: Array[Spawn_info] = []
5. 
6. 
7. @onready var player = get_tree().get_first_node_in_group("player")
8. 
9. var time = 0
10. 
11. func _on_timer_timeout():
12. 	time += 1
13. 	var enemy_spawns = spawns
14. 	for i in enemy_spawns:
15. 		if time >= i.time_start and time <= i.time_end:
16. 			if i.spawn_delay_counter < i.enemy_spawn_delay:
17. 				i.spawn_delay_counter += 1
18. 			else:
19. 				i.spawn_delay_counter = 0
20. 				var new_enemy = load(str(i.enemy.resource_path))
21. 				var counter = 0
22. 				while counter < i.enemy_num:
23. 					var enemy_spawn = new_enemy.instantiate()
24. 					enemy_spawn.global_position = get_random_position()
25. 					add_child(enemy_spawn)
26. 					counter += 1
27. 
28. func get_random_position():
29. 	var vpr = get_viewport_rect().size * randf_range(1.1,1.4)
30. 	var top_left = Vector2(player.global_position.x - vpr.x/2, player.global_position.y - vpr.y/2)
31. 	var top_right = Vector2(player.global_position.x + vpr.x/2, player.global_position.y - vpr.y/2)
32. 	var bottom_left = Vector2(player.global_position.x - vpr.x/2, player.global_position.y + vpr.y/2)
33. 	var bottom_right = Vector2(player.global_position.x + vpr.x/2, player.global_position.y - vpr.y/2)
34. 	var pos_side = ["up", "down", "right", "left"].pick_random()
35. 	var spawn_pos1 = Vector2.ZERO
36. 	var spawn_pos2 = Vector2.ZERO
37. 	
38. 	match pos_side:
39. 		"up":
40. 			spawn_pos1 = top_left
41. 			spawn_pos2 = top_right
42. 		"down":
43. 			spawn_pos1 = bottom_left
44. 			spawn_pos2 = bottom_right
45. 		"right":
46. 			spawn_pos1 = top_right
47. 			spawn_pos2 = bottom_right
48. 		"left":
49. 			spawn_pos1 = top_left
50. 			spawn_pos2 = bottom_left
51. 	var x_spawn = randf_range(spawn_pos1.x, spawn_pos2.x)
52. 	var y_spawn = randf_range(spawn_pos1.y, spawn_pos2.y)
53. 	return Vector2(x_spawn,y_spawn)

Can someone help?

  • xyz replied to this.

    xyz
    At line 30 I think, but the error only shows as Invalid get index 'global_position' (on base: 'null instance')

    • xyz replied to this.

      Pomelo The editor should highlight the exact line. Lines in the posted code are not numerated so we don't see which line is line 30. You can't expect people to count lines in your code. So take a look at the editor and post the content of the problematic line.

        If line 30 contains the expression player.global_position, then the problem is that player is not a valid reference.

        Add this:

        func _ready() -> void:
                print_debug("_ready, player=", player)

        Add this at the beginning of func on_timer_timeout():
        print_debug("on_timer_timeout, player=", player)

        Add this at the beginning of func get_random_position():
        print_debug("get_random_position, player=", player)

        What is the output?

          DaveTheCoder What is the output?

          This may not even be executed because someone is likely calling get_random_position() prior to node being ready. Likely a _ready() of a sibling that initializes first. Or there is some mess up with unintentional signal connections. Hard to tell without seeing the scene structure.

          That's true, but you have to start somewhere.

          I edited my post to add some more print's to deal with those cases.

          Pomelo No lines highlighted

          Are you sure? Because every time an exception is thrown, the execution is stopped and the stop line is highlighted by a yellow arrow. The top entry in stack frames window will also specify at which line in which script the execution stopped. Double clicking on that will bring you to that line in the code editor.

          Ok, I just fixed it

          I had to add a

          func _ready():
          	player = get_node("/root/Main/Player")

          And it started working

          • xyz replied to this.

            Pomelo That means that your player node is not in the group "player", as your code it (unsuccessfully) trying to get a reference to it via the group membership: @onready var player = get_tree().get_first_node_in_group("player")