<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Toru Hisai</title>
    <description>The latest articles on DEV Community by Toru Hisai (@torus).</description>
    <link>https://dev.to/torus</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F48339%2F2d6287a8-daff-4d3f-a3ab-c93efa29c9f6.jpeg</url>
      <title>DEV Community: Toru Hisai</title>
      <link>https://dev.to/torus</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/torus"/>
    <language>en</language>
    <item>
      <title>Higher-Order Blueprint</title>
      <dc:creator>Toru Hisai</dc:creator>
      <pubDate>Wed, 01 May 2019 16:50:35 +0000</pubDate>
      <link>https://dev.to/torus/higher-order-blueprint-22oi</link>
      <guid>https://dev.to/torus/higher-order-blueprint-22oi</guid>
      <description>&lt;p&gt;In this article, we'll see how higher-order/functional programming can be done with Blueprint on Unreal Engine 4.&lt;/p&gt;

&lt;h2&gt;
  
  
  Actor as an Environment
&lt;/h2&gt;

&lt;p&gt;Create a new Actor Blueprint.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5a78990dvt65p5zdo09s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5a78990dvt65p5zdo09s.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the Level Blueprint and spawn an instance of the Actor class on the BeginPlay event.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fakd1qlphgmih3b9egksz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fakd1qlphgmih3b9egksz.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can put an instance of the actor directly on the level, but every time modifying the Actor Blueprint, the level also somehow updated. This is annoying for source control.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event as a Function
&lt;/h2&gt;

&lt;p&gt;We use Events to represent functions. You can also use Blueprint functions, but in this article we consider events as functions.&lt;/p&gt;

&lt;p&gt;Let's create a Hello World!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkkmdzhrbwx9bixeh1dyu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkkmdzhrbwx9bixeh1dyu.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another example, Celsius to Fahrenheit converter, can be like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjrn99d6en2jb6dty24r2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjrn99d6en2jb6dty24r2.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning Value with Another Event
&lt;/h2&gt;

&lt;p&gt;As seen above, events can behave like functions. But unlike functions, an event can't return a value.&lt;/p&gt;

&lt;p&gt;To pass some value from the function to the caller, let's create an another event to receive the result from a function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6q2zv9etqdmjilypugdg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6q2zv9etqdmjilypugdg.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, the Celsius to Fahrenheit converter with returned value can be like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbgnyub755v97ygfnyxk2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbgnyub755v97ygfnyxk2.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Recursive Call
&lt;/h2&gt;

&lt;p&gt;An event doesn't have variable scope, it's not possible to call an event recursively without using variables.&lt;/p&gt;

&lt;p&gt;Let's see a factorial function implemented in JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;fact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;fact()&lt;/code&gt; is called from inside &lt;code&gt;fact()&lt;/code&gt; function. With JavaScript, the value of &lt;code&gt;x&lt;/code&gt; is stored in the call stack. So, each &lt;code&gt;x&lt;/code&gt; in a different level can hold different value.&lt;/p&gt;

&lt;p&gt;To emulate this, we can create another Actor instance to represent another stack frame.&lt;/p&gt;

&lt;p&gt;Before implementing a Blueprint version of factorial function, let's create a utility function, a real Blueprint function, to spawn an instance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhg7a9171hd2qql7i29ui.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fhg7a9171hd2qql7i29ui.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this function, let's make Factorial function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbo3mh7qxd7wh1lvxgkus.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbo3mh7qxd7wh1lvxgkus.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Continuation Passing Style
&lt;/h2&gt;

&lt;p&gt;On above examples, we had used an event to receive a returned value from a function (event). But this is not very flexible. The factorial function can't return the result to the top level caller.&lt;/p&gt;

&lt;p&gt;To make this more flexible, we can use event dispatchers. With an event dispatcher, we can specify different kind of events to handle returned value accordingly.&lt;/p&gt;

&lt;p&gt;Let's see the Fibonacci function. It can be written in JavaScript like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;fib&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, &lt;code&gt;fib()&lt;/code&gt; is called twice. Therefore the returned value from each of them needs to be handled differently.&lt;/p&gt;

&lt;p&gt;In Blueprint, it can be implemented like below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdimkvokxnby4fu5xyr9q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fdimkvokxnby4fu5xyr9q.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Events &lt;code&gt;result_Fibonacci1&lt;/code&gt; and &lt;code&gt;result_Fibonacci2&lt;/code&gt; handle the returned value from each inner function call.&lt;/p&gt;

&lt;p&gt;Additionally, you can simplify this graph a little bit by "Collapse to Function".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F380qq6uapgjzoj4jvmwa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F380qq6uapgjzoj4jvmwa.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that if you collapse some nodes including binding an event to a event dispatcher, then the generated function will have a Delegate input.&lt;/p&gt;

&lt;p&gt;Below is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fs9740f5d1cvgzn3mhgvw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fs9740f5d1cvgzn3mhgvw.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With an event dispatcher and events to handle the returned value, we can code a function with continuation passing style.&lt;/p&gt;

&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;A function which maps an array to an another array.&lt;/p&gt;

&lt;p&gt;For loop.&lt;/p&gt;

&lt;p&gt;(TBD)&lt;/p&gt;

&lt;h2&gt;
  
  
  Fold
&lt;/h2&gt;

&lt;p&gt;A function which accumulates all the values in an array into a value.&lt;/p&gt;

&lt;p&gt;(TBD)&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>unreal</category>
      <category>blueprint</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
