playMaker

Author Topic: Enable / Disable Control Scripts functions?  (Read 9923 times)

Col. Phil Bilko

  • Playmaker Newbie
  • *
  • Posts: 45
Enable / Disable Control Scripts functions?
« on: April 14, 2011, 06:37:45 AM »
Hello again;

I am trying to disable / enable an ability on my game object which has the FSM attached to it, the object is controlled by a script and in that script I want to enable and disable an ability. Basically what I would like to do is based on what TAG (Magnetic / NOT Magnetic) the object has, would determine if this ability is enabled or disabled. Collide with Magnetic = True / Collide with NOT Magnetic = False.

How would I go about setting that up?

Regards;

Bilko
« Last Edit: April 14, 2011, 06:53:57 AM by Col. Phil Bilko »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Enable / Disable Control Scripts functions?
« Reply #1 on: April 14, 2011, 07:21:30 AM »
Hi,

 Is that not simply a matter of maintaining a boolean value acting as a flag?

In your Fsm, you create a bool variable called MagneticEnabled

You then set it to true or false and when it collides, you check for that boolean and act accordingly like triggering an event or another/none.

the action to check for a boolean value is : https://hutonggames.fogbugz.com/default.asp?W76

 Do you have a problem in understanding how to do it in fsm, or simply how to do even if you could script it normally?

 Bye,

 Jean

Col. Phil Bilko

  • Playmaker Newbie
  • *
  • Posts: 45
Re: Enable / Disable Control Scripts functions?
« Reply #2 on: April 14, 2011, 07:30:25 AM »
Yes to all, I am a complete novice and know no code what so ever, as I am an artist not a coder, unfortunately I wish I knew that too hahahaha it would make life easier.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Enable / Disable Control Scripts functions?
« Reply #3 on: April 14, 2011, 07:34:38 AM »
Hi,

  Understood :)

  I'll shoot a screen cast hopefully within the next few hours to show you how you can achieve this.

 Bye,

 Jean

Col. Phil Bilko

  • Playmaker Newbie
  • *
  • Posts: 45
Re: Enable / Disable Control Scripts functions?
« Reply #4 on: April 14, 2011, 07:54:40 AM »
Oh that would be such a help, thanks ever so much. I look forward to viewing this.

Regards;

Bilko

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Enable / Disable Control Scripts functions?
« Reply #5 on: April 15, 2011, 05:40:38 PM »
Hi,

 Ok, screen cast for the first part done. I have to say at this point, I nearly gave up, I just couldn't record that screencast without making mistakes, too much caffeine in my blood at the moment  :P  If there is enough complain, I'll reshoot it :)

http://www.screencast.com/t/GyEFL5Tk8DHp

To explain a bit more on getting the pointer to the fsm component, I went for a slightly advanced wayin this screencast. There are other ways, I have made a small non exhaustive list of the most obvious ways:

 1: Declare a public variable:

Code: [Select]
public  PlayMakerFSM fsmInterface;
In the inspector, you then simply drag the gameObject with the fsm you want to talk to. Be careful it will pick the first fsm component attacched to that gameObject. So not ideal, but straight forward, not much coding involved. the nice thing as well is that the fsm can be on another gameobject, it's sometimes useful to have this kind of flexibility.

2: Access the first fsm component via code.

Code: [Select]
PlayMakerFSM fsmInterface = GetComponent<PlayMakerFSM>();
 Again, not ideal, because you can only access the first fsm component on that gameObject  like with technic 1.

3: A more versatile and flexible way ( used in the screencast and available in the code below )

 Scanning through all the fsm of that gameObject and pick the one matching the name you decide during authoring. The downside is that you need to be clean and name your fsm properly and take the time to reference it.


Hope you'll find it useful and clear enough. I will in a second screen cast use that script to enable disable the magnetic property within that fsm we created and use that to take the appropriate action when colliding with other objects. Then you'll have a complete example on how to achieve your goal using scripts and playmaker.

Bye,

Jean


Code: [Select]
using UnityEngine;
using System.Collections;
using HutongGames.PlayMaker;

public class TalkToFsm: MonoBehaviour {

// The name of the fsm you want to communicate with
public string fsmInterfaceName = "ScriptInterface";
// the related fsm pointer to fsmInterfaceName
private PlayMakerFSM fsmInterface;

// Use this for initialization
void Start () {

// run trough all the fsm attached to this gameObject and pick the one with the right name
PlayMakerFSM[] temp = GetComponents<PlayMakerFSM>();
foreach (PlayMakerFSM fsm in temp) {
if (fsm.FsmName == fsmInterfaceName){
fsmInterface = fsm;
break;
}
}

}


// Update is called once per frame
void Update () {

if (Input.anyKeyDown){

if (fsmInterface != null){

// send an arbitrary event
fsmInterface.Fsm.Event("MY SCRIPT EVENT");

// access and set an arbitrary value
fsmInterface.FsmVariables.GetFsmFloat("SomeRandomValue").Value = Random.value;


}

}
}
}
« Last Edit: April 16, 2011, 04:45:55 PM by jeanfabre »