playMaker

Author Topic: Custom Action Assistance - Audio Clip[SOLVED]  (Read 2604 times)

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Custom Action Assistance - Audio Clip[SOLVED]
« on: February 20, 2017, 05:40:17 AM »
I am trying to set an audio clip with a custom action. Seems like I am not having much luck.

I am getting the following error: Assets/VRWeapon_Playmaker/WeaponShotAudioAction.cs(72,35): error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.AudioClip'. An explicit conversion exists (are you missing a cast?)

The orginal script sets the variable as AudioClip
public AudioClip shotSound;

In playmaker, I set my Var as

[ObjectType(typeof(AudioClip))]
public FsmObject gunSound;


But that doesnt seem to work.


------

My Full Custom Action

Quote
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
   [ActionCategory("VRweapons")]
   [Tooltip("Adjust the shot sound for VRweapons script on an object")]

   // the class must match the name of the action
   // if the action is named missleAction then that should be the name of the class
   public class WeaponShotAudioAction : FsmStateAction
   {
      [RequiredField]
      // add the name of your script inside of typeof("yourScriptName"))
      [CheckForComponent(typeof(Weapon))]   

      // this is the game object the script is on
      public FsmOwnerDefault gameObject;

      [Tooltip("Set audio shot sound.")]
      // add the variables you want in your action
      [ObjectType(typeof(AudioClip))]
      public FsmObject gunSound;

      // you can usually leave this alone
      public FsmBool everyFrame;

      // you are making a custom variable with the scripts type
      Weapon theScript;

      public override void Reset()
      {
         //its good practice to set your var to null at start
         gameObject = null;
         gunSound = null;
         everyFrame = false;
      }

      public override void OnEnter()
      {
         var go = Fsm.GetOwnerDefaultTarget(gameObject);

         // you are grabbing the script from the game object and storing it in your custom variable type
         theScript = go.GetComponent<Weapon>();

         if (!everyFrame.Value)
         {
            DoTheMagic();
            Finish();
         }

      }

      public override void OnUpdate()
      {
         if (everyFrame.Value)
         {
            DoTheMagic();
         }
      }

      //Name your method here
      void DoTheMagic()
      {
         var go = Fsm.GetOwnerDefaultTarget(gameObject);
         if (go == null)
         {
            return;
         }

         //Playmaker variable to Script

         theScript.shotSound = gunSound.Value;

         //Note! Playmaker var's need .Value after them or they won't work in some cases


      }

   }
}
« Last Edit: February 21, 2017, 04:03:15 AM by jeanfabre »

mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • Can't code? Who cares! Make games anyway!
Re: Custom Action Assistance - Audio Clip
« Reply #1 on: February 20, 2017, 07:04:57 AM »
Add this to that line in the code-

Code: [Select]
theScript.shotSound = gunSound.Value as AudioClip;
As you can see I added "as AudioClip" when using Playmaker object variable types you have to tell the script what the variable type is.
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: Custom Action Assistance - Audio Clip
« Reply #2 on: February 20, 2017, 10:27:01 AM »
Thank you!

While I have you here, do you have any examples or know how to get a Enum into a FsmEnum in a custom action? I have looked around to find some examples on the net, but havent seemed to be able to find one.

mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • Can't code? Who cares! Make games anyway!
Re: Custom Action Assistance - Audio Clip
« Reply #3 on: February 20, 2017, 10:26:48 PM »
No problem-

I use enums a lot in custom actions for UI purposes to create a popup menu selection but I don't think I've ever used them to set Fsm Enums-

Here is my code snippet I keep to remind myself how to use them
Code: [Select]
public enum theTypes
        {
            Unguided,
            Guided,
            Predictive
        }

// create a var for the enum

        public theTypes theTypeEnum;
 
 
 //this gets the int index of the enum "MyEnum"
 int eValue = (int)theTypeEnum;

So you can use normal enum's like that in your custom actions- then you want to use one of those enums to set an Fsm Enum?

If so, you would take the int value of the regular enum and use it to set the Fsm Enum type-

If you can't figure it out let me know and I'll see if I can make some example code-

Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: Custom Action Assistance - Audio Clip
« Reply #4 on: February 21, 2017, 01:53:01 AM »
Thanks, I appreciate the assistance. I think I am going to go back and re-watch some parts of a C# course I am taking first. I am hoping that will help to clarify some things for me. I am fairly new to C#.

Ill post back here later once I have done so.