0 votes
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?

Godot version v3.5.stable.official [991bb6ac7]
in Engine by (18 points)

1 Answer

0 votes

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 randirange instead of randfrange
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

by (37 points)
edited by

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 emitsignal: Error calling method from signal 'buttondown': 'Button(Button.gd)::onButtonbuttondown': Method not found..
<C++ Source> core/object.cpp:1242 @ emit_signal()

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.