• Godot HelpProgramming
  • Implement interface like Unity or Unreal to get one button to do many different things?

Hi all,

I'm thinking of getting into Godot, but one thing I'm wondering is does Godot have interfaces like Unity C# or Unreal?

An example would be using a single button to do many different things in game. In C# I would do this via interfaces, if I collide with an interactive object I would check to see if it implements an interface, if it does I then call its functions or methods.

Can this be done in Godot GDscript? if not how would you go about this?

Thanks in advance

Welcome to the forums @bigStoater!

Yes, Godot has a way to do this, kinda. Godot has signals for things like buttons being pressed or collision events, and you can connect multiple functions to a single signal. There is not necessarily any interface checking built-in to Godot, but using the has_method function, you can write a quick wrapper that will only call functions on nodes that have the expected functions.

TLDR: yes, it is totally possible. It is slightly different than Unity (I'm not sure on Unreal), but it is totally doable.

In a project I am working on, I have:

func process_key_event(event):
	if(event.is_action_pressed("ui_accept")):
		print("ui_accept triggered")
		emit_signal("char_action_signal")

Thing is, I want that button to do different things in different contexts. If I have an item menu open... I want this signal to open the currently selected menu option. If I have a dialogue window open, I want this action to move the dialogue forwards. If I am in a Battle menu, I want it to select a battle option.

I do this by changing the function that gets called based on a GameMode + other relevant context.

This action is bound to the "Enter" button; but using the same technique on a button in the UI would give you the same flexibility of different behavior for different context.

Implementing this as an abstract method with an interface.... I admit that I haven't figured out how to do this in GDScript, yet. I believe it would be possible in GDNative (c++), though.

2 years later