How to Build a Turn Based Strategy Game

5

Index

Attached Files

The following files have been attached to this tutorial:

Stats

3,559 visits, 6,372 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 11 Feb, 2023. Last updated 28 Feb, 2023

An example of what can be accomplished using this tutorial and the provided TBS.capx template visit the game Classic French vs British on the Construct Arcade.

Brief Summary

Those interested in making a Turn Based Strategy (TBS) game using Construct 2/3 have probably encountered various difficulties in doing so. One major difficulty is how to implement the core fundamental elements of a TBS game in Construct 2/3 using Event blocks.

The core fundamental elements of a TBS are,

  1. Turn Mechanics – Exchanging turns between player and computer
  2. Player Mechanics – Controlling units
  3. Computer Mechanics – Executing decisions

The first element is most intuitive to realize. The second and third elements require the utilization of Dsjakart’s and A* algorithms which can be implemented using Constructs 2/3 Event blocks.

Once Dsjakart’s and A* algorithms are implemented followed by a set of events and a finite state machine (FSM) you should end up with a functional TBS game.

1. Project Setup

First, in the layout add three layers named Background, Main, and HUD. Next, add the listed below game objects of corresponding types to the project,

  • Sprite: Attacked, Attacker, Node, Unit_B, Unit_A
  • Button: End Turn
  • Input: Mouse
  • Text: Turn

The Background will hold the Node game object. Main layer will hold Attacked, Attacker, Unit_B, and Unit_R. End Turn button and Turn text will be on the HUD layer.

To make the game scalable i.e. adding new units and terrain, create two families called “Units” which encompasses Unit_B and Unit_R and “OtherNode” which will only hold one object the Node sprite.

For each of the respective game objects instantiate the following variables as seen in the image below,

Second, add an Array, Dictionary, AJAX, and Function plugins as they will be used to load, store, read, and write data.

Lastly, add some nice artwork for units to make visual recognition easier.

2. Initial Setup

2.1 Setting up the Map

There are three methods for setting up your game map; manually inserting nodes, using for loops, or using Rex’s Plugins for Construct 2 the Board Plugin. In this project used for loops to build the map, see events 1 to 6 below.

2.2 Setting up the Debugging

The events 7 to 12 are not necessary but are good to have for purposes of developing and debugging Dsjakart’s and A* algorithm performances.

2.3 Setting up the Units

In the layout setup Unit_B and Unit_R in whatever quantity and wherever you want. In this project, because of how the conditions are set player control units with ID = “Blue” and AI control units with ID = “Red”.

For all Unit_B.ID set it equal to “Blue” and Unit_R.ID to “Red”.

For all Unit_B.Name and Unit_R.Name set equal to one of four options, “Blue_Default”, “Blue_Inf”, “Blue_Tank”, or “Blue_Arty”. The next following image illustrates an example of how to setup the layout.

On start of layout, AJAX requests “UnitsData.json” to be loaded and on completion stores that data in an Array, see event 14. A short delay is introduced before calling on the function "SetupUnits" to ensure that the data has been completely loaded into the Array.

Next, the events 15 to 17 simply create for each unit their respective accessory assets that are there for visual purpose of relaying the unit’s states. The event 18 instantiates a text object for all nodes and is used for debugging the Dsjakart’s and A* algorithm.

The function "SetupUnits" event 19 is called to instantiate each unit their characteristics, Type, Health, Damage, Range, MovesLeft, and hpConst. The function "SetupUnits" calls on function "Instantiate" event 22 to load unit’s data from Array into Dictionary where from Dictionary units characteristics can be setup by their name.

2.4 "SetupUnits" and "Instantiate" Functions

Objective is to load some data from the Array into the Dictionary and using the expression of Dictionary.Get(“key”) to set units variables based of the Dictionary.Get(“key”) value.

The Array stores data as a 2D structure where at Y = 0 it stores units name and at Y = 1 stores units characteristics.

Function "Instantiate" requires an input argument Function.Paraam(0) that must be an element from the Array at Y = 0. Using Array.IndexOf(“name”) expression the X index can be found and used as a reference to extract its associative Y = 1 data.

For this to work we need to clear the Dictionary once and add new keys to be later accessed by units variables. First how many keys to add can be found using the tokencount(src, separator) where src equals to the value we find from the Array at Y = 1 given the X index while the separator is a set to be a comma (depends on what symbol was used in UnitsData.json file).

Using a for loop and two tokenat(src, index, separator) expressions we create a key name and store a value to associate with that key name. In the first innermost tokenat expression we need to extract data a form KeyName: KeyValue (e.g. Type:Inf, Health:10, …) and the second outermost tokenat separates KeyName from KeyValue based on separator value, 0 for KeyName and 1 for KeyValue.

In the first tokenat src value is equal to the data in Array at X = Function.Parm(0) and Y = 1, index is equal to loopindex which is the current index of the for loop running, and the separator is a comma. The second tokenat src is of the form of KeyName:KeValue, index is either a 0 or 1 where 0 is for KeyName and 1 is for KeyValue, separator is colon.

Function "SetupUnits" is a one-time function called at start to call the "Instantiate" function. Where for each unit their variables (i.e. Health, Damage, …) are set from Dictionary.Get(“KeyName”) where the value of KeyName is equal to the units respective variable name (i.e. KeyName = Health).

  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hallo. Idont see AJAX plugin in add.

    • AJAX plugin already exists by default in Construct 2 and 3.

      When you double-click in one of your layouts a new window will appear for creating new objects.

      Inside that new window scroll all the way down to the Web category until you see the AJAX plugin. Or simply use the search tool and search for AJAX. Double-click the AJAX icon or click on the insert button to add AJAX into your project.

      If you do not see the AJAX plugin in your attempt to add it into your project, then it simply means that you have already added AJAX into your project already.