PlayMaker Updates & Downloads > Share New Actions

Playmaker + Scriptable Objects = Win

(1/6) > >>

Thore:
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:


--- Quote ---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.
--- End quote ---

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 of this sheet and then fill each out to make actual characters.

By analogy, you create a scriptable object type holding whatever data you want. And then you use that type to create as many instances as you like. For example, holding stats of your RPG characters. Then you can easily make instances and fill out the values for warrior, rogue and wizard etc.



One of best things about them is that they are versatile: They can hold one individual variable, or 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, which is what the quote above says (i.e. instead that every enemy of one type is a copy, they actually all use the exact same data set. When you, say, increase the hit points, it changes for all at once).

You can also use them instead of enums! In this case, you can leave it completely empty and use it to have "pluggable" types that can be extended without the problems of enums. For this and more example cases, check out this playlist curated by Matt Shell.

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, effectively like a global variable type. And then use that 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. You can even generalize this and make an Int Holder Scriptable Object, and one instance represents player 1 HP, another holds player 2 HP and so on.

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, so it sets data into the asset (store data). Here’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 a game object, 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.

//www.youtube.com/watch?v=aPXvoWVabPY
//www.youtube.com/watch?v=OZ8NX6sqdbA
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.

craigz:
Thanks for making this! :D

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

:D

cheers!
craigz

rechronicle:
Hello,
Is it actually possible to put GetSetScriptable action in the Ecosystem / make it work with Ecosystem?
Like an editable action that you can write what you want to get/set from/to the scriptable object.

Thanks!

jeanfabre:
Hi,

Let me give it a proper go and see how I can put that on the ecosystem.

Bye,

 Jean

rechronicle:
Like when we can drag and drop gameObject/component into the state and 'Get Property / Set Property' pops up on the pointer.
Not sure how it works tho.

Thanks!

Navigation

[0] Message Index

[#] Next page

Go to full version