playMaker

Author Topic: Triggering a PM transition from a script?  (Read 4806 times)

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
Triggering a PM transition from a script?
« on: July 04, 2012, 06:58:38 AM »
Total newb here, so please be nice..

I am using a dialogue generation script for my game. I am triggering the script by playMaker enabling it as I collide with the chracter I want to speak. At the same time, Im using PM to disable the mouselook and controller components of my character, so I can click gui elements without the camera view swimming about while in the dialogue.

This works fine, but how can I turn the mouselook and controller components BACK ON, afte the dialogue has finished... This script (http://kimmonsdesign.com/node/21) has a script function so it can run a script at certain places in the dialogue and this would be great if I could send a variable to PlayMaker and PlayMaker would watch for this variable (or variables) and transition to states (including the enddialogue state, that turns this stuff back on) but could also .. oh I don't know run animations, or whatever).

How do I achieve this?

Im looking for a nece simple generic bit of code that I can put into this dialogue generator, that doesn't change (apart from the name of the variable perhaps)

Then I can continue to handle all my scripting in PlayMaker.

I was assuming something like

GameObject.Find("Mia").GetComponent(Play Maker FSM). something something something;).

where Mia is the character where PlayMaker and the dialogue script are attached... Am I on the right track and whats the something something something bit?

:)

Mark

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Triggering a PM transition from a script?
« Reply #1 on: July 05, 2012, 04:26:03 AM »
Hi,

 yes you are on the right track.

 create an event "HELLO" in your fsm and you will be able to call that like this:

myFsm is your reference to the fsm component:

Code: [Select]
myFsm.Event("HELLO");

Bye,

 Jean

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
Re: Triggering a PM transition from a script?
« Reply #2 on: July 05, 2012, 04:37:29 AM »
Thanks Jean, thats excellent!

Im only wondering one thing, and maybe I´ll have tried this out before you have a chance to respond anyway, but in your example,
Code: [Select]
myFsm.Event("HELLO");
Are you saying that this is ALL I need to access an FSM on a object? or do I still need this GameObject.Find stuff too.., and do I need

Assuming my game object is called "mia" that the dialogue generator AND PlayMakert FSM is attatched to, what do I actually write... I get Im wondering if your myFSM.EVent thing is found globally within my game or if I have to direct the script to it??

Again I apologize for the NEWB-NESS of this
 :)

Mark

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Triggering a PM transition from a script?
« Reply #3 on: July 05, 2012, 05:30:29 AM »
Hi,

 no worries! don't be shy on asking questions, even they are basics, we all have to start somewhere!

You will have to make sure you point to the right component, this is not a global thing. If you want to fire a global event, simply use PlayMakerFSM.BroadcastEvent("MY GLOBAL EVENT") and ANY fsm implement this event as a transition will be called.

So in your case: two ways to get a reference for a gameObject.

1: in your component script, create a public variable of type GameObject and drag and drop a gameObject onto the related inspector field. This is the way Unity works for 90% of the cases

2: you can of course use the Find system, but it is slow and most likely not required, if your gameObject exists in your scene hierarchy, you are better of using technic 1. If you create objects at runtime, you are also better off calling your script with a method and pass a reference of your newly created gameObject, it will be more efficient.

once you have a reference of the gameObject, you need a reference of the Fsm component

You can create a public variable of type PlaymakerFsm and drop any fsm component directly, that's very simple. butmake sure you drag the fsm component, not the gameObject, else it will pick the first fsm component on that gameObject, so if you have several fsm component, be sure to drag the right one.

Once you have a reference of that Fsm component, then yes, you simply need to call the Event() method.

bye,

 Jean

markfrancombe

  • Sr. Member
  • ****
  • Posts: 338
Re: Triggering a PM transition from a script?
« Reply #4 on: July 05, 2012, 07:56:41 AM »
Thanks again Jean, Im slowly getting closer to understanding this..

I suppose the first thing I should explain is that this dialogue generator tool gives various choices after each conversation element. It can simple continue, provide a choice (for a branching conversation) end the dialogue and also gives the option to add a script.

All of this is done in the inspector, with NO coding, Including adding a script, but typed into a place in the inspector. So adding a public variable and dragging in the components aint gonna work here.

So think I have to go with the Find system as you call it. (For now anyway, it could be that I dont go much further with this solution if its gonna be a bit clunky)

But first I tried the global event thing, I typed exactly what you wrote into the script area and changed the event name

Code: [Select]
PlayMakerFSM.BroadcastEvent("MY GLOBAL EVENT")
this did not work...

So  would you mind explaining how I should format this FIND system using my example???


Thanks

Mark
 :)

PS: someone on the other end of this problem, (posting on the site that provides this script was working with) tried this:

Code: [Select]
var end : PlayMakerFSM;
end = gameObject.Find("Mia").GetComponent.<PlayMakerFSM>();
end.Event("ConvoEnd");

(I inserted MY object names and stuff here) but this was not working either, but I think I see what he was trying to do...