Trying to show/hide a Sprite3D While an array is empty/populated

Godot Version

Godot Engine v4.5.stable.official.876b29033

Question

I currently have a dragbox that is abel to “select” single or multiple entities and deselect them.
I’m currently trying to make use of the “Empty Array” to do something. In my case, “Auto Select” another entity.

The “Selection” is basically a Sprite3D named “Selected_Indicator” that shows when the array is populated and empty that same array when the user clicks elsewhere

func get_dragbox_selected_objects(selectable_list:Array,dragbox_rect:Rect2) -> Array[Node3D]:
	var selected_array:Array[Node3D] = []
	
	##Current problem to solve
	while selected_array == []:
		obj_default_selection.obj_selected_indicator.show()
	## ^To solve
	
	for object:Node3D in (selectable_list as Array[Node3D]):
		var position_in_2d:Vector2 = get_viewport().get_camera_3d().unproject_position(object.global_position)
		if dragbox_rect.has_point(position_in_2d):
			selected_array.append(object)
	return selected_array

The other “Builder” entity does have a @onready var and it being called into the script

##Onready
@onready var obj_selected_indicator: Sprite3D = $Selected_indicator
## consts
const DRAGBOX_MIN_SIZE:int = 1
const SCRIPT_DEFAULT_SELECTION:Script = preload("../builder_control/builder.gd")

## exports

## public var
## private var
## onready var
@onready var obj_ui_dragbox: NinePatchRect = $NinePatchRect
@onready var obj_default_selection: SCRIPT_DEFAULT_SELECTION = $"../Builder"

Looks like that while is an endless loop.

True, that explain why it froze when I try to populate the array via my dragbox
Changed it to
if selected_array == []:
Doesn’t change the end results, but at least I can test without hard freezing Godot :stuck_out_tongue:

If you want to check if selected_array is empty, you should probably do that after trying to fill it with objects. Otherwise it will always be empty.

And for hiding the sprite when the array is populated, you can just add an else statement afterwards:

	else:
		obj_default_selection.obj_selected_indicator.hide()
1 Like

Also doing ==[] is wasteful because creates an unnecessary array each time you do this. Use Array::is_empty() instead.

2 Likes

Thank for your continuous help
One more step finally completed, one step closer to a finished project :slight_smile:

1 Like

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