Author Topic: Playmaker + Scriptable Objects = Win  (Read 910 times)


  • Sr. Member
  • ****
  • Posts: 353
    • View Profile
Playmaker + Scriptable Objects = Win
« on: July 13, 2019, 09:14:08 AM »
Scriptable Objects are amazing and everybody should use them. I went ahead and made two example scripts with copious amounts of comments that show how it works and how you can make your own, even without coding experience.

From Unity's documentation:

A ScriptableObject is a data container that you can use to save large amounts of data, independent of class instances. One of the main use cases for ScriptableObjects is to reduce your Project’s memory usage by avoiding copies of values. This is useful if your Project has a Prefab that stores unchanging data in attached MonoBehaviour scripts. Everytime you instantiate that Prefab, it will get its own copy of that data. Instead of using the method, and storing duplicated data, you can use a ScriptableObject to store the data and then access it by reference from all of the Prefabs. This means that there is one copy of the data in memory.

General Concept
Scriptable Objects are a collection of variables stored into an asset. Suppose you play Dungeons & Dragons. The book contained a character sheet printed somewhere. It defines the variables that make up one particular character. But you don't actually use that character sheet itself. You would make copies and then fill each out to make actual characters.

One of best things about them is that they are versatile: They can hold one individual variable, or could be complete data sets. You can keep any collection of variables in them, settings, stats, recipes, item lists, etc. It's even useful for a single object, because everything else can reference it and read/write into it.

Even more amazingly, they can be nested. You can make one Scriptable Object where you define a costume, perhaps keeping some prefabs that belong to the costume. And then you can plug that "costume object" into another scriptable object for characters. Or you can make ingredients made up from ingredients, and endlessly combine them.

It's very potent also to keep persistent data across scenes. You could create a type of scriptable object called "HP" which only contains a single int value. Then, you make two instances of it, one for player 1 and one for player 2. And then use them to permanently keep track of the HP across scenes. Also, in many games, you can simply have enemies write into the HP asset directly, when they deal damage, without complicated FSM setups. 

How It Works
Copy both scripts somewhere into your asset folder.

ScriptableTemplate.cs is the example script to generate actual scriptable objects of one particular type. For every type you want, you make a new script. It's akin to the character sheet printed in the D&D book, but which you do not touch at all, but make assets from to fill out.

GetSetScriptableObjects.cs is the example action for Playmaker that at first simply reads out the variables from a scriptable object asset you plugged in. You can open the script, and change it to use to set data into the asset (store data). It's an example to show how to do this.

  • STEP 1: In your project folder, right-click. In this case, it's clunky MyScriptables > ScriptableObjectExample, but you're supposed to change it later.
  • STEP 2: Select the newly created asset and fill in some values.
  • STEP 3: Make a new scene, add an FSM to the camera, and add the action GetSetScriptableObject that came with this script.
  • STEP 4: you need to plug in the scriptable object asset you've made in step 1. And hit run.

You'll notice that some values are copied over. They are not entirely "correct" on purpose, just look into the script and read my comments to see why (I use this to illustrate something).

The Playmaker script by default GETs the values from the Scriptable Object you plugged in. Open up that action script, and you also see below a function to SET the values. This is saving them to the asset.

With this equipped and tons of comments in the two scripts, you should be able to create your own types. I recommend creating different actions for getting and setting. My example is simply to illustrate how it works.

Bonus: To easily tell one scriptable object type from another, you can also give them icons. To make your own, just make a PNG, and name it "<ScriptName> Icon.png". Then, copy this into Assets/Gizmos, and change the Texture Type to Editor GUI and Legacy GUI. You can download my example and try it out.
« Last Edit: July 16, 2019, 02:50:36 AM by Thore »


  • Beta Group
  • Full Member
  • *
  • Posts: 200
    • View Profile
    • Haven Made
Re: Playmaker + Scriptable Objects = Win
« Reply #1 on: October 16, 2019, 08:55:14 PM »
Thanks for making this! :D

They're SO cool! Hard to believe there's no comments on here 🤦‍♂️