How to Add C++ Class Functionality or Blueprints to Tiles within a TileSet and/or TileMap?
What I am trying to do: I have been trying to create a top down tile based game using Unreal’s Paper2D functionality. I was wondering if it was possibly to add class functionality to tile within the TileSet and/or the TileMap.
Problem: My overarching problem is that I am unable to add objects to the tile map that have functionality beyond collision. I would like to refrain from just placing something on top of my TileMap within the level. I feel that the TileMap would be more coherent if all the objects could be placed within the TileMap editor.
Example: For example, how would I go about creating a tile like a pressure plate that would trigger when it was stepped on by the player. If I create a class/blueprint that has all the functionality of my pressure plate how can I apply it to my Tile within the TileSet to make sure that the tile’s behavior is consistent when placed within the TileMap.
Additional Info: I did see in the TileSet editor that there is an option to add meta data to a tile, but the only metadata that can be added is a single string. Furthermore, the TileSet editor recommends that I use the Tile’s metadata as an index within a DataTable. Is this metadata something that I could use to attach a class to a given Tile by referencing the DataTable.
Thank you very much for your time and consideration in reading this post. I look forward to hearing back from you. Thanks again for the help.
asked Jun 15 '16 at 07:34 AM in C++ Programming
Alright, I figured it out with Blueprint (you can probably do it in C++ though). Even though you'll probably never see this, hopefully someone will come across it - as there's basically no documentation on these features.
Alright, so step 1 is to get your tileset from a texture. For my example, I'll be using some classic Zelda sprites courtesy of 'Darth RPG'. The tileset size I'm using is 16x16, which will be important down the line.
Step 2 is to set up both collision AND metadata on the tile in question you want to perform the blueprint functionality. I'll be using a staircase. To set up the metadata, input text into the "Used Data Name" field. For my example, I'm using the string "Stairs". Also note from the image, my collision doesn't encompass the entire tile. It's more like 9x9 for 16x16 tile.
Place your tilesets on your tile map, and then place that in the world. This is my example scene, that allows me to test the collision of the pillars, and my staircase in the middle.
NOW comes the big important step, the blueprint. Long story short, when we hit collision, we want to get the metadata of the tile we're colliding with. Unfortunately there is no "get tile" for this sort of thing, so we have to make one. I'm doing this in my character, but you would probably be better off doing this in a component or fired off via a function. In this code, I start off Event Hit (an occurance for any collision event) and take the Hit result. Breaking the hit result I get the hit actor, and cast to PaperTileMapActor. Getting the casted actor's location, the location of our tilemap, I subtract the Impact Point from the Hit Result to get the aproximate position of our tileset. That point needs to be converted to a tile that can be found, and Metadata extracted from. Breaking the vector from that subtraction node, I get the X and Y values and divide them by my tile size (16). These values might get sketchy if you aren't working on a 1,1,1 scale like I am, and you'll probably need to multiply them by a scale if you aren't - but regardless, you'll want to round the divided results. In my example I invert my Y value but that might not be nescessary depending on your setup. The imporant step, is for you to finally get the render component from the PaperTileMapActor you casted to earlier, and "Get Tile" from it. Input the rounded X and Y values into the appropriate slots. From there you perform the final and most important step: "Get Tile User Data", which will hopefully return the Text of the tile's Metadata.
The end result, from by debug outputs, is the Metadata text for the tile. From there, it's easy enough. Perform a switch case statement on the metadata names to perform whatever function you want. The nice thing about this solution is that if I ever want any staircase in any tileset to perform the same function, I'd just have to give it the metadata of "Stairs".
answered Apr 10 '17 at 06:47 AM
Someone thought about the logic of determining the surface to play a certain sound of walking depending on the surface? I understand... we can use key or composite_key that do it, but m.b. exist more elegant way. Or use invisible collision. If i can get layer name, that can be key.
Follow this question
Once you sign in you will be able to subscribe for any updates here