playMaker

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

Thore

  • Sr. Member
  • ****
  • Posts: 480
Playmaker + Scriptable Objects = Win
« on: July 13, 2019, 12:14:08 PM »
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.

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.



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: June 02, 2020, 06:17:28 PM by Thore »

craigz

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

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

:D

cheers!
craigz

rechronicle

  • Full Member
  • ***
  • Posts: 119
  • Mystvaldia
    • Indie RPG Creator
Re: Playmaker + Scriptable Objects = Win
« Reply #2 on: February 07, 2020, 05:21:27 AM »
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

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Playmaker + Scriptable Objects = Win
« Reply #3 on: February 20, 2020, 08:26:59 AM »
Hi,

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

Bye,

 Jean

rechronicle

  • Full Member
  • ***
  • Posts: 119
  • Mystvaldia
    • Indie RPG Creator
Re: Playmaker + Scriptable Objects = Win
« Reply #4 on: February 20, 2020, 10:21:38 PM »
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!

PlaymakerNOOB

  • Full Member
  • ***
  • Posts: 219
Re: Playmaker + Scriptable Objects = Win
« Reply #5 on: February 28, 2020, 10:51:58 AM »
Creating scriptable actions seems like it will be insanely difficult to cover everything.  Although, totally looking forward to it!

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: Playmaker + Scriptable Objects = Win
« Reply #6 on: February 28, 2020, 11:41:27 PM »
Commenting just to keep a bookmark. Thanks for the post!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Playmaker + Scriptable Objects = Win
« Reply #7 on: March 02, 2020, 06:20:41 AM »
Hi,

 you actually have a bookmark system on this forum :) so you can simply add that thread to that.

Bye,

 Jean

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: Playmaker + Scriptable Objects = Win
« Reply #8 on: March 02, 2020, 11:23:30 PM »
Hi,

 you actually have a bookmark system on this forum :) so you can simply add that thread to that.

Bye,

 Jean
Thanks. I didn't notice that but will use it now.

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Playmaker + Scriptable Objects = Win
« Reply #9 on: March 03, 2020, 01:10:57 PM »
I must be dumb but I don't understand what this does. Please, can anybody explain it to me in simpler terms?

wetcircuit

  • Full Member
  • ***
  • Posts: 158
    • wetcircuit.com
Re: Playmaker + Scriptable Objects = Win
« Reply #10 on: March 03, 2020, 01:51:58 PM »
Based on the examples given…

You'd use scriptable objects, kind of like a Rolodex™, but connected to other scriptable objets and categories of objects…. Sort of an object oriented database.

I can see it being huge for sim management games, generative worlds…  the Brackey's video shows card-stats… And seems like it's easy to keep updated, like if you wanted to introduce a new category or change system-wide stats, or add content later with new rules….

I think I will try it with some character stats for my NPC…

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Playmaker + Scriptable Objects = Win
« Reply #11 on: March 03, 2020, 02:16:20 PM »
You have to think of scriptable objects in two parts: first, the “template”. This defines what variables you want to store but not “filled out”. Say, a ‘card’ is made up of a string suite and an int number. Now you defined what a card is.

The second part is making individual cards. With the template you create an asset and fill out suite = Spades, number = 11. Or suite = Hearts, number = 7.

Since these “cards” are actual assets, they can be shared and referenced across the project, that way, when you spawn a million cards, they can use the exact same asset containing the data.

Imagine the following scenarios.

Example 1
Your game has a million of zombies each with a few stats (like walking speed). When you instance the million zombies, then there are a million of identical stats stored in memory. Now with scriptable objects, you can make one literal asset that contains your stats, once, and all zombies use that exact same asset. This is like a global variable of sorts, but with collection of variables.

Example 2
Maybe you know this problem. You create several characters that use the exact same type of FSM, but in each the variable values are different. One has more health, the other can run faster etc. What if you could store the values in a sort of character sheet? So you have just one prefab, and then you could slot in a character sheet and then those values would make each instance individual.

I use this to safe variable settings during prototyping. The problem is, an FSM holds only the values you use at the moment. You cannot save them away and load them for prototype purpose. Once you change running speed, hit points etc around, your previous values are gone. Now with scriptable objects you can save those “settings” away and load them, create variants etc.
« Last Edit: March 03, 2020, 02:20:53 PM by Thore »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Playmaker + Scriptable Objects = Win
« Reply #12 on: March 06, 2020, 03:34:46 AM »
Hi,

 the more I think of this the more I believe it could be easier to simply create an fsm template, and then create empty prefabs with just that template and fill in the data in the fsm inspector.

This way, you don't require any coding and you can load them and use them just like scriptable objects instances. The benefit with this will be that the template can have some logic done in its fsm, again without any coding.

Bye,

 Jean

LordHorusNL

  • Beta Group
  • Full Member
  • *
  • Posts: 226
Re: Playmaker + Scriptable Objects = Win
« Reply #13 on: March 06, 2020, 06:03:55 PM »
Hi,

 the more I think of this the more I believe it could be easier to simply create an fsm template, and then create empty prefabs with just that template and fill in the data in the fsm inspector.

This way, you don't require any coding and you can load them and use them just like scriptable objects instances. The benefit with this will be that the template can have some logic done in its fsm, again without any coding.

Bye,

 Jean

One of the big drawbacks when using this method is that you can't store or change data on these FSM templates beyond the current session/scene like you can on scriptable objects.

So in theory it would be great for retrieving data, but useless when storing it.

And the option for storing data from session to session is one of the biggest benefits to using scriptable objects for me personally.

But damn you need a lot of custom actions when working with scriptables.


Edit: Never mind, turns out you can store persistent data on FSM templates, hot damn you learn something new with PlayMaker every day. :P
« Last Edit: March 06, 2020, 06:11:08 PM by LordHorusNL »

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Playmaker + Scriptable Objects = Win
« Reply #14 on: March 07, 2020, 02:25:15 PM »
You have to think of scriptable objects in two parts: first, the “template”. This defines what variables you want to store but not “filled out”. Say, a ‘card’ is made up of a string suite and an int number. Now you defined what a card is.

The second part is making individual cards. With the template you create an asset and fill out suite = Spades, number = 11. Or suite = Hearts, number = 7.

Since these “cards” are actual assets, they can be shared and referenced across the project, that way, when you spawn a million cards, they can use the exact same asset containing the data.

Imagine the following scenarios.

Example 1
Your game has a million of zombies each with a few stats (like walking speed). When you instance the million zombies, then there are a million of identical stats stored in memory. Now with scriptable objects, you can make one literal asset that contains your stats, once, and all zombies use that exact same asset. This is like a global variable of sorts, but with collection of variables.

Example 2
Maybe you know this problem. You create several characters that use the exact same type of FSM, but in each the variable values are different. One has more health, the other can run faster etc. What if you could store the values in a sort of character sheet? So you have just one prefab, and then you could slot in a character sheet and then those values would make each instance individual.

I use this to safe variable settings during prototyping. The problem is, an FSM holds only the values you use at the moment. You cannot save them away and load them for prototype purpose. Once you change running speed, hit points etc around, your previous values are gone. Now with scriptable objects you can save those “settings” away and load them, create variants etc.

Thank you for this detailed explanation. I used my last neurons trying to comprehend it so here's my take at what it means:

Example 1
That's like a typical Unity Prefab (with its own FSMs).

Example 2
Wouldn't each character, instantiated from a Prefab too, have a FSM or too that would also fetch those stats from the sheet?
You ascribe an ID to each character which you use to find the proper values for a given variable.