I can't set a custom size to an object when instancing (gdscript)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Starfighter

Hi,

I am using the code provided with the official step by step tutorial : Instancing

There are 2 scenes in the project:

  • Main (Node)
  • Ball (RigidBody2D)
  • Sprite
  • CollisionShape2D

What I want to do is to set a random size to the balls when creating instances.
Into the Main’ Script, I added 2 lines of code to do so but it doesn’t work.

extends Node

export (PackedScene) var Ball

func _input(event):
	if event.is_action_pressed("click"):
		var new_ball = Ball.instance()
		new_ball.position = get_viewport().get_mouse_position()
        var r = float (randi() % 9 + 1) / 10     # I added these 2 lines
		new_ball.scale = Vector2 (r, r)          # only
		add_child(new_ball)

What I’ve got to do the create new instances of the ball with a custom size?

Thanks

:bust_in_silhouette: Reply From: kidscancode

Rigid bodies cannot be scaled - the engine will ignore any scale you apply. You can see this if you scale a body in the editor: a warning will appear and when run, the body is still at its original size.

Instead, you can scale the sprite and the collision shape.

Thank you. :slight_smile:

Instead, you can scale the sprite and the collision shape.

Any clue about how to do this?
The Sprite node is a child of the Ball’s node (which is a RigidBody2D) and I didn’t find a way to set the scale parameter of this node.

The best I found so far is to add this code:
get_node("Ball").get_node("Sprite").set_scale(Vector2(r, r)).
I’am getting close but this doesn’t work the way I want: every time I click, a new ball spawns with the regular size , the random size is set to the very 1st ball created.
This make sense cause this line doesn’ involve the new_ball instance… and this is exactly the problem I can’t solve.

Any help is welcome.

Starfighter | 2018-07-22 17:27

Are you calling randomize() in ready:

func _ready():
    randomize()

As for the rest, this should work fine, setting the scale on the newly created instance:

func _input(event):
    if event.is_action_pressed("click"):
        var new_ball = Ball.instance()
        add_child(new_ball)
        new_ball.position = get_viewport().get_mouse_position()
        var r = (randi() % 9 + 1) / 10 # no need to cast to float
        new_ball.get_node("Sprite").scale = Vector2(r, r)
        

However, you also have to set the collision shape size as well. This depends on the shape you’re using - circles use radius, rects use extents, etc. Remember, instances share resources, so if you just change the size of the collision shape, it will change for all instances. Instead, you have to make a new shape and set its size:

        var s = CircleShape2D.new() # assuming you're using a circle
        s.radius = r
        new_ball.get_node("CollisionShape2D").shape = s

kidscancode | 2018-07-22 17:40

Thank you. :slight_smile:

Instead, you can scale the sprite and the collision shape.

Any clue about how to do this?
The Sprite node is a child of the Ball’s node (which is a RigidBody2D) and I didn’t find a way to set the scale parameter of this node.

The best I found so far is to add this code:
get_node("Ball").get_node("Sprite").set_scale(Vector2(r, r)).
I’am getting close but this doesn’t work the way I want: every time I click, a new ball spawns with the regular size , the random size is set to the very 1st ball created.
This make sense cause this line doesn’ involve the new_ball instance… and this is exactly the problem I can’t solve.

Any help is welcome.

Post #2

new_ball.get_node(“Sprite”).scale = Vector2(r, r)

It works perfectly, You made my day!

Thank you so much for your precious explanations, collision shape size adjustment is exactly the complementary part I needed too.

Are you the same KidsCanCode than the youtube channel author? If so, congrats for the amazing videos.

Cheers

Starfighter | 2018-07-22 18:33

Yes, that’s my YT channel. Glad this helped you, good luck!

kidscancode | 2018-07-22 18:38