gamedev

C++ – Functions

C++ – Functions

Functions allow to structure programs in segments of code to perform individual tasks.

Functions with type

Working samples:


// function example
#include <iostream>
using namespace std;

// integer - name of the function - data
int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z; // the result is 8
}


// function example
#include <iostream>
using namespace std;

int subtraction (int a, int b)
{
  int r;
  r=a-b;
  return r;
}

int main ()
{
  int x=5, y=3, z;
  z = subtraction (7,2);
  cout << "The first result is " << z << '\n';
  cout << "The second result is " << subtraction (7,2) << '\n';
  cout << "The third result is " << subtraction (x,y) << '\n';
  z= 4 + subtraction (x,y);
  cout << "The fourth result is " << z << '\n';
}

Functions with no type – void


// void function example
#include <iostream>
using namespace std;

void printmessage ()
{
  cout << "I'm a function!"; // it prints 'I'm a function!'
}

int main ()
{
  printmessage ();
}

Arguments passed by reference

// passing parameters by reference
#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)
{
  a*=2;
  b*=2;
  c*=2;
}

int main ()
{
  int x=1, y=3, z=7;
  duplicate (x, y, z);
  cout << "x=" << x << ", y=" << y << ", z=" << z;
  return 0;
}

The result:
x=2, y=6, z=14

By |C++, Video Games Development|Commenti disabilitati su C++ – Functions

C++ – Do While Statement

C++ – Do While Statement

Working sample:


// custom countdown using while
#include <iostream>
using namespace std;

int main ()
{
  int n = 10;

  while (n>0) {
    cout << n << ", ";
    --n;
  }

  cout << "liftoff!\n";
}

The result:

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!


// echo machine
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str;
  do {
    cout << "Enter text: ";
    getline (cin,str);
    cout << "You entered: " << str << '\n';
  } while (str != "goodbye");
}

The result:

Enter text: hello
You entered: hello
Enter text: who’s there?
You entered: who’s there?
Enter text: goodbye
You entered: goodbye
Process returned 0

By |C++, Video Games Development|Commenti disabilitati su C++ – Do While Statement

C++ – Strings

C++ – Strings

Inizialization


...
string mystring = "This is a string";
string mystring ("This is a string");
string mystring {"This is a string"};
...

Basic Statement


// include standard input and output operations
#include <iostream>
// include strings
#include <string>
using namespace std;

int main ()
{
  string mystring;
  mystring = "This is the initial string content";
  // character output - variable - end line
  cout << mystring << endl;
  mystring = "This is a different string content";
  cout << mystring << endl;
  return 0;
}

Escape Codes

\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\’ single quote (‘)
\” double quote (“)
\? question mark (?)
\\ backslash (\)

By |C++, Video Games Development|Commenti disabilitati su C++ – Strings

Unreal Game Engine – Blueprints – Overview

Unreal Game Engine – Blueprints – Overview

Blueprints are the Visual Programming System of Unreal Game Engine.
They allow non-programmers to create features by themselves.
To create behaviours you will need only create relatioship between blocks of a flow chart!

How to create a simple Blueprint?
It is really easy and funny, in the next sample we toggle a light using Blueprints.

Object -> Create Blueprint

1. LEFT COLUMN> Modes> Light DRAG AND DROP a Point Light in the scene

2. LEFT COLUMN> Modes> Basic> DRAG AND DROP a Box Trigger in the scene

3. RIGHT COLUMN> Actor: TriggerBox> Blueprint> Add Level Events for TriggerBox> Add OnActorBeginOverlap> Blueprint Window appears

4a. 3D Viewport> select the Point Light
4b. Blueprint Window> RMB in a empty area> Call Function On Point light> Rendering> Toggle Visibility

5 Blueprint Window> connect OnActorBeginOverlap output—>input Toggle Visibility

6. MAIN TOP TOOLBAR> Play this level (the joypad icon), move the player over the Box Trigger to turn light on/off

unreal-blueprints-overview

Blueprint -> Attach Component

1. LEFT COLUMN> Content Browser> select Blueprints folder> New> ‘Actor’
2. name it MyBlueprint, DOUBLE click on it
3. Blueprint window> TOP RIGHT> Components
4. Blueprint window> LEFT COLUMN> Components> Add Component> Rendering> Billboard
5. Blueprint window> TOP RIGHT> Graph
6. LEFT COLUMN> MyBlueprint> +variable> name it MyVar
7. LEFT COLUMN> Details> Variable Name ‘MyVar’, Variable Type> bool
8. LEFT COLUMN> MyBlueprint> DRAG AND DROP MyVar into Event Graph> Get -> after that you can’t change the Variable Type

… and so on to create your Blueprint …

By |Unreal Engine, Video Games Development|Commenti disabilitati su Unreal Game Engine – Blueprints – Overview

Unreal Engine – Editor Overview

Unreal Engine – Editor Overview

Place New Objects

Window> Modes> Place> Geometry> DRAG AND DROP the game object thumb over the 3D Viewport

OR

Window> Content Browser> DRAG AND DROP the game object thumb over the 3D Viewport

Move/Scale/Rotate/Setup Objects

LMB select the object and use the top right small icons to select transformations tools, after that use the handles on screen

OR

1. Window> Scene Outliner> click over the object list to select
2. Window> Details> setup

Static Mesh Editor

RMB over a mesh> (home icon)Edityourmesh

Parenting objects

RIGHT COLUMN> Scene Outliner> DRAG AND DROP an object over another object, the dragged object will be the child of the second one.

Material Editor

RIGHT COLUMN> Details> Materials> DOUBLE CLICK over material preview

Move Camera View

LMB: rotate camera right left and move forward / backward

MMB: move left / right / up / down

Mouse Whell: zoom in / out

RMB: look at right / left / up / down

RMB + WASD: look and move like a FPS

ALT + LMB: orbit around the selected object

By |Unreal Engine, Video Games Development|Commenti disabilitati su Unreal Engine – Editor Overview