Noob needs help: Moving Units

Hi. I’m totally new to scripting and am trying to put a basic game together via blueprints. the game is based around a chess style turn based system.

I need some advice on how to approach grid based movement. the things i need to do are:

  • within the unit blueprint, define where it can move to (each unit will only be able to move in a certain way e,g bishop moves diagonal. pawn moves 1 space forward and can take 1 space diagonally forward)
  • when a unit is clicked on, its possible moves are highlighted on the grid and if a grid space is clicked on the unit moves there.

Any help would be greatly appreciated.

I kinda want to achieve something along theses lines seen in this video:

It’s quite a lot to explain in detail, I’m doing something remotely similar and might be able to give a general idea how I’d approach it.

I’d suggest creating a 2d array of the entire map (you’ll have to do this yourself, I’m using 2 arrays - one for X and one for Y) and probably write a few functions to I/O between world units and grid/array units. You can then also place obstacles into this 2d array, as well as characters etc. Store the actual actors into these arrays so you can quickly grab items/players/etc from grid coords, so both will essentially have grid coords and world coords this way. Then writing out the number of units your unit can move becomes quite easy with for loops.

I’m not sure how new you are but the mouse events are easy to handle, there’s a bunch of tutorials for that out there.

Hope that helps somewhat. There might be better ways to do it but frankly I don’t know of any. :slight_smile:

Good luck!

This seemed like an interesting challenge to try and figure out, so I took a few hours and gave it a shot. It’s probably not the best way to do it, but it sort of works.

I’ve got 3 main components that interact with each other and then some helper stuff.

Grid Spawner: I started off creating a grid with some static mesh squares on the floor using the construction script. I automated the process as much as I could so you really only have to insert the Row Amount to change the size of the grid. (It’s also pretty easy to just change the mesh and the OffsetAmount based on the size of the mesh to get a nice grid.) It’s very important that all the meshes have the same material for my system to work, because I use that for checking for available movement later. (Prolly not something I should have done, but I figured it’d be easy enough for this example.)

MyCharacter: I used a first person template, because I had one extra project with that made already anyway. So instead of a mouse pointer, aiming is done with the view direction of the camera. (Which is again not ideal, but I was too lazy to change it to a mouse pointer.) I do a line trace out from the camera and check what I’m hitting. First it checks whether it’s an AI character (that I confusingly called Actor for no reason), if so it sends a event to the AI. If not it checks whether you clicked on the grid, if so it checks the material of the grid to see if it’s highlighted by stuff the AI character does when it’s selected. If it is a valid move spot, it gets the location of the mesh and sets that as a vector variable (that way the characters always move to the centre of the mesh) and it turns the square white so you can see where you clicked. In the very end it sends the move command to the AI itself so it can do the stuff it needs to do. There’s also a bit where right click deselects the actor and sends an event through to it.

There’s also a bit for highlighting the square based on what you’re looking at (turns blue in the video). It doesn’t make the highlighted squares turn any different colour, because I’d have to redo the valid movement locations check then and I’m somewhat lazy. It turns a single square blue and then back to normal if you move your view off that square. It also double checks and turns any blue squares back to default before activating a new blue square, just because it kept leaving blue ones without clearing them and this was a quick fix. (It uses the array of meshes I set in the construction script of the Grid Spawner.)

AI Character (Actor): The thing I named somewhat confusingly as an actor. First it gets references to MyCharacter and GridSpawner, gets the ArrayOfMeshes and the OffsetAmount from the grid, which I’ll use in a bit. Then I use Event Select AI to highlight the area around that AI by running line traces down from above with offsets determined by the grid offset amount. I use some loops to run several traces in a single direction to have less repeating bits, but mostly I did each trace separately. That way you can have absolute control over the shape of movement area you want. I also make sure that all the traces are going to go exactly in the middle of each square, so that when there’s an obstacle in the way, it doesn’t highlight the grid plate and you cannot move there. There’s one more bit that checks whether the trace hits an actor, which could be used for detecting friendlies or enemies and have a separate attack function for dealing with that, but right now I just left it to do nothing.

There’s another bit that (starts off with a pointless branch node by accident) sends the move command, which just moves the AI to the location that was set in MyCharacter and upon success it returns that square to the default colour (because we changed that to white before). After that it highlights a new set of squares around it and lets you move around again. (In an actual game you’d probably want to either show a new smaller area or not have one at all if the move points are all spent.)
And deselecting just clears the highlights so that the next thing you select can’t move into those squares.

I didn’t bother adding cutoff areas for the highlighted grid when you want your AI to move behind something, but they’re pretty easy to make, especially if you use loops to draw them in a single direction. You can just stop the loop when it hits some kind of obstacle. In the end of the video I show how it kind of successfully moves around an obstacle, but that’s more luck than expected behaviour.

Also if it’s not obvious, I use a blueprint interface to send the events from MyCharacter to the Actor. It just has 3 functions that I just named and nothing else, they’re all empty. I had to tweak some collision settings on the AIs to make sure they block the visibility trace I’m sending out and of course there’s a nav mesh bounds volume around the grid so that the AI’s could even move.

And here’s the blueprints for everything:


Edit: There’s a few things I accidentally did that weren’t right, like when drawing the highlights, it did it as part of the loop as many times as the array had elements and not at the completion of the loop as it should have. Also if for drawing the line traces to highlight move paths you replace the “for loops” with “for loops with break” and use the failure of the class check near the end, it will prevent it from drawing more moves behind enemies.

3 Likes

So cool job bohrium !!! since liquidjoy talked about his trouble i was thinking about something like that to do, you know, like a “potter” sorcerer’s chess with lots of particles on events :p.

Thanks for your assistance guys, Your awsome.

Im gona work through some of the solutions that you offered, I’m totally new to scripting so don’t know exactly what is meant by some of the terminology you have used but If I follow the handy blueprints you have provided I hope to get something up and running and learn a lot in the process.

This is pretty darn awesome. How do you know if a grid is occupied to make sure it doesn’t light up? I can’t figure that part out… Very very nice anyway!!

I sort of went the route of least resistance and made a line trace come straight down in the middle of the square to light it up in the first place. And if there’s something in the way, the trace will never reach the square. And because all movements are going directly to the centre of the squares, if there’s something in that square, it’s going to block the trace. This way you can place anything that blocks visibility traces onto the grid and it will automatically not count those squares as long as the middle of them is covered. For anything that doesn’t cover the middle (a U shaped spacecraft or a part of an obstacle) you can add an invisible box that blocks visibility traces either to the object or just on top of the squares you don’t want to have available.

Would take a bit more work to create a grid that you could spawn dynamically in an area (like for a battle scene) and have the obstacles in the world change the available spots to move to and to deal with different kinds of terrain elevations. But it’s nothing too difficult to do.

Ok, that’s a pretty good idea. You solved it elegantly I think. Thanks for sharing!

Digging this one up again!

Been trying to get the blueprint Bohrium posted to work but I’m kinda stuck at (least) 2 points;

  1. Trying to connect the “Self” to “AI MoveTo” doesn’t work, says Actor_C isn’t compatible with ActorPawn

  2. Probably related with 1, can’ select whatever I try making the active AI mesh.

It’s probably noticeable but this is my first foray into working with blueprints, only other experience I’ve had is with Kismet and that was a while ago :expressionless:

If possible could you post the project? Been using it as a base for learning more about this type of movement as i want to try making a chess game and
evolving it into something a bit more advanced later on.

Have you seen the chess part 1 and 2 tutorials, they are quite helpful:

Also fen’s thread:

and

All of these are quite helpful.

Cheers,
Max

Thanks man, just found the chess tutorials after posting this (even though I’m sure I searched for chess before :|) helps a lot!

Ah, I think it could be a problem with the type of blueprint that you created. The parent class (the one you pick when you make a new blueprint) should be either a pawn or a character (mine’s based on character). If you picked just the actor class it doesn’t have the stuff needed by default to receive input from a controller. I probably couldn’t have picked a more confusing name for it if I tried. I’ll be sure to try to pick some kind of name that doesn’t mean something else when I’m trying to explain something in the future. Like AI_dude or Jim or Bob or something.

Also I did a bit of fiddling about with the grid myself later and made the grid form to an uneven surface or landscape and fixed a silly location error. For like when you want to dynamically spawn a movement grid when encountering an enemy or something. Although some fiddling with the movement square highlight traces would be necessary to prevent running to invalid squares.

There’s a landscape with that curvature below the grid. It takes the surface normal of whatever is beneath it and rotates the tile to match it more or less and sets the height to be roughly on the surface. The AI can walk on it as long as the steps aren’t too big (so no cliffs) and the angle of the steps is below max walking angle.


That looks quite awesome :smiley:

Probably going to come back to the solution you posted further up at a later date as a lot of it is still a bit to far over
my head as to how it works, which means I can’t modify or troubleshoot it really :<

Hope to see more of your work though!

Hi there! Ur work looks awesome! I want really want to try this out, but im totally noob… so … whats the “MoveAiInterface”? I hope you remember))

Great insights guys! I’m here trying to make some things turn based. Thank you all!

I will release my turn based blueprint system in December. Have a look;

https://forums.unrealengine.com/unreal-engine/marketplace/1533463-turn-based-rpg-template

Hey, I wanted to try this example but unfortunately i can’t see the pictures that you added here. Do you or others know how I can fix this?