godotgdscriptgodot4

need help fixing my godot gdscript that is throwing an error "Invalid type in function 'set_size'"


i have been having an issue with my gd script in godot, where it has been throwing an error at me

what the script is supposed to do:
sync the background size with the color picker when the color picker changes size

here is the error:

Invalid type in function 'set_size' in base 'ColorRect'. Cannot convert argument 1 from float to Vector2

here is a screenshot of the scene nodes

scene nodes

here is the code:

extends Area2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
    var offset = 8
    print(str("offset = ",offset))
    var x = $ColorPicker.size.x + (offset*2)
    print(str("x = ",x)) # should print 314
    var y = $ColorPicker.size.y + (offset*2)
    print(str("y = ",y)) # should print 592
    print("this next line is where it crashes")
    $ColorRect.set_size( x, y ) # this is where it errors
    print("if you see this text it didnt crash")
    pass

and here is the output log:

Godot Engine v4.3.stable.mono.official.77dcf97d8 - https://godotengine.org
Vulkan 1.3.289 - Forward+ - Using Device #0: AMD - AMD Radeon 780M (RADV GFX1103_R1)

offset = 8
x = 314
y = 592
this next line is where it crashes

any help would be greatly apreciated


Solution

  • The set_size method takes two parameters, but not x and y as two separate floats. The first parameter must be of type Vector2 and the second of type boolean (this one should be optional). Combine x and y in a new vector2 type variable and it should work:

    var myVector = Vector2(x, y) 
    

    Pass it as the first parameter to set_size:

    $ColorRect.set_size(myVector)