proletariatgames / unreal.hx

Unreal.hx: Haxe Integration for Unreal
MIT License
423 stars 42 forks source link

Unreal.hx

Unreal.hx is a plugin for Unreal Engine 4 that allows you to write code in the Haxe programming language. Haxe is a modern, high-level, type-safe programming language that offers high performance critical for game development.

Main Features

Haxe Features

Setup

Examples

package mygame;
import unreal.*;

@:uclass
class AMyActor extends AActor {
  // make a property that is editable in the editor.
  @:uproperty(EditDefaultsOnly, Category=Inventory)
  var items:TArray<AActor>;

  // make a function that is callable from C++/Blueprints
  @:ufunction(BlueprintCallable, Category=Inventory)
  public function addToInventory(item:AActor) {
    items.push(item);
  }

  // override native C++ function
  override function Tick(deltaTime:Float32) : Void {
    super.Tick(deltaTime); // call super functions
    trace('Hello, World!'); // all traces are redirected to Unreal's Log
    trace('Warning', 'Some Warning'); // and it supports Warning, Error and Fatal as well
  }

  // tell Unreal to call our Tick function
  public function new(wrapped) {
    super(wrapped);
    this.PrimaryActorTick.bCanEverTick = true;
  }
}
package mygame;
import unreal.*;

@:uclass
class AMyGameMode extends AGameMode {
  @:uproperty(BlueprintReadWrite)
  public var playerName:FString;

  @:ufunction(BlueprintCallable)
  public function makeExternalRequest(data:String) {
    // use Unreal C++ API to make an HTTP Request
    var httpRequest = FHttpModule.Get().CreateRequest();
    httpRequest.SetVerb("GET");
    httpRequest.SetHeader("Content-Type", "application/json");
    httpRequest.SetURL("www.mygame.com/players");

    // use Haxe JSON library to encode data.
    var json = {
      name: this.playerName,
      playerData: data,
    };
    httpRequest.SetContentAsString(haxe.Json.stringify(json));

    // Receive a callback when the response is received.
    httpRequest.OnProcessRequestComplete.BindLambda(function (request, response, success) {
      trace('response received: ${response.GetContentAsString()}');
    });

    httpRequest.ProcessRequest();
  }
}

For a complete example project, check out https://github.com/waneck/HaxePlatformerGame , which is a port of Unreal's Platformer Game demo to Haxe 3.3/Unreal 4.11

More information is available on the Wiki