Thursday, 15 October 2015

Conditional Statements

At its core "a conditional statement is a set of rules performed if a certain condition is met." (Computer Hope, 2015) So essentially you could think of it like a checklist or flowchart the software uses to keep track of what the player does. Here's a simple example I came up with that software could actually check for: Did the player collide with an enemy's hitbox? If yes, kill the player and restart the level and if no, check again see see if they hit it. They are incredibly useful for seperating code into segments for decision-making. Here's an example flowchart below using conditional statements:

Here's a basic conditional statement in flowchart-form, with varying outcomes. (Center for Experimental for Social Science, 2015)
Conditional statements can branch out code in both UE4 with both Blueprints and C++, this helps add variety to the game, from in-depth decision making all the way down to whether or not a player collected an object. In UE4 in particular, the 'Branch' node (shown below) can help games with decision making involved via true and false statements using Boolean data types. It can separate the decisions into two independent outcomes for the user to add depth of choice to their game. (Docs.unrealengine b, 2015) Blueprints also have Switch nodes alongside Branch nodes which serve a different function, and as stated by (Docs.unrealengine b, 2015): "A switch node reads in a data input, and based on the value of that input, sends the execution flow out of the matching (or optional default) execution output. There are several types of switches available: Int, String, Name, and Enum." 


An example of the branch node in UE4, used to separate out Blueprints for conditional statements. (Docs.unrealengine c, 2015)

In C++ there are both 'if...else' and 'switch' statements, both with a different purpose. Firstly, as supported by (W3schools, 2015) if...else statements are used to do different tasks depending on the different scenarios in your code, and there are different types of this statement used in another programming software, Java. And as quoted by (W3schools, 2015): "Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed".
Next-up are switch statements, where as mentioned above can mentioned multiple pieces of code to be used instead of a singular piece instead. This is very useful for typing code efficiently as you can affect multiple segments at once, without having to separate them all out with if statements.

No comments:

Post a Comment