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")
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
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