mouse_exit() per script or connect signals dynamically

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bobokox
:warning: Old Version Published before Godot 3 was released.

Hey!

I have a script for textured buttons and i want to use it for more than one node.
If i connect a signal with a script its fixed on the node. indipendet from where the script is.
i already found an alternative for pressed() in script, but i totally need the mouse_exit() function. I found is_hovered() but it doesn’t help, because i need the trigger in the moment the mouse exits the button.
is there a way to do it per script, Independent from the node which carries the script or is there a possibility to get the connection automatically from the current node?

thank you

I’m a bit confused about what you mean. When you say “for more than one node”, do you actually mean “for more than one type of node”? (for example Button, Control, TextureButton…).
All controls inherit Control, there is enough stuff in there to do any kind of button.
Can you provide one example where you have a problem?

Zylann | 2017-04-04 12:43

Hey Zylann.

thank you for the comment.
sry for the bad explanation. Let’s try it again :slight_smile:
I have several texturebutton-nodes in my scenes. They are all collectable and all of them should carry the same script.
The script should change a global variable, when the mouse is hovering, and set the variable to 0 when the mouse exits the button. and here is the problem. when i make the script with

if is_hovered() == false: global.MyVar = 0

it doesn’t work if i have more than one texturebutton-node with the script in my scene, because the last script which runs will set the global var to 0 if its not hovered, meaningless if an other texturebutton is hovered.
so, what i need is something like the mouse_exit() signal, but individually for every texturebutton-node which carries the script.
Don’t get me wrong, im pretty sure i would be able to solve this with is_hovered() and some if querys, but there is such a simple way to do it with signals, so their must be a way to do it simple in script too.

the whole script:

extends TextureButton

export var type = "coco"
export var time_need = 5
export var visible = true

var generate = false

func _ready():
	set_fixed_process(true)
	
func _fixed_process(delta):
	if visible:
		set_hidden(false)
	else:
		set_hidden(true)
	
	if is_hovered():
		global.time_needed = time_need
		
	if is_hovered() == false: ################### heres the problem
		global.time_needed = 0
	
	if is_pressed() == true:
		visible = false
		if type in global:
			var a = global.get(type)
			a += 1
			global.set(type, a)
			print(global.get(type))

i hope its better understandable now. im very bad in explaining things :slight_smile:

bobokox | 2017-04-04 14:25

:bust_in_silhouette: Reply From: YeOldeDM

why not connect the button’s mouse_enter/mouse_exit signals? If you have many of these buttons, polling each in fixed process is going to slow things down, and I don’t see anything really in your process loop that needs to be done per-frame, you’re just checking for conditions that could be triggered by signals instead.

extends TextureButton

export var type = "coco"
export var time_need = 5
export var visible = true setget _set_visible

var generate = false

func _ready():
	connect("mouse_enter", self, "_on_mouse_enter")
	connect("mouse_exit", self, "_on_mouse_exit")
	connect("pressed", self, "_on_button_pressed")

func _on_button_pressed():
	visible = false
	if type in global:
		var a = global.get(type)
		a += 1
		global.set(type, a)
		print(global.get(type))

func _on_mouse_enter():
	global.time_needed = time_need

func _on_mouse_exit():
	global.time_needed = 0


func _set_visible(what):
	visible = what
	set_hidden(!visible)

hey!

thank you. didn’t know that there is a way to connect per script. this is great. always did it in the editor.

thank you for this and for cleaning my script :slight_smile:

bobokox | 2017-04-04 17:38