This post is going to try to be the most definitive guide to setting up the Board system from scratch for the purposes of movement. This means it will hit the following requirements.
- Maps using Tiled/TMX via TMX Importer V2
- Board setup based on TMX reading
- SLG Movement for pathfinding
- AJAX, for reading TMX files
- Browser, for logging
- Function2M (or any function plugin)
- Keyboard, escaping movement
- InstanceGroup, for storing path information from SLG
- SLG Movement, for tile pathfinding
- TMX Importer V2
- TMX-XML Parser
- GridMove, for the movers on the grid.
- AJAX calls the tmx to be loaded, which gets it as a string.Then the trigger AJAX:On completed is called.
- SquareTx’s position offset is set to (16,16); ie the position offset is the physical coordinates of LogicXY (0,0). In an orthographic setup (which this event sheet screencap is based), the value of 16 refers to the offset so that the center of the tile would be moved inside the layout, and the top-left corner of the upper-left most tile will be aligned squarely at the layout’s (0,0) coordinates.
- SLG is configured to use the Board as its Board, and an InstanceGroup (ig).
- The mover’s GridMove behaviour is configured to use a particular InstanceGroup for its data.
- Make sure that any object that is to be instantiated using the TMX process is destroyed. This makes sure that during the instantiation the proper object is being referenced.
- When AJAX completes reading of the tmx, it will trigger its On completed event.
- Use the TMX Importer V2 (tmx) to import
AJAX.LastDatausing the XML parser. This populates the tmx object.
- Then set the SquareTX’s cell width and height to correspond with the tmx.
- Set the Board’s width and height (logical entries) to correspond to the size of the map in the tmx.
- Then initiate the tile retrieval.
TMX Data Retrieval
I’ve not yet documented the timings of Objects vs Tile retrieval, so I’m not making any dependence on timings.
Tiles are retrieved first before Objects.
When creating areas for movement, I prefer to create a Tiled layer for movable areas and leave tiles blank where it’s not possible to move on, rather than tagging tiles impassable, so I don’t need to check this during the cost function.
On each tile cell
- Use Board:Create tile to instantiate the tiles and place them on the board.
- Board: Add chess could also be used, but this is more confusing because it only places a logical ‘marker’, and does not instantiate it.
- Configure the frame of the tile (ie id of the tileset).
- Note that some it’s not always the case that you can configure the tiles after they’ve been created, for example, re-positioning tiles after instantiation didn’t seem to be reliable or possible. But it seems that instance variables are ok.
- TMX tile properties are set as necessary
On each object
Same sort of thing as On each tile cell.
- Create using Board:Create chess.
- Note: pay attention to the Board z-index as well as the C2 layer.
- Configure non-Board related stuff as needed.
- Note OXY2LXY, which is the ‘Orthogonal to Logical coordinate function’. To repeat a past post, the TMX Object’s position is recorded in orthogonal coordinates, and the Board or SquareTX object have no convenience features to translate those values to isometric. The OXY2LXY function is this translation:
This completes the TMX retrieval.
Initiating the move
The first thing to consider is the first call to move.
In this case it is a LMB on a tile.
- Note ig:Clean group. This removes all previous path entries (in this case it is “path” referring to the waypoint nodes)
- The main command is:
slg: Get moving path start from <mover> to tile/chess <tile> with moving points to slg.INFINITY and cost to <cost_function> then put result to group <instance_group_name>
- <mover> refers to the chess that is already on the Board.
- <tile> refers to the tiles on Z=0 on the Board, which is the basis for moving.
- Presumably (haven’t checked), if <tile> is at a specific Z index on the Board, then SLG will consider that Z index and pathfind on that level only. But what if the <tile> is at Z=2, for example?
- <cost_function> is the cost function of SLG which determines the resulting path. We can also call this a path function.
- <instance_group_name> is the group name inside the InstanceGroup object which stores the UIDs of the pathfinding nodes.
- Then on the condition that the GridMove is not moving the mover, we pop the first waypoint, which is the first waypointand this is SOL’d as the tile object.
- With this SOL, direct GridMove to move to that tile.
- This the initial movement phase.
Cost function (moving path function)
Before dealing with the continuation of the move, we must define the cost function of SLG on the mouse click.
The cost function, also called moving path function, is called by SLG when a moving path is required.
- The basic definition of a cost function to make set the the return cost to 1.
- Returning a cost of
SLG.BLOCKINGwill make this tile impassable
slg.TileUIDas the reference to the tile being queried for pathfinding.
- If the map was generated with blank areas, then there’s no need to check against those, as they won’t be even be considered for pathfinding.
Continuing the move
Once the move has been initiated, then continue to move as long as there are nodes in the InstanceGroup for paths.
- The continuation of the move is on the GridMove:On reach target trigger, which is triggered when GridMove moves on top of each tile as stored in the InstanceGroup.
- Use the condition
InstanceGroup:Pop one instance <tiles> from group <instance_group_name>in order to determine if it has popped the last one. If it has then GridMove is bypassed.
Movable area function
The movable area function may or may not be used in Citizen 2401, but this is a good time to document this function.
SLG has 2 ‘cost’ functions. One is the movable path, and the other is the movable area.
Just like movable path, movable area’s search pattern is to move out from a logical coordinate.
What the events above are trying to do is to generate a list of tiles which the AI can move to that are not LOS’d by the player. The LOS of the tile is determined by another function which switches the los instance variable accordingly, so that only this variable is checked.
The movable area cost function itself (‘p evade area’) does not check for the LOS state, and the reason is described here. Simply put, because of the movable area search ‘creep’ may get blocked by LOS’d tiles, only the tiles are tested on a distance basis (ie movement cost as defined in the
SLG:Get moving area)
Then, a filter function is applied on top of the results of the movable area function in order to get rid of those tiles that is LOS’d.
Stopping, changing paths
To stop, simply clear the InstanceGroup path group. This will give GridMove no waypoints to go when
GridMove:On reach target is triggered.
When trying to LMB on a tile while still moving, simply clean the Instance path group.
This has the effect of generating a new path while making sure the GridMove still goes to the last assigned waypoint.