Hello. I am trying to understand how interfaces work. That is, I understand the essence, how to manage them, but I still do something wrong. I just don’t get the value I’m trying to transfer. So please help someone who is not hard to understand how to transfer a value from one actor to another, for example: Integer value between 2 actors, just to guide me to the right path in understanding interfaces.
TLDR:
You implement interface in some actor. And add function or event. Then You can always cast to interface (instead of casting to actor class) and use that function or event. For it all to work you need to add ACTUAL code to that function in blueprint that implements (uses) interface.
Longer version:
It is hard to explain that stuff without explaining how pointers work in C++ (so i will not dig into this, i try simplify it all).
Every actor class has its template (really allocated memory with all structures around). So when you want to send some variable over to another actor, your code has no idea about that another actor structure (and this is what casting does, it is remapping of stuff)
However when you have more than one class communicating, you have no idea which template to use. This would require casting to each possible class until you get correct one.
That is what for interfaces are. You create middle man class/template that both sides know and can use.
I explain on example:
I want widget HUD be able to tell any widgets some thing (for simplicity lets say its int32 id number).
So HUD goes trough all widget and child`s of widgets, then sets some integer in. However almost every widget is different class.
Solution is blueprint interface. I would create one with Function “SetIntID” that just sets some variable in widget.
With CastTo HUD would need cast to every possible widget class i use until it finds correct one.
But with interface, if widget implements my interface, HUD can cast to just that interface.
Now this next part is about style of coding. I group interfaces by what they do. In above case i would make interface with name like “HUD communication” and put there all functions and events that communicate from or to HUD. This makes a bit messy code because some functions/events are intended to be implemented in widgets some in HUD only. But you can use some naming convention or set group for functions etc. This way both HUD and its child widgets use same interface (not needed when you do not group stuff working both ways).
PS.
Instead of that int32 as ID i use almost same method for setting reference/pointer to owner (master widget). So i create “SetMyParent” function in interface. then every child implements that interface. From master i iterate trough all childs and childs of childs and set that reference. This way all those widgets can always ask parent about things. This way i can always pull that reference and read values directly from parent (no matter where widget is nested in whole hierarchy).