Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: tcmeric on February 20, 2017, 02:22:32 PM
-
Hi,
Is there any support for FsmUshort in custom actions? I am having some trouble converting a float to ushort and back :'(
Thanks.
-
Hi
Support for custom Fsmxxx is not available and not planned.
can you explain how you intend to use this ushort and where it comes from or where you need to set it?
Bye,
Jean
-
Hi,
Thank you for the reply. I am basically just setting up a custom action which sets playmaker FsmVariables to script variables. It is for the VRweapons package on the asset store that allows quick building of weapons for steam VR. The playmaker actions will be release for free (I am not the author of the asset but have their permission).
In the Weapons.cs script, it is written this way:
[Range(0, 0.2f)]
public float bulletSpreadRange;
public float kickStrength;
public float rotKickStrength;
public ushort hapticStrength;
etc etc.
My script is basically the template from MdotStrange, which works otherwise. I was hoping I could dump the ushort into a float. I have never done a variable type conversion. I have done some reading up on it, but havent figured it out yet.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("VRweapons")]
[Tooltip("Adjust weapon haptic time per shot in frames 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 WeaponHapticAction : 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("Adjust weapon haptic time per shot in frames.")]
// add the variables you want in your action
public FsmFloat HapticStre;
// 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;
HapticStre = 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)
{
MakeItSo();
Finish();
}
}
public override void OnUpdate()
{
if (everyFrame.Value)
{
MakeItSo();
}
}
//Name your method here
void MakeItSo()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}
//Playmaker variable to Script
theScript.hapticStrength = HapticStre.Value;
//Note! Playmaker var's need .Value after them or they won't work in some cases
}
}
}
Also can I ask, is there any way to get variables from serialized fields from a script into playmaker? I tried the same script above (changed for floats), but i get an error about them being read only.
Script.cs
[SerializeField]
float maxHealth = 100f;
[SerializeField]
float currentHealth;
-
Hi,
ok, ushort is an int, so in your action you should have an FsmInt for it.
ushort.MinValue = 0
ushort.MaxValue = 65535
so you can even create an error check in the action code to warn the user when he set that int value outside this range.
then when you want to set that ushort inside the script, simply cast it like so:
theScript.hapticStrength = (uShort)HapticStre.Value;
also, don't hesitate to full name variables, "HapticStrength" is better and more readable.
as for the readonly, I am not sure, if you can send me the package with the script and action, I'll have a look.
Bye,
Jean
-
Perfect, thank you for your help. I spent more time reading about implicit and explicit casting. Your solution worked and helped point me in the right direction to learn more!
I sent you a PM regarding the serialized field.
-
Hi,
odd, I did not received anything in my pm box.
Bye,
Jean
-
Opps, maybe I sent it to your email by mistake. I sent a PM this time (I think ;D )
-
Hi,
actually email is better, but haven't received one neither
Bye.
Jean