How do I move my position 2d??????

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


var rngx = RandomNumberGenerator.new()
var rngy = RandomNumberGenerator.new()
func _on_Button_button_down() -> void:
    var x = int(rngx.randf_range(0,1024))
    var y = int(rngy.randf_range(0,600))
    $self.position(x,y)

Whenever I put this code in it give me the error, Attempt to call function ‘position’ in base ‘null instance’ on a null instance.
What am I doing wrong? How should I fix it?

:bust_in_silhouette: Reply From: Ghost#1

make this:

var x = int(rngx.randi_range(0,1024))
var y = int(rngy.randi_range(0,600))

Instead of this:

var x = int(rngx.randf_range(0,1024))
var y = int(rngy.randf_range(0,600))

write randi_range instead of randf_range
This error cause beacause randf_range takes float number (f in randf means float) and you wrote integer number.
And $self (last line) make it self without dollar sign

This is my code now:

extends Position2D

var rngx = RandomNumberGenerator.new()
var rngy = RandomNumberGenerator.new()

func _on_Button_button_down() -> void:
    var x = int(rngx.randi_range(0,1024))
    var y = int(rngy.randi_range(0,600))
    self.position(x,y)

The hierarchy:
enter image description here
When I put this code in I get an error saying: E 0:00:01.280 emit_signal: Error calling method from signal ‘button_down’: ‘Button(Button.gd)::_on_Button_button_down’: Method not found…
<C++ Source> core/object.cpp:1242 @ emit_signal()

Vickyboi | 2022-09-03 23:38