How to adjust size of visibilitynotifier2d from code?

Godot Version

<stable4.2>

Question

<Im trying to make it possible to adjust the size of visibilitynotifier2d from Inspector. Following code is not working and have no idea why. I would appreciate if you could help me.

@export var spawn_range : float = 8: set = set_spawn_range
@onready var spawn_range_vis = $SpawnRange as VisibleOnScreenEnabler2D

func set_spawn_range(new_value: float):
	spawn_range = new_value

	if get_node_or_null("SpawnRange") != null:
		var rect_centralizer = RectCentralizer.new()
		var centralized_rect_value : Rect2
		centralized_rect_value = rect_centralizer.center_rect_by_value(new_value)
		get_node("SpawnRange").rect = centralized_rect_value
		get_node("SpawnRange").position = -centralized_rect_value.size

Global Script:

extends Node

class_name RectCentralizer

static func center_rect_by_x_y(vector2: Vector2) -> Rect2:
	return Rect2(-vector2, vector2 * 2)
static func center_rect_by_value(f : float) -> Rect2:
	return Rect2(Vector2(f, f), Vector2(f, f) * 2)

static func set_position_by_rect(rect : Rect2) -> Vector2:
	return -rect.size

I believe this should help. Note that self in my code is the VisibilityNotifier2D.
self.rect = Rect2(self.rect.position, Vector2(1, 1))

Vector2 is the x and y → Vector2(x, y) ← width and height

where should i replace that code?

This depends on where you want to change the size, hard to understand your code, but I believe that instead of all those lines you written, you can just use this 1 and be good to go.

with that one line of code, i can change the size of rect from inspector ?

I believe you mean script? In that case, yes

@export var spawn_range_x : float = 16
@export var spawn_range_y : float = 16
@onready var spawn_range_vis = $SpawnRange as VisibleOnScreenEnabler2D

func _physics_process(delta):
	spawn_range_vis.rect = Rect2(spawn_range_vis.rect.position, Vector2(spawn_range_x, spawn_range_y))

the size does not change. any idea why?

Well, you define the SpawnRange as VisibleOnScreenEnabler2D, and I believe the code I wrote is for VisibilityNotifier2D, unless it works for this too?

well VisibleOnScreenenabler2D is VisibilityNotifier2D in 4.2. I guess I need to find other way since it doesnt work.

my bad. It did changed. not in editor but in game

There are both, I am using Godot 4.2.2:
image

Also, I have tested my code on VisibleOnScreenEnabler2D and it works for me:
Before starting the game:
image
After starting the game:
image

Am I correct that you want to change this property?

I also believe that the issue here could be your @onready reference of the element you want the rect to change.

Let’s try to fix it :wink:

You are right, I fixed onready reference
did it change on editor screen?

If I understand you correctly. When you start the game and go into the editor, no values will be different. If you want to see in the editor if anything changed, you need to go:
image

here at the top, you can see “remote” and “local”, if you press on remote, you will be able to see all scenes, their variable values when you start the game.

Hope this helps

what i meant was is it possible to change the size in editor screen like in screenshot, so i dont need to start the project every time and itll be easier when designing the level.

I think if you press on the SpawnRange, there should be the size you can set? Would like to get more information on how you are planning on using that when building the game. If I understand correctly, you will want sth to spawn inside the Obj spawner area?

enemy will just spawn at global position of ObjSpawner. Changing the size of SpawnRange can change how far the enemy can spawn from player.

OMG, I FOUND WHAT YOU NEED, AND THIS WILL HELP ME TOO!

Put your ObjSpawner scene into main, and right click on ObjSpawner, then press → editable children!! I believe we solved your problem?

I knew I could do that, but wanted avoid doing that cuz its kinda annoying to manually change the size and reposition the offset.

Oh, sorry, I dont think I can think of any other solution