Calling interface function in a widget doesn't work

guys im trying to call an interface that was implemented in a widget like this.


Annotation 2021-10-24 214150

why isn’t this working? i thought the whole point of specifying a owner is so that we can call it later.

You’re calling InitText on the player controller. Does the player controller implement the DialogeInterface interface? It looks to me as if the widget is the one that implements the interface, in which case the widget that you created in screenshot 1 needs to be the target for the InitText node in screenshot 2.

well isnt the controller the owner of the widget? how else am I supposed to call it then there’s no other way to access widgets other than direct references maybe.

You already have a direct reference. You’re storing the dialogue widget in the Dialoge variable in the first screenshot, so you can use that as the target for the interface call in the second screenshot.

You are right, the controller is the owner of the widget. But the owner has nothing to do with calling a function defined in an interface. An interface declares a set of functions and classes that implement that interface define those functions by implementing the specific functionality. When you make a call to an interface function in Blueprint, you need to pass as target an object whose class implements the given interface. How you store and get that object is entirely your own design choice.

that reference is in the game mode class. if I want to get access I probably need to cast to it which destroys the whole point of using interfaces in the first place.

If you want to avoid casting on the game mode, then you need to write an interface that your game mode implements, not one that the widget implements. If your situation is this:

  • You have a game mode
  • That game mode stores a reference to your dialogue widget that is of the type DialogWidget
  • You want to access that widget from anywhere else in the game without having to cast the game mode to your specific game mode class

Then your setup should be closer to this:

  • Create an interface DialogueWidgetProvider with one function GetDialogueWidget that returns a widget of type DialogWidget
  • Your game mode implements that interface by defining that function and returning the variable that stores the dialogue widget.
  • Your character makes an interface call to GetDialgoueWidget and passes in the game mode, no casting necessary.
  • You call InitText on the widget

You’ll notice that there is no need to have an interface for InitText anymore because you’re working with the dialogue widget class directly.