playMaker

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

rechronicle

  • Full Member
  • ***
  • Posts: 119
  • Mystvaldia
    • View Profile
    • Indie RPG Creator
Re: Playmaker + Scriptable Objects = Win
« Reply #15 on: March 07, 2020, 10:45:51 PM »
You ascribe an ID to each character which you use to find the proper values for a given variable.

How do you set up the connection between the data and their respective ID? Or is it just simply using 'Int Compare' and 'Int Switch'?

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 656
    • View Profile
the subject of this thread is the subject of this thread is
« Reply #16 on: March 08, 2020, 08:10:10 AM »
Something along those lines. I suppose an action that parses the sheet/array for a specific key (ID) would retrieve the entire line of stats. The advantage of sheets is that you could present them in a table of virtual limitless columns, which is not possible with Arrays, Array Lists or Hash Tables.

As to how define the IDs, either it has to be done manually for each character, something you cannot avoid, or you have so many computer generated characters that you create a random string of letters and numbers and attribute them to each new instance, with also storing them in string array every time which you'll check on to see if the string has already been created and attributed. That or you code your ID generator in such a way that you know it's never going to generate the same ID twice.

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
    • View Profile
Re: Playmaker + Scriptable Objects = Win
« Reply #17 on: March 10, 2020, 02:39:02 PM »
I can recommend a SO package such as Unity Atoms (which I use myself with positive results): https://adamramberg.github.io/unity-atoms/

The SO events and event listeners have really made a difference in making everything modular and been a nice bridge between scripts and Playmaker. Of course great at storing and sharing data between scripts/FSMs as well:
https://unity.com/how-to/architect-game-code-scriptable-objects
or:


I've made a few actions myself as I needed them, but they're for Unity Atoms v2 and v4 is just around the corner which has API breaking changes.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15495
  • Official Playmaker Support
    • View Profile
Re: Playmaker + Scriptable Objects = Win
« Reply #18 on: March 11, 2020, 02:34:16 AM »
Hi,

 Very interesting this atoms thingy!

Bye,

 Jean

Thore

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
Re: Playmaker + Scriptable Objects = Win
« Reply #19 on: June 02, 2020, 07:04:42 PM »
I am not quite ready to go all in with scriptable objects, but I found Matt Shell's cookbook playlist very useful and added some of these principles and script ideas to my project, and combined it with PlayMaker.

https://www.youtube.com/playlist?list=PLuldlT8dkudpCEkYQJb_H26BcbE7ozisX

GR00V3R

  • Playmaker Newbie
  • *
  • Posts: 12
    • View Profile
Re: Playmaker + Scriptable Objects = Win
« Reply #20 on: June 13, 2020, 11:32:29 AM »
Hi Thore, et al!   :)

Firstly, THANK YOU for working on this Action!    :D

So, I've got a GetScriptableObjects Action that works, having followed your quite detailed instructions (much appreciated); however, I'm struggling to make the scriptable object field an FSM Variable, and so can only set it manually in the FSM state.

Here's my GetScriptableObject code:

Code: [Select]
namespace HutongGames.PlayMaker.Actions
{

    [ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("This loads the CardAsset scriptable object and passes its data into Playmaker variables.")]
public class GetScriptableObjects : FsmStateAction
{

        public CardAsset scriptableObject;
        public FsmString title;
        public FsmString description;
        public FsmObject CardImage;
        public FsmObject CardTypeImage;
        public FsmInt moveRangeMin;
        public FsmInt moveRangeMax;
        public FsmInt attackDamage;
        public FsmInt attackRange;

        // All variables you use should be reset. This means that when the action runs another time, it will clear out old values first.
        // for some reason, the .Value is not used here.
        public override void Reset()
        {

            title = "";
            description = "";
            CardImage = null;
            CardTypeImage = null;
            moveRangeMin = null;
            moveRangeMax = null;
            attackDamage = null;
            attackRange = null;

        }

        // Code that runs on entering the state.
        public override void OnEnter()
{
            // You could put the code inside here, but it's cleaner to make a function call.
            GetScriptable();
            Finish();

}

        public void GetScriptable()
        {

            title.Value = scriptableObject.Title;    // note again, FSM variables need the .Value
            description.Value = scriptableObject.Description;
            CardImage.Value = scriptableObject.CardImage;
            CardTypeImage.Value = scriptableObject.CardTypeImage;
            moveRangeMin.Value = scriptableObject.MoveRangeMin;
            moveRangeMax.Value = scriptableObject.MoveRangeMax;
            attackDamage.Value = scriptableObject.AttackDamage;
            attackRange.Value = scriptableObject.AttackRange;

        }

    }

}

And here's the CardAsset scriptable object code:

Code: [Select]
public class CardAsset : ScriptableObject
{
    [Header("General Data")]
    public string Title;
    public CharacterAsset characterAsset;
    [TextArea(2,3)]
    public string Description;
    public Sprite CardImage;
    public Sprite CardTypeImage;

    [Header("Move Data")]
    public int MoveRangeMin;
    public int MoveRangeMax;

    [Header("Attack Data")]
    public int AttackDamage;  // if not 0, this is a Movement card
    public int AttackRange;

}

Any suggestions as to what I should be doing to allow scriptableObject to be an FSM Variable?

GR00V3R

  • Playmaker Newbie
  • *
  • Posts: 12
    • View Profile
Re: Playmaker + Scriptable Objects = Win
« Reply #21 on: June 13, 2020, 08:19:48 PM »
I tried to come at this a slightly different way with the following, but I still get the same fundamental issue, which is that Unity won't allow implicit conversion from FsmObject to CardAsset.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{

    [ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("This loads the CardAsset scriptable object and passes its data into Playmaker variables.")]
public class GetScriptableObjects : FsmStateAction
{

        // public CardAsset scriptableObject;
        public FsmObject tempScriptableObject;  // ADDED
        public FsmString title;
        public FsmString description;
        public FsmObject CardImage;
        public FsmObject CardTypeImage;
        public FsmInt moveRangeMin;
        public FsmInt moveRangeMax;
        public FsmInt attackDamage;
        public FsmInt attackRange;

        // All variables you use should be reset. This means that when the action runs another time, it will clear out old values first.
        // for some reason, the .Value is not used here.
        public override void Reset()
        {

            title = "";
            description = "";
            CardImage = null;
            CardTypeImage = null;
            moveRangeMin = null;
            moveRangeMax = null;
            attackDamage = null;
            attackRange = null;

        }

        // Code that runs on entering the state.
        public override void OnEnter()
{
            // You could put the code inside here, but it's cleaner to make a function call.
            GetScriptable();
            Finish();

}

        public void GetScriptable()
        {

            CardAsset scriptableObject = ScriptableObject.CreateInstance<CardAsset>(); // ADDED
            scriptableObject = tempScriptableObject; // ADDED
            title.Value = scriptableObject.Title;    // note again, FSM variables need the .Value
            description.Value = scriptableObject.Description;
            CardImage.Value = scriptableObject.CardImage;
            CardTypeImage.Value = scriptableObject.CardTypeImage;
            moveRangeMin.Value = scriptableObject.MoveRangeMin;
            moveRangeMax.Value = scriptableObject.MoveRangeMax;
            attackDamage.Value = scriptableObject.AttackDamage;
            attackRange.Value = scriptableObject.AttackRange;

        }

    }

}

Thore

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
Re: Playmaker + Scriptable Objects = Win
« Reply #22 on: June 14, 2020, 06:51:29 AM »
Hi,

The TDLR version: create a FSM variable of "object". No need to fill in anything. Here, I called it "fsmScriptableObject". This code allows you to cast your custom Scriptable Object into the FsmObject variable.

Code: [Select]
fsmScriptableObject.Value = (MyScriptableObject)scriptable;
Now that it is an FsmObject, and "inside PlayMaker", you can pass it over to some other action that uses and reads it. To access it most easily again, you need to do something like this:

Code: [Select]
scriptable = (MyScriptableObject)objectToGet.Value;
        passedAlongVariable.Value = scriptable.someInt;

... we're putting FsmObject variable into "scriptable", and then it can be used as usual to read whatever variable you want. Here, we're fetching an int.

I made two example actions that show how this works, plus the necessary scriptableObject to use in the example.

/// How To ///

(1) add the three files into a separate folder (to delete later).
(2) right click in that folder, myMenu > MyScriptableObject to make one.
(3) inspect that new object and just set some number there.
(4) in PlayMaker, set it up like shown in the attached image. The variable is just an "object", no need to configure it.
(5) gets Scriptable Object, and in a different action read the Int inside it.

Hope that helps.

GR00V3R

  • Playmaker Newbie
  • *
  • Posts: 12
    • View Profile
Re: Playmaker + Scriptable Objects = Win
« Reply #23 on: June 14, 2020, 07:56:24 AM »
You are awesome. Worked a treat! Thank you for the thorough response!  :)
« Last Edit: June 14, 2020, 08:07:29 AM by GR00V3R »

rpgmaker12

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: Playmaker + Scriptable Objects = Win
« Reply #24 on: July 11, 2020, 07:23:55 AM »
very cool, well done