{"id":1443,"date":"2020-01-18T23:03:28","date_gmt":"2020-01-18T23:03:28","guid":{"rendered":"https:\/\/www.danielparente.net\/en\/2020\/01\/18\/getting-started-with-typescript-in-unreal-engine\/"},"modified":"2020-01-18T23:03:28","modified_gmt":"2020-01-18T23:03:28","slug":"getting-started-with-typescript-in-unreal-engine","status":"publish","type":"post","link":"https:\/\/www.danielparente.net\/en\/2020\/01\/18\/getting-started-with-typescript-in-unreal-engine\/","title":{"rendered":"Getting Started With TypeScript in Unreal Engine"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div>\n<div>\n<div class=\"ex\">\n<div class=\"n ey ez fa fb\">\n<div class=\"o n\">\n<div><a href=\"https:\/\/itnext.io\/@imseansullivan?source=post_page-----f1a70adb624----------------------\" rel=\"noopener\" target=\"_blank\"><img decoding=\"async\" alt=\"Sean Sullivan\" class=\"r fc fd fe\" src=\"https:\/\/miro.medium.com\/fit\/c\/96\/96\/1*Gph4xdnpU_RwuVlYOflJTA.jpeg\" width=\"48\" height=\"48\"\/><\/a><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<figure class=\"gr gs gt gu gv gw do dp paragraph-image\"\/>\n<p id=\"14dd\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Chances are, you\u2019ve heard of games like Fortnite and Player Unknown\u2019s Battlegrounds. Both games were built in Unreal Engine 4. Unreal Engine has been a favorite among triple-A game developers and indie studios alike. Ever since Epic Games released the engine as free-to-use back in 2015, many eager developers and enthusiasts have tried their hand at creating games in the robust 3D engine. A majority of hobbyists and small teams opt for the engine\u2019s built in blueprint editor rather than script in C++. While Unreal\u2019s blueprint editor is very simple to use, connecting many nodes in a GUI interface can seem cumbersome to some developers. Luckily, the JavaScript community always finds a way to pair their favorite language with anything, thus giving us <a href=\"https:\/\/github.com\/ncsoft\/Unreal.js\" class=\"dc by ib ic id ie\" target=\"_blank\" rel=\"noopener noreferrer\">Unreal.JS<\/a>. Unreal.js is a V8 runtime for Unreal Engine that can be an alternative to, or run alongside existing C++ and Blueprint scripts. The only thing that could make developing a large 3D game with JS easier is the infinitely-scalable TypeScript. Here, I will show you how to get started with Unreal.js and TypeScript.<\/p>\n<p id=\"022e\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">First, if you haven\u2019t already, install Unreal Engine through the Epic Games Launcher. Next, in the launcher go to the marketplace and find Unreal.js, then run \u201cInstall to Engine\u201d.<\/p>\n<figure class=\"gr gs gt gu gv gw do dp paragraph-image\"\/>\n<p id=\"5880\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Open Unreal Engine and create a new project. In this example, I created a third-person blueprint project.<\/p>\n<p id=\"fcc2\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Once you have the editor open, go to \u201cEdit&gt;Plugins\u201d and make sure that \u201cUnreal JS\u201d is enabled. If it isn\u2019t, enable it and you will see a prompt to restart the editor.<\/p>\n<p id=\"2956\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Now in your project\u2019s content folder you should see a folder called \u201cScripts\u201d. If you don\u2019t have typescript installed, install it by running<\/p>\n<pre class=\"gr gs gt gu gv ih ii cm\"><span id=\"868e\" class=\"ij ik ef at il b fi im in r io\">npm install -g typescript<\/span><\/pre>\n<p id=\"b9f3\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Next, open up your terminal and navigate to the Scripts folder and run<\/p>\n<pre class=\"gr gs gt gu gv ih ii cm\"><span id=\"993d\" class=\"ij ik ef at il b fi im in r io\">tsc --init<\/span><\/pre>\n<p id=\"eccc\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">This should generate a tsconfig.json file. Open the file and change the settings to your preferences. At the bottom we will exclude the \u201caliases.js\u201d file from Unreal.js.<\/p>\n<pre class=\"gr gs gt gu gv ih ii cm\"><span id=\"56eb\" class=\"ij ik ef at il b fi im in r io\">tsconfig.json<br\/>...<br\/>...<\/span><span id=\"f8ad\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ \"emitDecoratorMetadata\": true,         \/* Enables experimental          support for emitting type metadata for decorators. *\/<\/span><span id=\"3d70\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">},<\/span><span id=\"1ace\" class=\"ij ik ef at il b fi ip iq ir is it in r io\"> \"exclude\": [<\/span><span id=\"c0a5\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">   \"aliases.js\"<\/span><span id=\"e8b0\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  ]<\/span><span id=\"1e12\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">}<\/span><\/pre>\n<p id=\"744a\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Now open the Scripts folder in your favorite editor and create a file called \u201chelloUnreal.ts\u201d.<\/p>\n<p id=\"8957\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">For now let\u2019s just add a console message. In the file add<\/p>\n<pre class=\"gr gs gt gu gv ih ii cm\"><span id=\"7277\" class=\"ij ik ef at il b fi im in r io\">console.log(\u2019HELLO UNREAL!\u2019)<\/span><\/pre>\n<p id=\"424b\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Now in our terminal let\u2019s run the TypeScript Compiler.<\/p>\n<pre class=\"gr gs gt gu gv ih ii cm\"><span id=\"9d3f\" class=\"ij ik ef at il b fi im in r io\">tsc helloUnreal.ts<\/span><\/pre>\n<p id=\"2f78\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">This should create a \u201chelloUnreal.js\u201d file.<\/p>\n<p id=\"53ab\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Open up the unreal editor and in the ThirdPersonExampleMap, go to \u2018Blueprints\u2019 and open the event graph. In the event graph we will add an event that triggers when the game is started called \u201cEventBeginPlay\u201d. The easiest way to add the event is to right click on the blueprint and search for it.<\/p>\n<figure class=\"gr gs gt gu gv gw do dp paragraph-image\"\/>\n<p id=\"7cae\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Next we will find the function for adding a JavaScript Component using the same method and connect the two nodes together by clicking and dragging. Select the JavaScript Component, and then on the \u201cDetails\u201d tab in the right side of the editor add our \u201chelloUnreal.js\u201d file to the \u201cScript Source File\u201d input box.<\/p>\n<figure class=\"gr gs gt gu gv gw do dp paragraph-image\"\/>\n<figure class=\"gr gs gt gu gv gw do dp paragraph-image\"\/>\n<p id=\"2a9d\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Now make sure to click \u201cCompile\u201d and \u201cSave\u201d and that should be it. Now we can open our JavaScript Console and test our code. Go to \u201cWindow&gt;Developer Tools&gt;Javascript Console\u201d and open the console.<\/p>\n<p id=\"8f3c\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Now click the \u201cPlay\u201d button and run the game. In the console, you should see the \u201cHELLO UNREAL!\u201d message that we added earlier.<\/p>\n<figure class=\"gr gs gt gu gv gw do dp paragraph-image\"\/>\n<p id=\"34d7\" class=\"hn ho ef at hp b hq jn hs jo hu jp hw jq hy jr ia\">Ok, now let\u2019s try and actually do something to our game using Unreal.js. First, to initiate a live reloading dev environment, let\u2019s create a bootstrap file called \u201cbootstrap.ts\u201d that will follow the <a href=\"https:\/\/github.com\/ncsoft\/Unreal.js\/blob\/master\/Examples\/Content\/Scripts\/bootstrap.js\" class=\"dc by ib ic id ie\" target=\"_blank\" rel=\"noopener noreferrer\">Unreal.js Example<\/a>.<\/p>\n<pre class=\"gr gs gt gu gv ih ii cm\"><span id=\"6550\" class=\"ij ik ef at il b fi im in r io\">\/\/\/ &lt;reference path=\"typings\/ue.d.ts\"&gt;\/&gt;<\/span><span id=\"5af8\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">export default function (filename:string) {<\/span><span id=\"b91c\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">    Context.RunFile('aliases.js')<\/span><span id=\"b24f\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">    Context.RunFile('polyfill\/unrealengine.js')<\/span><span id=\"ba7b\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">    Context.RunFile('polyfill\/timers.js')<\/span><span id=\"db12\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  require('devrequire')(filename)<\/span><span id=\"71e8\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">}<\/span><\/pre>\n<p id=\"47d5\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Now let\u2019s run TypeScript to watch for changes as we edit our helloUnreal.ts file with<\/p>\n<pre class=\"gr gs gt gu gv ih ii cm\"><span id=\"9cd2\" class=\"ij ik ef at il b fi im in r io\">tsc -w<\/span><\/pre>\n<p id=\"3103\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Let\u2019s print our \u201cHELLO UNREAL!\u201d message to the HUD by creating an actor. Following the <a href=\"https:\/\/github.com\/ncsoft\/Unreal.js\/blob\/master\/Examples\/Content\/Scripts\/helloCanvas.js\" class=\"dc by ib ic id ie\" target=\"_blank\" rel=\"noopener noreferrer\">helloCanvas example<\/a>. Open up helloUnreal.ts and add the following code:<\/p>\n<pre class=\"gr gs gt gu gv ih ii cm\"><span id=\"c327\" class=\"ij ik ef at il b fi im in r io\">\/\/\/ &lt;reference path=\"typings\/ue.d.ts\"&gt;\/&gt;<\/span><span id=\"ad9a\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ find a local player controller<\/span><span id=\"7fe2\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">function GetPC() {<\/span><span id=\"a455\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ out-ref function returns an object which contains out-ref params and return param.<\/span><span id=\"9d42\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ eg) bool USomeClass::Func(int a, int* b, int* c); -&gt; {$:{bool}, b:{int}, c:{int}}<\/span><span id=\"1932\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">return PlayerController.C(GWorld.GetAllActorsOfClass(PlayerController).OutActors[0])<\/span><span id=\"f519\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">}<\/span><span id=\"8d4d\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">function main(){<\/span><span id=\"de50\" class=\"ij ik ef at il b fi ip iq ir is it in r io\"> class MyHUD extends HUD {<\/span><span id=\"7b05\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">    ctor() {<\/span><span id=\"2086\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">    }<\/span><span id=\"a100\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ override ReceiveDrawHUD<\/span><span id=\"c444\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">   ReceiveDrawHUD() {<\/span><span id=\"62f0\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">   var text:string = 'HELLO UNREAL!';<\/span><span id=\"5543\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">   this.Canvas.DrawText(<\/span><span id=\"a30e\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      GEngine.SmallFont,<\/span><span id=\"158c\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      text,<\/span><span id=\"201c\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      {X:this.Canvas.SizeX\/2,Y:this.Canvas.SizeY\/2},<\/span><span id=\"d7e6\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      {R:1,G:1,B:1,A:1},<\/span><span id=\"fee7\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      0,<\/span><span id=\"a68f\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      {R:0,G:0,B:0,A:1},<\/span><span id=\"0499\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      {X:1,Y:1},<\/span><span id=\"c191\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      true,true,true,<\/span><span id=\"73c4\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">      {R:0,G:0,B:0,A:1}<\/span><span id=\"0d4a\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">    )<\/span><span id=\"4ff4\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  }<\/span><span id=\"378d\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">}<\/span><span id=\"c938\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ register new HUD class<\/span><span id=\"407b\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">let MyHUD_C = require('uclass')()(global,MyHUD);<\/span><span id=\"5e09\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ and instantiate it<\/span><span id=\"b0ec\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  GetPC().ClientSetHUD(MyHUD_C)<\/span><span id=\"bc64\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ no mess to clean<\/span><span id=\"9fe6\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  return function () {<\/span><span id=\"1cb8\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  }<\/span><span id=\"5faa\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">}<\/span><span id=\"c3e2\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">import bootstrap from \".\/bootstrap\"<\/span><span id=\"c5f8\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ bootstrap to initiate live-reloading dev env.<\/span><span id=\"ab4a\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">try {<\/span><span id=\"964f\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  module.exports = () =&gt; {<\/span><span id=\"e775\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">   let cleanup:Function|null = null<\/span><span id=\"480a\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ wait for map to be loaded.<\/span><span id=\"bb73\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">process.nextTick(() =&gt; cleanup = main());<\/span><span id=\"408f\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">\/\/ live-reloadable function should return its cleanup function<\/span><span id=\"822f\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  return () =&gt; (&lt;Function&gt;cleanup)()<\/span><span id=\"f1a1\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  }<\/span><span id=\"c040\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">}<\/span><span id=\"ba93\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">catch (e) {<\/span><span id=\"49f9\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">  bootstrap('helloUnreal')<\/span><span id=\"20d0\" class=\"ij ik ef at il b fi ip iq ir is it in r io\">}<\/span><\/pre>\n<p id=\"29e1\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Now when we hit \u201cPlay\u201d in our game we should see the \u201cHELLO UNREAL!\u201d message in the center of the screen.<\/p>\n<figure class=\"gr gs gt gu gv gw do dp paragraph-image\"\/>\n<p id=\"2e81\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Notice that this example includes a cleanup function that isn\u2019t used. This function is very important if you are creating Actors in your script, however and you can see a basic example of it <a href=\"https:\/\/github.com\/ncsoft\/Unreal.js\/blob\/master\/Examples\/Content\/Scripts\/helloWorld.js\" class=\"dc by ib ic id ie\" target=\"_blank\" rel=\"noopener noreferrer\">here.<\/a><\/p>\n<p id=\"a22d\" class=\"hn ho ef at hp b hq hr hs ht hu hv hw hx hy hz ia\">Now we have an Unreal Engine project with <a href=\"https:\/\/github.com\/ncsoft\/Unreal.js\" class=\"dc by ib ic id ie\" target=\"_blank\" rel=\"noopener noreferrer\">Unreal.JS<\/a> that can be scripted using TypeScript. Happy coding!<\/p>\n<\/div>\n<p>[ad_2]<br \/>\n<br \/><a href=\"https:\/\/itnext.io\/getting-started-with-typescript-in-unreal-engine-f1a70adb624\" target=\"_blank\" rel=\"noopener\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Chances are, you\u2019ve heard of games like Fortnite and Player Unknown\u2019s Battlegrounds. Both games were built in Unreal Engine 4. Unreal Engine has been a favorite among triple-A game developers and indie studios alike. Ever since Epic Games released the engine as free-to-use back in 2015, many eager developers and enthusiasts have tried their [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1444,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[18],"tags":[],"class_list":["post-1443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gamedev"],"blocksy_meta":[],"jetpack_featured_media_url":"https:\/\/e928cfdc7rs.exactdn.com\/info\/uploads\/sites\/3\/2020\/01\/Getting-Started-With-TypeScript-in-Unreal-Engine.png?strip=all","jetpack_shortlink":"https:\/\/wp.me\/p2TFCd-nh","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts\/1443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/comments?post=1443"}],"version-history":[{"count":0,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/posts\/1443\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/media\/1444"}],"wp:attachment":[{"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/media?parent=1443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/categories?post=1443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.danielparente.net\/en\/wp-json\/wp\/v2\/tags?post=1443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}