playMaker

Author Topic: Passing a class variable to playmaker and using it  (Read 1269 times)

Alatriste

  • Full Member
  • ***
  • Posts: 193
Passing a class variable to playmaker and using it
« on: January 21, 2019, 09:29:11 AM »
Hi, I'm in the process of learning to create my own custom actions. I did some progress but now I found a problem I don't know how to solve.

I'm doing a custom action to add an item using Inventory Engine. I managed to make it work to a basic level, but when I try to pass the "item" variable to playmaker somehow the "prefab" item gets lost during runtime.

It must be a way because I manage to pass the variable using the Call Method action.

This is my code:

Code: [Select]
  using UnityEngine;
    using MoreMountains.InventoryEngine;

    namespace HutongGames.PlayMaker.Actions
    {

    [ActionCategory("Inventory Engine")]
    [Tooltip("Add an item to the Inventory Engine plugin. The item must be a prefab created using the InventoryEngine menu.")]
    public class AddItem : FsmStateAction
    {

            // Define the gameObject owner of the Inventory script.
            [RequiredField]
            [Tooltip("The GameObject where the Inventory script is.")]
            public FsmOwnerDefault scriptOwner;

            [RequiredField]
            [Tooltip("Attach the prefab object to add as an item.")]
            public FsmObject Item;   
            private InventoryItem prefab;
           

            [RequiredField]
            [Tooltip("How many of the previous item must be added to the Inventory.")]
            public FsmInt numberOfItemsAdd;


            Inventory inventoryScript;


            // Code that runs on entering the state.
            public override void OnEnter()
    {


                Item = prefab;
               
                inventoryScript = scriptOwner.GameObject.Value.GetComponent<Inventory>();

                inventoryScript.AddItem(prefab, numberOfItemsAdd.Value);


    Finish();
    }


    }

    }

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Passing a class variable to playmaker and using it
« Reply #1 on: January 22, 2019, 03:17:19 AM »
Hi,

 is it not because you are referencing the prefab asset and not the prefab instance in the scene?

BYe,

 Jean

Alatriste

  • Full Member
  • ***
  • Posts: 193
Re: Passing a class variable to playmaker and using it
« Reply #2 on: January 22, 2019, 05:29:31 AM »
Most probably... Although I don't have a clue about how to change it.
I was looking for some script examples, but it seems to be different in this case, as the variable "prefab" belongs to the InventoryItem.

(I'm doing a C# course in Udemy but I'm already trying things... :P)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Passing a class variable to playmaker and using it
« Reply #3 on: January 23, 2019, 12:37:27 AM »
Hi,

You should debug using a FsmGameObject variable where you put that gameobject, ad then you log it in the Unity log using UnityEngine.Debug.Log("sdasd",thegameobjectreference), this way whe you click on the log it will ping it in the scene or in the assets and you'll know.

else you can also use https://docs.unity3d.com/ScriptReference/EditorUtility.IsPersistent.html


 Bye,

 Jean