Error with calculating random position around a player

Godot Version

4.2.1 Stable

Question

Hard workers, I was thinking about spawning lamps around the player, but it seems like it’s going to be quite liquid.

I’m making a script in the main node of the level with the following variables:

var num_lamps = 10
var radius = 50

var lamp_scene = preload("res://Resources/Scenes/Level1Component/LightLamp.tscn")

var lamps = []

Next, in the process function, I check the distance between the player and each lamp
And I check whether I need to create a new one

func _process(delta):
  for lamp in lamps:
    if lamp.global_position.distance_to(Global.player.global_position) > radius:
      lamp.queue_free()
      lamps.erase(lamps.find(lamp))
  
  if lamps.size() < num_lamps:
    spawn_lamps()

the spawn_lamps() function calculates a random position around the player (here I most likely did nonsense)
Creates a lamp instance, positions it and adds it to the level

func spawn_lamps():
  var direction = Vector3(randi_range(-1, 1) , 1.854 , randi_range(-1, 1))
  var position = Global.player.global_position + direction * radius
  
  var lamp = lamp_scene.instantiate()
  if lamp != null:
    lamp.global_position = position
    add_child(lamp)
    lamps.append(lamp)

I received an error:

Invalid get index 'global_position' (on base: 'previously freed').

How to fix this error? And did I write the formula for calculating the position of the lamp correctly?

It seems like I made some changes, but the lamps are not created, I’m obviously making very strange code

func _process(delta):
	var remaining_lamps = []
	for lamp in lamps:
		if lamp and not lamp.is_queued_for_deletion():
			if lamp.global_position.distance_to(Global.player.global_position) > radius:
				lamp.queue_free()
			else :
				remaining_lamps.append(lamp)
	lamps = remaining_lamps
	
	if lamps.size() < num_lamps:
		spawn_lamps()

E 0:00:01:0659 LevelScript.gd:55 @ spawn_lamps(): Condition “!is_inside_tree()” is true. Returning: Transform3D()
<Исходный код C++>scene/3d/node_3d.cpp:343 @ get_global_transform()
<Трассировка стека>LevelScript.gd:55 @ spawn_lamps()
LevelScript.gd:47 @ _process()

Hello o/
I feel like I might be missing part of the puzzle here. What is “lamp” in this context? It’s defined in your first message but not in the last. Try printing it out to console and see what comes up.

Sorry, here are all the pieces of this “puzzle”

func _process(delta):
	var remaining_lamps = []
	for lamp in lamps:
		if lamp and not lamp.is_queued_for_deletion():
			if Global.lamp.global_position.distance_to(Global.player.global_position) > radius:
				lamp.queue_free()
			else :
				remaining_lamps.append(lamp)
	lamps = remaining_lamps
	
	if lamps.size() < num_lamps:
		spawn_lamps()

func spawn_lamps():
	var direction = Vector3(randi_range(-1, 1) , randi_range(-1, 1) , randi_range(-1, 1))
	var position = Global.player.global_position + direction * radius
	
	var lamp = lamp_scene.instantiate()
	Global.lamp.global_position = position
	add_child(lamp)
	print(lamp)
	lamps.append(lamp)

I am completely correct that the program logic itself is bad.

I’m trying to make the lamps spawn at the same height, in different places, around the player within a radius of 50 meters and when leaving this radius they are removed.

what exactly should this do?

Global.lamp.global_position = position

you need to add a global position to the lamp position, something like this:

#example
lamp.position = global_position

and it might be a good idea to change the var position variable and call it something else, you might have a conflict with self.position depending on the context

the position variable is defined using a formula that I came up with out of my head

var pos = Global.player.global_position + direction * radius

I could be wrong but can you change a position of a node before its added to the tree?

Shouldnt you add the node first and then change the position?

otherwise, you need to add a global position of something to the lamp position… so if you want a global position for a lamp, you have to assign it to the lamp position

Yes, you were right. Thank you, but now I understand the logic of my formula… It changes the Y coordinate too. I didn’t really want this

var direction = Vector3(randi_range(-1, 1) , 1.854 , randi_range(-1, 1))|

var pos = Global.player.global_position + direction * radius|

Yes, there is an error in my formula for calculating the random coordinate xd

In general, I made this mechanic after about an hour of conservative philological thoughts. Thanks to whoever answered - you helped me

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.