playMaker

Author Topic: [solved] How to make custom actions  (Read 3523 times)

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
[solved] How to make custom actions
« on: March 26, 2011, 01:37:59 AM »
I would love to start being able to create my own custom actions, and I was wondering if you could explain it to someone who knows C# coding a bit but is far from proficient at it. I've look at the Actions and tried to figure out by myself but I was hoping you could provide a very basic example explaining how each part works. Here's an example bit of code I would like to make into an action:

Code: [Select]
GetComponent<MeshRenderer>().material.renderQueue +=1 // Higher the value the later it will be rendered. (C Mono only)
I use this in my 2D game to set which objects gets rendered in what order. It works great and I would love to create an action out of this. If I understand how it works, maybe I could start creating some iTween actions as well. In the action above I would like to be able to specify a gameobject (or use the owner of the script) and then set it layer order.

Another example I'd like to put in as an action, I could be wrong but don't think it's available, is this:

Code: [Select]
for (int i = 0; i < int_variable; i++)
{
Do Action "int_variable" times;
}

This would be cleaner than doing a float/int compare action. If you could explain a couple of examples of how to turn two pieces of code like this into actions, I could start producing actions of my own that I would share with the community.

And lastly, what about an iTween action so maybe I could start on some that I would like to use? Maybe this one to get me started:

Code: [Select]
iTween.ValueTo(GameObject target, Hashtable args)

// Example below

iTween.ValueTo(gameObject,{"from":score,"to":score+bonus,"time":.6,"onUpdate":"UpdateScoreDisplay"}) // complete Hashtable args available here: http://itween.pixelplacement.com/documentation.php#ValueTo


Teach a man how to fish! :) Thank you in case you have the time to help!

Tobbe

(Don't know why the text in the CODE tags appear so small...)
« Last Edit: April 08, 2011, 02:04:29 PM by alexchouls »

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
Re: How to make custom actions
« Reply #1 on: March 26, 2011, 02:14:23 AM »
I'm looking through the manual, I had missed the section that explained how to do custom actions. A little bit of help on the above examples would still be appreciated to get me started! :)

e.Ko

  • Administrator
  • Playmaker Newbie
  • *****
  • Posts: 13
    • View Profile
Re: How to make custom actions
« Reply #2 on: March 27, 2011, 08:36:43 AM »
For others finding this thread, here's the link to the API docs:
https://hutonggames.fogbugz.com/default.asp?W127

Here's an example of setting the renderQueue. Also look at the Material actions (e.g., Set Material Color):

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Material)]
public class IncrementRenderQueue : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(Renderer))]
public FsmOwnerDefault gameObject;

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

public override void OnEnter()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;

go.renderer.material.renderQueue += 1;
Finish();
}
}
}

Hmmm, have to fix the formatting for code blocks!

A few notes:

- FsmOwnerDefault shows the UI that lets the user pick the Owner or another object.
- Fsm.GetOwnerDefaultTarget gets the user's choice.
- Once you have the game object you can do whatever you'd normally do to a game object!
- Call Finish() when you're done to let the FSM know the action is finished. (When all actions on a state are finished a FINISHED event is sent).

You're second example is not so straightforward. Actions cannot be called independently, but are called by the state as described here: https://hutonggames.fogbugz.com/default.asp?W350

But you could make an action that sends an event (e.g., repeat) n times to repeat a state then continue. If I have a moment I'll write that action...

iTween actions are coming in 1.1...

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
Re: How to make custom actions
« Reply #3 on: March 27, 2011, 04:06:11 PM »
Thanks for the example! It makes complete sense, especially after having read through that section in the manual as well.

The second example wasn't so important as I've been able to do it with "compare" Actions and with a float variable plus a simple math Action to repeat a state X number of times. Was just thinking of making it easier. I don't know enough about what makes a game run slower but with my current game it's not a huge issue.

Very happy to hear that iTween actions are coming in 1.1. Is there an eta? Are we talking days, weeks, months?

Thanks for the response!

Tobbe