playMaker

Author Topic: SetFsmGameObject that accepts null  (Read 2961 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14055
  • Official Playmaker Support
    • View Profile
SetFsmGameObject that accepts null
« on: April 20, 2011, 12:11:36 AM »
Hello Everyone,

 Just a very minor but quite important mod actually ( at least for my needs :) )

 Basically the original action requires you to select a gameObject to set, but in many cases, it is important to be able to remove the reference. so I modified the action to allow for this. I might miss an obvious drawback maybe.


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
//
// This version allow setting the variable to null,
// the original version requires a gameobject to be selected,
// but in many situation, it is important to be able to remove any reference.
//

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.StateMachine)]
[Tooltip("Set the value of a Game Object Variable in another FSM. Accept null reference")]
public class SetFsmGameObject : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[UIHint(UIHint.FsmName)]
[Tooltip("Optional name of FSM on Game Object")]
public FsmString fsmName;
[RequiredField]
[UIHint(UIHint.FsmGameObject)]
public FsmString variableName;
public FsmGameObject setValue;
public bool everyFrame;

GameObject goLastFrame;
PlayMakerFSM fsm;

public override void Reset()
{
gameObject = null;
fsmName = "";
setValue = null;
}

public override void OnEnter()
{
DoSetFsmGameObject();

if (!everyFrame)
Finish();
}

void DoSetFsmGameObject()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;

if (go != goLastFrame)
{
goLastFrame = go;

// only get the fsm component if go has changed

fsm = ActionHelpers.GetGameObjectFsm(go, fsmName.Value);
}

if (fsm == null) return;

FsmGameObject fsmGameObject = fsm.FsmVariables.GetFsmGameObject(variableName.Value);

if (fsmGameObject == null) return;

if (setValue == null) {
fsmGameObject.Value = null;
}else{
fsmGameObject.Value = setValue.Value;
}
}

public override void OnUpdate()
{
DoSetFsmGameObject();
}

}
}


Bye,

 Jean

dreamerflyer19

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
Re: SetFsmGameObject that accepts null
« Reply #1 on: May 27, 2014, 10:00:39 AM »
how to change the specify gameobject  named "Enemy" in this fsmstate  to other gameobject by code?

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3574
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: SetFsmGameObject that accepts null
« Reply #2 on: May 29, 2014, 10:27:26 AM »
The easiest way is to set it to a variable then set the value of that variable in your script:
https://hutonggames.fogbugz.com/default.asp?W329

In general you should try to interface with FSMs using events and variables.

dreamerflyer19

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
Re: SetFsmGameObject that accepts null
« Reply #3 on: May 29, 2014, 06:30:21 PM »
I have an question about  the function GetOwnerDefaultTarget();


currentFSM.FsmStates[0].Fsm.GetOwnerDefaultTarget("How to get currentFSM.FsmStates[0] 's FsmOwnerDefault?"));

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3574
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: SetFsmGameObject that accepts null
« Reply #4 on: May 29, 2014, 07:32:53 PM »
GetOwnerDefaultTarget is a helper function that returns either the GameObject that owns the FSM or a specified target, based in the user selection. What are you trying to do with this function?

dreamerflyer19

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
Re: SetFsmGameObject that accepts null
« Reply #5 on: May 31, 2014, 08:07:44 AM »
question
1: "How to get currentFSM.FsmStates[0] 's FsmOwnerDefault?",
2:how to re agsin new gameobject in  FsmOwnerDefault by code?

I want to chang user selected the GameObject that owns the FSM or a specified target by code,then give this FSM runing on the new gameobject

dreamerflyer19

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
Re: SetFsmGameObject that accepts null
« Reply #6 on: June 05, 2014, 12:37:42 AM »
any help?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14055
  • Official Playmaker Support
    • View Profile
Re: SetFsmGameObject that accepts null
« Reply #7 on: June 09, 2014, 01:24:55 AM »
Hi,

 the default gameobject owner is a fsm wide value, it's simply the "owner" property of the fsm, you don't need to look into states to get this info.

Bye,

 Jean