indiegamedev

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

Unity3D – What is Object Pooling

Unity3D – Object Pooling

What is a pool?
In computer science, a pool (piscina) is a set of initialised resources that are kept ready to use, rather than allocated and destroyed on demand.
A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished with an object (or resource), it returns it to the pool rather than destroying it.

What is object pooling?
In games you need to manage a lot gameobjects at one time (bullets, enemies etc…), and istantiate and destroy every object can slow down your game performance.
To improve performance and save CPU time the best way is create a pool of gameobjects and request gameobjects from the pool.

For example you can have an gameObject off screen named “enemies” which holds all the enemies, and a master script to manage it:

enemies (Empty Game Object) -> master script ‘Enemy.js’
|
|—-enemy
|
|—-enemy
|
|—-enemy

Another example is reuse bullets:

Slower performance:
1. Awake(), do nothing
2. Update()
a- Instantiate bullet in its start position
b- Move along the way
c- Move bullet in its final position
d- Destroy bullet

Object Pooling. best performance:
1. Awake(), create a pool off screen
2. Update ()
a- Get gameobject bullet from the pool
b- SetActive -> true, Move bullet in its start position
c- Move along the way
d- Move bullet in its final position
e- SetActive -> false
f- Reuse from point b- and so on…

In terms of performance for real scenarios, if you use only 10-50 gameobjects at one time the benefits of object pooling are irrelevant.

In Unity3D you will need object pooling when you have thousands of gameObjects at one time or if you develop for slower mobile devices.

References:

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – What is Object Pooling

Unity3D – Scrolling Typewriter effect – JavaScript

Unity3D – Scrolling Typewriter effect – JavaScript

Create a scene with:

– TextMesh
– Empty Object, assign TypeWriter.js

#pragma strict

var displayText : TextMesh;

var textShownOnScreen: String ;
var fullText : String = "The text you want shown on screen with typewriter effect.";
var wordsPerSecond : float = 2; // speed of typewriter, words per second
var timeElapsed : float = 0;   
 
function Update()
{
    timeElapsed += Time.deltaTime;
    textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond);
    displayText.text = textShownOnScreen;
}
 
function GetWords(text : String, wordCount : int): String
{
    var words : int = wordCount;
 
    // loop through each character in text
    for (var i : int = 0; i < text.Length; i++)
    { 
        if (text[i] == ' ')
        {
            words--;
        }
 
        if (words <= 0)
        {
            return text.Substring(0, i);
        }
    }
 
    return text;
}

Inspector, assign a 3DText to var displayText.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Scrolling Typewriter effect – JavaScript

Unity3D – Explosion Force – JavaScript

Unity3D – Explosion Force – JavaScript

Create a scene with:

– Plane (Collider)

– Cube1 (Collider + Rigid Body)
– Cube2 (Collider + Rigid Body)
– Cube3 (Collider + Rigid Body)

– Sphere (SphereExplosion.js)

SphereExplosion.js:

#pragma strict

	var radius = 50.0; // radius of explosion
	var power = 500.0; // power of explosion
	var bombObject : GameObject; // Assign in Inspector the Bomb GameObject
	
	
function Start () {
}
	
function OnMouseDown ()
{
        // When you click over the object
        
                // Force START -----------------------------------------------------------
                // Applies an explosion force to all nearby rigidbodies
		var explosionPos : Vector3 = bombObject.transform.position;
		// Returns an array with all colliders touching or inside the sphere.
		var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
		
		for (var hit : Collider in colliders) {
			if (hit && hit.rigidbody)
				hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
		}
		 // Force END -------------------------------------------------------------
		
}

function Update () {
}

Inspector> Assign the Sphere Mesh to var bombObject, setup var radius and var power

Play and click over the Sphere… boom!!!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Explosion Force – JavaScript

Unity3D – Destroy if transform.position

Unity3D – Destroy if transform.position

Create a Cube and attach Cube.js:


#pragma strict

function Updare(){
	Destroy();
}

function Destroy(){
    // destroy this game object if y < 2 
    var posY : float = gameObject.transform.position.y;
    if (posY <= -2 ) {
    Destroy(gameObject);
    }
} // END Destroy

Play and move the cube under 2 units in y

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Destroy if transform.position