I’m making a board game and even though I’ve already figured a way to look for other pieces on the board it look highly inefficient. The board is tile based and all the pieces spawn directly in the middle of the tile. So the way I figured out is to “Line trace” from lets say x:0,y:0,z:500 to x:0.y:0,z:-10 (board is on z:0) and see if the actor that gets hit is the board or a piece. I want to highlight the tiles that an enemy piece can be destroyed (by switching the tile to a red glowy material) so that means that with this way I need the trace I mentioned above to happen to the middle of all the tiles the piece can move to.
Will this be efficient or should I figure out another way (if so can you suggest any?)
To make it a bit more clear about what I want to do, let’s assume I’m playing chess and I click on the bishop in this image. (can move diagonally as shown)
If there’s any enemy pawn in the directions shown by the blue arrows I want to highlight the tile below the enemy pawn. The question is how do I efficiently check the 3 tiles on the top left, 3 bottom left, 3 bottom right and 4 top right to see if there’s an enemy piece
At the moment I have just 1 blueprint that spawns the whole board in it’s constructions script like this, should I really turn each tile into it’s own instance of a bp?
The way I thought about changing the material is just some very simple math. After figuring where the enemy piece is simply choose the tile which is at x-200,y-200,z=0 since the tiles are 400x400 atm and the pieces are all spawned directly in the middle (I’ve already tested and this works fine). So my only problem as I’ve already mentioned is finding where those enemy pieces are.
I’ve thought of your method as it’s a simpler version of what I did in my top comment but there’s the issue of pieces that can move in a weird pattern which can’t be figured from a straight line trace.
I guess I should mix your method for simple move pieces and something among the lines of mine for more complex ones.
Also what if I eventually want to highlight all the spots between my bishop and the enemy piece? I have to do a vertical trace for all the tiles and change their material as mentioned in my first top post?
hi… just a suggestion… I’m not a hundred percent sure what you are going for… but I think it might be a lot simpler and a lot more efficient… to use an actor blueprint for each tile… as well as a static mesh component, attach a trigger volume… maybe two types (neutral and harmful)… when a pawn lands on a tile, setting off an overlap event, the tile BP dispatches an appropriate event to the Pawn BP or Pawn BP controller, which might execute something like a death/damage event
… there are a bunch of ways to approach this… as you’d guess…
now that I understand better, your way does seem to make sense…
to change the tile material, I’d just trigger a change in it’s Dynamic Material Instance
I’m a hundred percent sure this isn’t the most efficient way, but… using your chess example… you could run a trace from your Pawn in the diagonals, when/if your trace collides you can get the Actor’s details (including useful stuff like class, location… a bit of maths might give you a reference to your tile). But, instead of a bit of maths, a quick 'n dirty way would be to run a second trace directly downwards from the collision point of your first trace, which would hit your tile (assuming it has the right collision settings) returning a reference to the specific tile, which you would then notify to change material.
… here’s a slightly different approach… but it builds on your method… it’s quick 'n dirty
Using your chess analogy again, let’s say we have a knight… no direct line trace possible… you could set up scene components (which correspond to your knight’s attack moves) and run traces between the relevant ones for the relevant distance. Or you could just start the non-linear traces from a point offset to the knight. I’m not sure about how to efficiently highlight free squares in the attack path, but it is possible to have a collision volume on them and just check the actor class in your line traces.
Honestly though, I think -with the non-straight line traces- it might be easier just to have each tile as an Actor BP.
This is an interesting question, I suspect there is a much more efficient way to iterate through the attackable squares to check if an enemy is located there. Something like a) an array of all tile locations, b) an array of actor locations, and an c) array of attackable tiles from your player’s current position (c is the tricky bit, but doable). This would be computationally efficient, and… as you ask… allow you to highlight the tiles with enemies, or highlight the empty attackable tiles. Anyone got any better ideas?