Unity3D – Game Engine – Script Lifecycle Flowchart

Unity3D – Game Engine – Script Lifecycle Flowchart

monobehaviour_flowchart

My website: http://www.lucedigitale.com

Ref: http://docs.unity3d.com/Manual/ExecutionOrder.html

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Game Engine – Script Lifecycle Flowchart

Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

To create videogames Unity3D give us special functions to manage special operations as variable inizialization, updating, physic management.


using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour
{

        // Initialization Events
        // ******************************************

	void Awake ()
	{
		// first inizialization
		Debug.Log("Awake called.");
	}
	
	
	void Start ()
	{
		// second inizialization
		Debug.Log("Start called.");
	}

        // Regular Update Events
        // ******************************************

	void FixedUpdate ()
	{
		// frame indipendent update
                // use this function to drive physic
		Debug.Log("FixedUpdate time :" + Time.deltaTime);
	}
	
	void Update ()
	{
		// update at every frame
		Debug.Log("Update time :" + Time.deltaTime);
	}

	void LateUpdate () 
	{
		// update at the end of the frame rendering
		Debug.Log("Late Update");
	}

        // GUI Events
        // ******************************************

        void OnGUI() 
        {
                // draw here labels, buttons and other Graphic User Interface elements
        	GUI.Label(labelRect, "Game Over");
        }

        // Mouse Events
        // ******************************************

        void OnMouseOver() 
        {
                // do something if mouse is over
        }

        void OnMouseDown() {
                // do something if mouse is down
        }

        // Physics Events
        // ******************************************

        void OnCollisionEnter(otherObj: Collision) 
        {
                // do stuff if there is a collision
        }

        void OnTriggerEnter(Collider other) 
        {
                // do stuff if there is an object that overlap the trigger
        }

} // END MyScript

My website: http://www.lucedigitale.com

Ref: http://docs.unity3d.com/Manual/EventFunctions.html

By |CSharp, Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – CSharp – Event Functions – Inizialization – Update – GUI – Mouse – Physics

Unity3D – C# – Basic Syntax

Unity3D – C# – Basic Syntax

First you need call basic namespace and nest all the code inside a class with the name of your script, see the example below.

Basic Structure

Project Window> RMB> Create> C#> name it ‘MyFirstScript’> double click to open ‘MonoDevelop’ editor


// call basic namespace of Unity3D
using UnityEngine;
using System.Collections;

// MonoBehaviour is the base class every script derives from
// class nameofmyscript : derived from MonoBehaviour
public class MyFirstScript : MonoBehaviour {

// Unity3D exclusive function - Use this for initialization
	void Start () {
	
	}// END Start()

	
// Unity3D exclusive function - Update is called once per frame
	void Update () {
	
	}// END Update()

}// END of class MyFirstScript

Variables and Fuctions


// call basic namespace of Unity3D
using UnityEngine;
using System.Collections;

// MonoBehaviour is the base class every script derives from
// scope class nameofmyscript : derived from MonoBehaviour
public class MyFirstScript : MonoBehaviour {
	
	// declare variables
	// scope type nameofvariable = value
	public int myInt = 5;   // you can setup it inside inspector
	private int myInt2 = 6;	// you can't setup it inside inspector
	
	// Unity3D exclusive function - Use this for initialization - void because it has no type
	void Start () {
		myInt = MultiplyByTwo(myInt);
		Debug.Log (myInt);
		
	}// END Start()
	
	// Your custom fuction
	// scope type functionname (type variable)
	public int MultiplyByTwo (int number)
	{
		int ret;
		ret = number * 2;
		return ret;
	}
	
	// Unity3D exclusive function - Update is called once per frame - void because it has no type
	void Update () {
		
	}// END Update()
	
}// END of class MyFirstScript

By |CSharp, Unity3D, Video Games Development|Commenti disabilitati su Unity3D – C# – Basic Syntax

Unreal Game Engine – C++ – Variables

Unreal Game Engine – C++ – Variables

You have to declare variables in your header file, see the sample below:

HelloWorldPrinter.h


#pragma once

#include "GameFramework/Actor.h"
#include "HelloWorldPrinter.generated.h"

/**
*
*/
UCLASS() // Questo rende Unreal Engine consapevoli della vostra nuova classe
class CODETESTPROJECT_API AHelloWorldPrinter : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY() // Questo rende Unreal Engine consapevole della vostra nuova proprietà

	int32 MyNumberInt;   // declare an integer variable, 32-bit unsigned
	float MyNumberFloat; // declare a float number

	virtual void BeginPlay() override;


};// END UCLASS

To display variables values use your source file, see the sample below:


#include "CodeTestProject.h"
#include "HelloWorldPrinter.h" 

//Your class constructor
AHelloWorldPrinter::AHelloWorldPrinter(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	MyNumberInt = 12;
	MyNumberFloat = 4.5;
}

// Dichiara la funzione - eseguita in BeginPlay() che è la funziona che unreal esegue all'inizio del game
void AHelloWorldPrinter::BeginPlay()
{
	Super::BeginPlay();

	if (GEngine) // Controllo se è valido l'oggetto globale GEngine
	{
		// Visualizza il testo a video
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("My Variables: "));
		// Visualizza le varibili - key - time - color - string - variable
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(MyNumberInt));
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::SanitizeFloat(MyNumberFloat));
	
	}
}

The result is:
4.5
12
My Variables:

NOTA BENE: sono messaggi di debug, vengono dati in sequenza, quello più in alto è l’ultimo.

By |Unity3D, Video Games Development|Commenti disabilitati su Unreal Game Engine – C++ – Variables

Unreal Game Engine – C++ Programming – Quick Start Guide

Unreal Game Engine – C++ Programming – Quick Start Guide

C++ (pronounced cee plus plus) is a general purpose programming language. It has imperative, object-oriented and generic programming features, while also providing the facilities for low level memory manipulation.

Unreal Engine does not have an internal code editor, you need to install Visual Studio od Visual studio Express.

1. Install the latest version of Internet Explorer
2. Install Visual Studio Express
3. Run Visual Studio

Increase the width of the Solution Configurations dropdown menu:
a. Right-click on the toolbar and select Customize (Personalizza) at the bottom of the menu that appears.
b. Click on the Commands tab (Comandi).
c. Select the Toolbar (Barra degli strumenti) radio button.
d. In the dropdown next to Toolbar, choose Standard.
e. In the Controls list at the bottom, select the Solution Configurations control (Configurazioni soluzione).
f. Click Modify Selection (Modifica selezione) on the right. Set the Width (larghezza) to “200”.
g. Click Close. Your toolbar should update immediately.

Add the Solution Platforms dropdown
Find the right-most button on the Standard toolbar, which opens a dropdown menu that will allow you to add and remove buttons from the toolbar.
Click the dropdown button, hover over Add or Remove Buttons, and then click on Solution Platforms (Piattaforme soluzione) to add the menu to the toolbar.

Turn off the Error List window
With Unreal Engine, the Error List can display false error information.

a. Close the Error List window if it is open.
b. From the Tools menu, open the Options dialog (Strumenti> Opzioni).
c. Select Projects and Solutions (Progetti e soluzioni) and uncheck Always show error list if build finishes with error (Mostra sempre Elenco errori se la compilazione finisce con errori).
d. Click OK

Create a New Project

1. Open Unreal Editor> New Project> Blank, name it ‘CodeTestProject’ uncheck ‘Include starter content’, click on ‘Create Project’ button

Add a New Class

1. MAIN TOP MENU> File> Add Code to Project> Actor
2. Name it ‘HelloWorldPrinter’> click ‘Create Class’ button, it will create in your project folder:
– HelloWorldPrinter.h (header)
– HelloWorldPrinter.cpp (c plus plus source)
3. In the next floating window click ‘Yes’ to edit now, Unreal will run Visual Studio Express

Visual Studio takes several minutes to analysing files, includes etc…, at the end you will se the message ‘Ready’ (Pronto), in the RIGHT COLUM> Solution Explorer (Esplora Soluzioni) you will see Games/CodeTestProject

Write the code:

HelloWorldPrinter.h


#pragma once

#include "GameFramework/Actor.h"
#include "HelloWorldPrinter.generated.h"

/**
 * 
 */
UCLASS() // Questo rende Unreal Engine consapevoli della vostra nuova classe
class CODETESTPROJECT_API AHelloWorldPrinter : public AActor
{
	GENERATED_UCLASS_BODY()

	UPROPERTY() // Questo rende Unreal Engine consapevoli della vostra nuova proprietà
	int32 MyNumber; // declare an integer variable

	virtual void BeginPlay() override;// Questo codice dichiara una funzione, ma lo fa eseguendo l'override di una funzione dalla classe padre.


};// END UCLASS

HelloWorldPrinter.cpp


#include "CodeTestProject.h"
#include "HelloWorldPrinter.h" 

//Your class constructor
AHelloWorldPrinter::AHelloWorldPrinter(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	MyNumber = 12;
}

// Dichiara la funzione - eseguita in BeginPlay() che è la funziona che unreal esegue all'inizio del game
void AHelloWorldPrinter::BeginPlay()
{
	Super::BeginPlay();

	if (GEngine) // Controllo se è valido l'oggetto globale GEngine
	{
		// Visualizza il testo a video
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Hello World!"));
		// Visualizza la variabile MyNumber
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(MyNumber));
	}
}

Save All

Compile the code

1. RIGHT COLUMN> Solution Explorer (Eslora Soluzioni)> RMB over yourproject (|++|CodeTestProject)> Build (Compila)

The process can take several minutes because it compile all the dependencies.

Using the New Class

1. Close Unreal Editor
2. Reopen the Unreal Editor and reload the Project
3. MAIN TOP MENU> Class Viewer> Filters> uncheck ‘Blueprint Bases Only’, search ‘HelloWorldPrinter’
4. DRAG and DROP Actor>HelloWorldPrinter (the Sphere icon), from the Class Viewer into 3D Viewport.
5. Play

Change the Class

1. Close Unreal Editor (if you do not close the Unreal Editor, Visual Studio can not overwrite the old file)
2. Inside Visual Studio open HelloWorldPrinter.cpp
3. Change … Yellow, TEXT(“Hello World!”) to …Yellow, TEXT(“Hello World Two!”)
4. RMB over yourproject (|++|CodeTestProject)> Build (Compila), the process takes only few seconds.
5. Reopen Unreal Editor
6. DRAG AND DROP Actor>HelloWorldPrinter (the Sphere icon), from the Class Viewer into 3D Viewport.
5. Play

Reference: https://docs.unrealengine.com/latest/INT/Programming/QuickStart/1/index.html

Editing an existing Project on the fly

0. Open Visual Studio> TOP TOOLBAR> Solutions Configurations (Configurazioni soluzioni)> DebugGame Editor
Questo significa che possiamo debuggare e aprire il progetto direttamente

1. Open Visual Studio Express> MAIn TOP MENU> File> Open Project (Apri Progetto)> select ‘CodeTestProject.sln’
2. RIGHT COLUMN> Solutions Explorer> click over Games/CodeTestProject/Source/HelloWorldPrinter.cpp
3. Change the script and save it
4. RIGHT COLUMN> Solutions Explorer> RMB over Games/CodeTestProject> Debug> Run New Instance (Avvia Nuova Istanza)

5. Unreal Enditor will be opened

Play to see changes

6. Switch to Visual Studio, change the script, save it.
7. Switch to Unreal Editor, MAIN TOP TOOLBAR> Compile> Recompile Game Code (Recompile and Reload C++ code on the fly)

Play to see changes
4. File> Save HelloWorldPrinter.cpp

For italian people: come funziona?

1. Visual Studio viene settato per permettere il debug direttamente all’interno di Unreal editor con ‘Configurazioni soluzioni> DebugGame Editor’
2. da visual Studio creo un ponte verso Unreal Editor con ‘Avvia Nuova Istanza’
3. da Unreal Editor posso ricompilare al volo direttamente dall TOOLBAR superiore con ‘Recompile Game Code’

By |Unity3D, Video Games Development|Commenti disabilitati su Unreal Game Engine – C++ Programming – Quick Start Guide