All my tiles implement the BPI_Tile Blueprint Interface. With the above function, I’d like to be able to say spawn Tile of type -whatever- at coordinate X,Y in my Tilemap, where -whatever- needs to implement BPI_Tile.
What I’d like to know is, how do I pass the Tile Type into this function? In the screenshot, I defined the TileType input as input of type BPI_Tile which is obviously wrong as that references an instance. Is there a Type-type or something like that? Is this even possible at all without C++ ?
It should just be a class reference. In C++ it is a templated TSubClassOf<YOUR_TYPE_HERE>
If you’re in blueprint it should just be your base class of Tile. You can see how with spawn actor it’s asking for a class reference, anything inheriting from AActor is a valid class for that pin. Does that make sense? Your tiles should really all be inheriting from a single BaseTile class in order for this to work properly.
My tiles are inheriting from the BPI_Tile Interface as I mentioned. The spawn actor doesn’t accept that as class. I’m pretty sure, it would accept a baseclass which inherits from Actor, but then it would never spawn a specific Tile, just an empty Baseclass instance which is obviously not what I want.
You’re using interfaces incorrectly. Interfaces do not define the class, only a behavior that the class gets to implement via functions. It’s not telling us what to spawn…
If you want to have one function spawn differnet types of tiles it would look something like this.
Function called SpawnTile
Spawn Tile takes 2 arguments, A transform and a BaseTileClass.
Any subclass of BaseTileClass is an acceptable input into the pin on the function.
Meaning if I want the function SpawnTile to spawn a GroundTile, then I just use GroundTile class as the input. Since GroundTile is a subclass of BaseTileClass it will work and spawn GroundTile.
Okay, when I think of it, that makes sense, since the interface doesn’t have the functionality needed by SpawnActor. So I guess I’ll need a BaseTile class, which then implements the interface (because I need to make sure all my tiles have the functionality defined in the interface) and all the specific tiles will then inherit the interface + actor functionality from the base class.
It’s not really what I wanted, because that way, I have to pass in an already created instance and not just a type, but I guess there’s no better way to do it.
Ah, I realized it now, you can define the parameter as class reference or object reference. I suppose class reference is basically the “type” I was looking for, while object reference is a specific instance.
After all, it works now with the different tile types (floor, wall_horizontal, wall_vertical and the four corner types) and generates a randomly sized room every time I launch it:
Yes, this is what you’re looking for it seems. FWIW, there’s also a blueprint node that can check if an interface is implemented by a specific thing, if that’s useful for you somehow. But also having a base class that implements the interface is perfect, unless you need to have two things that are very different base classes that implement the interface.
That’s good to know, but by passing in a class reference of my base class now, it’s practically impossible for the caller to stick in any other actor which doesn’t implement the interface, because the base class implements it already. So I think I’m good. Thank you.
yes. It can be useful information occasionally, like if you have multiple different base classes that implement the same interface, and you need to actually take a specific action based on whether that interface is available. Though that’s the nice thing about blueprint interface handling, is if you send an Interface call to something that doesn’t implement the interface, it just… doesn’t do anything, and passes onto the next node.