OnMouseEntered function on a hidden Button node? in C#

Godot Version

4.2.1

Question

Trying to setup a button so that it is hidden at first, shows up when the mouse hovers over it, then disappears again when the mouse moves away. Here’s my code:
‘’’
using Godot;
using System;

public partial class draw : Button
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
Visible = false;
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{

}

private void OnMouseEntered()
{ 
Visible = true;
}

private void OnMouseExited()
{
    Visible = false;
}

}
‘’’
I’ve been able to confirm that the signals are connected because if I make the button visible to start with I can make it disappear through setting that value in either signal method, but I can’t make it visible again. Do these functions just not work on a hidden object? If so, is there a setting to make it so they do?

It seems that mouse entered/exited signals aren’t emitted when the control is hidden. Try replacing Visible = true and Visible = false with Modulate.A = 1f and Modulate.A = 0f.

1 Like

Hmmm, that gives me the error CS1612 “Cannot modify the return value of ‘CanvasItem.Modulate’ because it is not a variable”.

My bad, I haven’t used C# in a long time. Try this Modulate = new Color(Modulate.R, Modulate.G, Modulate.B, value). Replace “value” with 1f and 0f.

2 Likes

Thank you! That did it.

1 Like

fyi c# has extension methods. you can use an extension method to change A without needing to: new Color(Modulate.R, Modulate.G, Modulate.B, value) It doesn’t really matter, but now you know.


public static class GodotColorExtensionMethod {
	public static Color SetAlpha(this Color a_color, float a_alpha) {
		return new Color(a_color.R, a_color.G, a_color.B, a_alpha);
	}
}
public partial class Button:Godot.Button {
	public override void _Ready() {
		SelfModulate = SelfModulate.SetAlpha(.5f);
		GD.Print(SelfModulate);
	}
}