Variable works fine everywhere but procecss delta

Godot Version

v4.2.2.stable.official [15073afe3]

Question

Hey, so I’m a beginner in godot and i tried making a drag and drop system in 2d. There is an Area2d and it has a simple script that sees if the mouse is inside the collisionshape. The variable works perfectly fine everywhere, but doesn’t work under process(delta). Here is the script.

drag script

extends Sprite2D
@onready var area2d = $Area2D
var mouseposition
@onready var mouseentered = area2d.mouseentered
func _ready():
	if mouseentered == false: #works fine!
		print("Mouse not entered.") #works fine!


func _process(_delta):
	if Input.is_action_pressed("LMB") and mouseentered: #cant access mouseentered in proccess delta??
		mouseposition = get_global_mouse_position()
		self.position = mouseposition
		print("Picked Up")

area2d

extends Area2D
var mouseentered
# Called when the node enters the scene tree for the first time.
func _ready():
	mouseentered = false
	print("Exited")


func _mouse_enter(): #all works fine here...
	mouseentered = true
	print("Entered")
func _mouse_exit():
	mouseentered = false
	print("Exited")

Any help would be greatly appreciated : - D

mouse_entered is signal. You need to connect to the mouse entered signal.

Try putting this variable:

@onready var mouseentered = area2d.mouseentered

In the actual _ready() function instead of an @onready variable… I think i remember this making the variable actually stay mapped to what you are mapping it to?

Also another solution would be to create a new Script called GameManager.gd and make it an Autoload script that houses all your variables you want access to. Example:

Extends node

#GameManager.gd

var mouse_entered = false

Then you can just update that variable from any other script as well as call it from any script like this:

#assign it a value from area2D script

GameManager.mouse_entered = true

#call that value from main script

GameManager.mouse_entered

@temato_0

But mouseentered is not a property of area2d or have I misunderstood something here?

I am not asking in order to correct you, am asking to learn something if I have missed something. Thanks in advance for explaining.

Oh I think I get it, the OP is accessing a variable in the node area2d. Never mind.

Yeah mouse_entered is just a variable you would have the actual mouse_entered signal assign but assign it to an Autoload script, which is a script accessible from anywhere.

You make a new script, with a variable called mouse_entered of type bool (like i shared above) and name it something like GameManager.gd and assign it as autoload within Project → project settings → Globals → autoload tab

Here you can assign scripts that run constantly and are always available to reference. Give it a name here as well like GameManager.

Then you update the mouse_entered variable in GameManager.gd from your area2D like this:

func _mouse_enter(): #all works fine here...
	GameManager.mouse_entered = true

And call it in your process like this:

func _process(_delta):
	if Input.is_action_pressed("LMB") and GameManager.mouse_entered:
		mouseposition = get_global_mouse_position()
		self.position = mouseposition

Thank you so much! This fixed it.

1 Like

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