Lightact can send to and receive variables from Unreal Engine 4 via shared memory. It does that using Lightact-UE4 plugin (if you haven’t downloaded it yet, you can do so here. If you need instructions on installing it, just head over to this article).
In this document, we’ll explain how to send variables from Unreal Engine to Lightact.
Writing to Shared Memory in Unreal Engine
Lightact UE4 plugin v2.0 adds additional 3 nodes that we will be using in this tutorial:
Open shared memory
This node is used to create a shared memory handle. It should be called once before all other nodes related to writing to shared memory (such as Write to shared memory and Close shared memory nodes). In most cases, you’ll want to connect it to Event BeginPlay node.
Write to shared memory
This node is used to write data to shared memory handle. It takes in a Map of Strings to Strings and converts it to JSON before writing it to shared memory.
Close shared memory
This node is for closing shared memory handle. It should be called once before quitting the game. The Handle Name property should be the same as in the Open shared memory node. In most cases, you’ll want to connect it to Event EndPlay node.
Opening and Closing Shared Memory
In Unreal Engine, in either Level Blueprint or a blueprint of one of the actors reproduce the following graph:
This graph, at Begin Play, creates a shared memory handle with vars2LA Handle Name and 1024 Handle Size. At End Play event, it closes the same shared memory handle. These values should, especially the Handle Name, match what you’ll type in to Write to shared memory node in Unreal and Shared memory reader node in Lightact.
Writing to Shared Memory
For writing to shared memory, insert a Write to shared memory node and connect it to Tick node. You can use it in any actor and it will work as long as the Handle Name property is the same as what you typed in Open shared memory node.
You should note the keys you used in the String Map as you’ll need to type them in Lightact as well.
Setting up a JSON Memory Reader in Lightact
The starting point in setting up Shared Memory Reader node is in the Devices window.
You right-click somewhere in the empty area, hover above JSON sharer and click on Add reader.
JSON memory reader node has 1 property:
- Handle name is the identifier which allows the receiving application to know which part of the memory is the starting point. This should match the Handle Name input on the Unreal’s side.
After you’ve entered it, check the Streaming checkbox. You might see a warning saying No shared memory file, which means that there is no shared memory handle with this name. This warning should disappear after you’ve set everything up correctly on Unreal’s side and Unreal is in Play mode (either Play-in-Editor, Standalone or Packaged).
Setup in Layer Layouts
After you’ve set up the JSON memory reader in the Devices window you can proceed to set up a Layer Layout.
Create a layer and in its Layout, insert a Shared memory listener node.
As with most listener nodes you have to select the Parent device. Select the memory reader node you created before. Then, click on Add pair button.
A Key string input and a Value string output will appear and most likely you’ll see a Key not found error. Once you type in the correct Key (it should match one of the keys used in the String Map on the Unreal’s side) the warning will disappear and the Value output will be the value with the Key in the String Map.