Playmaker Forum

Playmaker Updates & Downloads => Share New Actions => Topic started by: jeanfabre on April 20, 2011, 12:11:36 AM

Title: SetFsmGameObject that accepts null
Post by: jeanfabre 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
Title: Re: SetFsmGameObject that accepts null
Post by: dreamerflyer19 on May 27, 2014, 10:00:39 AM
how to change the specify gameobject  named "Enemy" in this fsmstate  to other gameobject by code?
Title: Re: SetFsmGameObject that accepts null
Post by: Alex Chouls 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.
Title: Re: SetFsmGameObject that accepts null
Post by: dreamerflyer19 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?"));
Title: Re: SetFsmGameObject that accepts null
Post by: Alex Chouls 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?
Title: Re: SetFsmGameObject that accepts null
Post by: dreamerflyer19 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
Title: Re: SetFsmGameObject that accepts null
Post by: dreamerflyer19 on June 05, 2014, 12:37:42 AM
any help?
Title: Re: SetFsmGameObject that accepts null
Post by: jeanfabre 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