I was following the " Creating In-Game Menus with Slate/C++, Part 2" tutorial, in which they use FSlateStyleSet to create a widget style. The name of “StyleSet” is a bit confusing for me, and I looked up the API it’s actually a derivative of ISlateStyle. So I substituted FSlateStyleSet with ISlateStyle, and it worked the same. So my question is, what is the purpose of using FSlateStyleSet in this case?
If you see any type with “I” in Ue4 it means this is Interface and it’s just common type to use between different types of class and structs.
C++ don’t formally support interfaces, only have class multi parenting and concept of abstract class as you just creating class that have declared but not defined virtual functions, which cause error if you try to create object with such class. So interfaces in C++ aren’t so obviously declared in C++ aspecially outside UObject class tree which has special implementation for them and UE4 use “I” prefix to identify them.
I guess they are there for future proof or ability to create different style diffrent types then FSlateStyleSet, so you are free to interface type if you want for your code to support any slate style that can be implemented in UE4.
Yeah, that makes sense to me. But what could be the risk if I create object with interface ? Because in this case, it seems to work just fine.
Using the interface should be the way to go, Imagine you create another style tomorrow which will have a different name, then you will have to change your MenuStyle class so that it uses the new class name.
If you instead create an interface and then parent that to any number of Style classes that you create, you will still be able to pass those into the MenuStyle class without ever having to change the MenuStyle class!! This is the style of programming to interface to achieve polymorphic behavior.
A very common example would be a parent weapon (even abstract) class, with many concrete methods, but only the signature for Fire. Create many new specific Weapon classes (example Rifle, Rocket Launcher, Shotgun with their own Fire.
Your character class calls Fire on Weapon, but you could pass in any of the specific classes at runtime and fire gets called on those classes
You wont be able to create one because virtual functions aren’t defined in interface so you will get error is you try. Reference is different kind of pointer so it can use abstract types like that, thats why it works in your case.