Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: Lobohotpants on June 04, 2012, 10:30:35 PM

Title: [SOLVED] Help with timer script
Post by: Lobohotpants on June 04, 2012, 10:30:35 PM
I was wondering if you guys could help me with creating an action or use existing actions that can access this timer script and create a controller instead of having to write the code.  It came with js and cs scripts but the tutorial was written in js.  I could copy the js but my project is using all cs and I have a feeling mixing the two is probably a bad idea.  That's where I ran into trouble.  I don't know cs syntax so everything I tried to import had parsing errors.

What I need is really simple.  I need to be able to access the timeString, turn the timer on and off and fire events every second.

This is all pretty new to me so any help is appreciated.
Title: Re: Help with timer script
Post by: jeanfabre on June 05, 2012, 03:18:07 AM
Hi,

 ok, I had a go at your Timer, and provided a working example hiliting several technics.

1: I created a proxy  "TimerProxy" so that you can receive the timer callback and fire a playmaker event as a result
2: I created a custom action "TimerGetFormattedTime" to call that function and get the result

So you'll find in the attached package a fully working system using this timer and how it can be integrated with playmaker.

WARNING: I modified the timer script because it doesn't work otherwise ( not related to playmaker, just the internal logic was wrong), I am not sure why this was so, Also the time information you get is not really about the count down, so more work here is needed to get this Timer system right.

Note that the timer could expose more variables publicly and it would void the need to create specific actions, and you would simply use the "get property" action to access string representations of the time, and various other features.

If you have more questions, don't hesitate, this is very important aspect of playmaker integration with existing framework and scripts.

Bye,

 Jean
Title: Re: Help with timer script
Post by: Lobohotpants on June 05, 2012, 11:52:41 AM
Hey thanks for taking a look at it.

I loaded up your example and saw what you did.  I didn't fully understand how it worked until this morning so maybe I can help you with the logic issues you were talking about.  It looks to me like the timer script stays in the project folder and you don't actually attach it to anything in the game manually.  The controller script attaches the timer script to the gameobject at runtime so that could be why it wasn't working? 

I'll attach the unitypackage so you can see how it all works though the controller example included is in js.

After I figured out how it works I tried setting the included controller script on a gameobject, setting up an FSM and trying to getProperty of the controller script.  I could access the public variables and even exposed some that were private but when I changed the values it didn't seem like it did anything.

Then I tried piecing together a cs controller using a js to cs converter.  Very handy!  This is what I came up with that seemed to work but again I couldn't access it with getProperty.
Code: [Select]
using UnityEngine;
using System.Collections;



public class Controller : MonoBehaviour {
public Timer timerObj;
public string timeString;
public float timeScaleFactor = 1.0f;

// Use this for initialization
void Start () {
// create timer and attach to this gameObject
timerObj = gameObject.AddComponent<Timer>();
timerObj.StartTimer();
}

// Update is called once per frame
void Update () {
// grab our time strings from the timer objects
timeString= timerObj.GetFormattedTime(2);
// dump it to the console
Debug.Log(timeString);
}
}
Title: Re: Help with timer script
Post by: jeanfabre on June 06, 2012, 02:47:17 AM
Hi,

 What exactly are you trying to access with getProperty? I am little confused.

 Bye,

 Jean
Title: Re: Help with timer script
Post by: Lobohotpants on June 06, 2012, 11:07:58 AM
Haha I'm also a little confused.  ??? Maybe I'm not understanding fully how to use getProperty but like I said in my original post I just need timeString, turn the timer on and off and fire events every second.

What I was trying to do in the previous example was change the timescale by using getProperty and it didn't seem like it was working.
Title: Re: Help with timer script
Post by: jeanfabre on June 06, 2012, 11:31:25 AM
Ok,

 for firing events every seconds, playmaker has it all figured, simply use the send event and set its delay to 1 sec, implement the event you fire in the same Fsm, and from that event come back to this state and you'll have your event fired every seconds. So as you start the timer, you also set up a fsm to fire event every sec and respond to the stop event as well, so that events are fired every sec only during the activation of the timer.

Also, could you explain what would be the use of sending an event every sec? Fsm that want to have this kind of timing, can totally set it up internally, and simply watch for a timer start and stop, and hanld themselves an action to perform every sec while the timer is active.

Then using what I gave you, should work really, cause it does allow you to start and stop that timer and access the timestring. so I am not sure what is not suitable in that package I sent you. you really don't need to use GetProperty at all.


 Bye,

 Jean
Title: Re: Help with timer script
Post by: Lobohotpants on June 08, 2012, 11:42:21 AM
You were right.  It wasn't getProperty that I needed, except for retrieving the timerString, but rather invokeMethod.  I used your example and created a controller script with some custom functions to start the timer, update and stop.  I prefer it this way b/c I didn't have to modify the timer.cs.

Regarding sending events every second.  I tried it your way where starting the timer fires an FSM event that delays 1 second and loops back but what I noticed is the even firing and the timer count get out of sync after about 20 seconds.  It's important that the events and the timer stay in sync for my game.
Title: Re: Help with timer script
Post by: jeanfabre on June 08, 2012, 01:50:54 PM
Hi,

 ok, I am glad you got it working.