
# **Heartbeat Runtime Manager 1.0**
Created by Aidan Wolf
The purpose of Heartbeat is to make runtime events quick & easy to set up as well as remain manageable throughout the duration of your project. This is a must-have asset for real-time games and complex Lenses built in Lens Studio. Here's an overview of the features:
* Universal pause & play
* Runtime event layers
* Taggable update loops
* Taggable delays
* Single update loop for entire project
* Avoids execution order issues on Prefabs
* Avoids errors when destroying Objects
In a nutshell, Heartbeat replaces `createEvent()` and makes it manageable. This is the most essential script in my entire LS game stack which is why I'm making it available to you.
## Instructions
1. Add the Heartbeat Prefab to the top of the Objects Panel hierarchy
2. Enter a new script and paste `global.Heartbeat.api.add(script)`
*Note: the Heartbeat Prefab makes `global.Heartbeat` accessible in all your scripts..*
## API
### Add
```javascript
global.Heartbeat.api.add(script,[optional] layer)
```
**.add() makes your script Heartbeat-enabled**
`layer` lets you define a runtime layer for the object to live in. For example, you could make the layer "enemy" enabling you to pause, play, and manage all your game's enemies. Other common examples might be "UI" or "Physics". Layers are useful because you might want your Pause menu to keep updating while conveniently pausing the rest of the game.
`script.api.addInit`, `script.api.addUpdate`, and `script.api.addDelay` are now available in your script to use.
### Create Init
```javascript
script.api.addInit(function)
```
Often in Lens Studio if you attempt to access script data or functions immediately after instantiating a Prefab you will get errors. Instead, wrap that code in addInit and it will execute on the next frame avoiding this common code execution order error.
### Create Update
```javascript
script.api.addUpdate(tag, function)
```
**.addUpdate() replaces createEvent("UpdateEvent")**
`tag` a string name to identify this update loop
`function` the code you want to call every frame
### Remove Update
```javascript
script.api.removeUpdate(tag)
```
**.removeUpdate() enables you to remove your update by its tag**
`tag` a string name to identify this update loop
Note: you can also just call `addUpdate(tag, ...)` to replace the current Update Loop under that tag
### Create Delay
```javascript
script.api.addDelay(tag, time, function)
```
**.addDelay() replaces createEvent("DelayedCallbackEvent")**
`tag` a string name to identify this delay event
`time` a float value in seconds to wait before call
`function` the code you want to call after the delay
### Remove Delay
```javascript
script.api.removeDelay(tag)
```
**.removeDelay() enables you to remove your delay by its tag**
`tag` a string name to identify this update loop
Note: you can also just call `addDelay(tag, ...)` to replace the current Delay under that tag
### Play
```javascript
global.Heartbeat.api.play([optional] layer)
```
**.play() resumes all Update Loops and Delays**
`layer` You can play an individual layer as defined in ``.add()``
### Pause
```javascript
global.Heartbeat.api.pause([optional] layer)
```
**.pause() pauses all Update Loops and Delays**
`layer` You can pause an individual layer as defined in ``.add()``
### Clear Events
```javascript
global.Heartbeat.api.clearAllEvents([optional] layer)
```
**.clearAllEvents() removes all Update Loops and Delays**
`layer` You can clear an individual layer as defined in ``.add()``
## Closing Notes
You should notice better performance in general as Heartbeat condenses everything down into a single Update Loop. The ability to replace update loops by adding another with the same tag name shouldn't be understated as this is a straightforward way to manage a specific behavior across multiple scripts - for example, if you assign all of a character's movement under "move" using the tag "move" ensures no update loop can override the other causing unexpected behavior. This has been essential for taking a module-based approach to my game's enemy and item design. Heartbeat may be overkill for some, but one of the most requested features in Lens Studio is the ability to pause and play so if you adopt the Heartbeat approach to your entire project you can do that. Would love to see better Runtime Management come to Lens Studio natively if its to mature and rival other game engines like Unity!