Randomize item scale on instantiation

Godot Version

4.2.1 stable

Question

Hi, I’m pretty new to all of this, so forgive the simple question.
I’m coding a behaviour for a Node so that it has a random scale property when each instance of it spawns.

I wrote it like this:

var random_size := randf_range(0.2,0.8)

func _ready() -> void:
	scale = Vector2(random_size,random_size)

I coded it this way because i want it to scale evenly, so scale.x and scale.y have to be the same.

It works, but I was wondering: is this the most “elegant” way to do it? It looks pretty ugly, having to write random_size twice to specify x and y for scale.

Is there a better way?

Thanks

Vector2.ONE * random_size

1 Like

Math to the rescue… lol
Thanks

1 Like

I realized that there’s a solution that’s even more beautiful:

instead of:

var random_size := randf_range(0.2,0.8)

func _ready() -> void:
	scale = Vector2(random_size,random_size)

I just:

func _ready() -> void:
	scale *= randf_range(0.2,0.8)

Since scale is already equal to Vector2.ONE by default if left untouched
But this might cause some trouble if I touch the scale from the inspector later on.

Edit:

but then I should just do this to prevent trouble:

func _ready() -> void:
	scale = Vector2.ONE * randf_range(0.2,0.8)

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