playMaker

Author Topic: Best way to initialize a state  (Read 2892 times)

rc183

  • Playmaker Newbie
  • *
  • Posts: 3
Best way to initialize a state
« on: August 22, 2013, 01:54:29 PM »
Hi,

I try to convert my game for playmaker and i find the best way to initialize a state with a complex behaviour.

I have used another FSM System before playmaker and this is my init code for my shop :

Code: [Select]
        public override void InitState()
{
base.InitState();

// Init shop elements
shopElements = new Dictionary<tk2dUIItem, ShopElement>();

// Retrieve buttons
middleCenterAnchor = GameObject.Find("MiddleCenterAnchor");
shopObject       = GameObject.Find("Shop");
closeButton       = shopObject.transform.Find ("ShopCloseButton").GetComponent<tk2dUIItem>();
scrollableArea    = shopObject.GetComponentInChildren<tk2dUIScrollableArea>();
scrollContent    = scrollableArea.transform.Find("Content");

// Store initial position
initialPosition    = shopObject.transform.localPosition;

// Load Buildings
int i = 0;
foreach (string prefabName in DungeonParkGame.Data.BuildingPrefabNames)
{
// Retrieve each positions
GameObject buildingObject = ((GameObject)GameObject.Instantiate(Resources.Load(DungeonParkGame.Config.BuildingsPrefabDirectory + prefabName)));
tk2dUIItem buildingButton = CreateBuildingButton(prefabName, i, buildingObject);

// Register event
buildingButton.OnClickUIItem += OnBuildingClick;

// Register the index associated to the item
shopElements.Add(buildingButton, new ShopElement() { prefab = prefabName });

// Update i
i++;
}

// Update the scroll bar area
scrollableArea.ContentLength = i * 300;
}

        private tk2dUIItem CreateBuildingButton(string name, int buttonIndex, GameObject buildingObject)
{
// Instanciate a radio button
tk2dUIItem buildingButtonInstance = ((GameObject)GameObject.Instantiate(Resources.Load(DungeonParkGame.Config.ShopPrefabDirectory + "BuildingButton"))).GetComponent<tk2dUIItem>();
buildingButtonInstance.name = "BuildingButton" + buttonIndex;

// Attach the building object to the building button
buildingObject.name = name;
buildingObject.transform.parent = buildingButtonInstance.transform;
buildingObject.transform.localPosition = new Vector3(-96, -16, -1);

// Deactivate some rotation
for (int i = 90; i <= 270; i += 90)
{
buildingObject.transform.Find(i.ToString()).gameObject.SetActiveRecursively(false);
}

// Attach the building to the scroll content
buildingButtonInstance.transform.parent = scrollContent.transform;
buildingButtonInstance.transform.localPosition = new Vector3(300 * (buttonIndex + 1), -160, -1);

return buildingButtonInstance;
}

I use 2dtoolkit and special stuff to load my items in the shop. My question is : is it better to try to reproduce this code in the playmaker editor (i don't know if it's really possible ?) or is it better to keep my script and add this script with the AddScript actions or create a specific action for this initialization or another way ?

Thank you in advance

rc183

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Best way to initialize a state
« Reply #1 on: August 23, 2013, 02:02:56 PM »
I'm new with PlayMaker. Do you have any tips for my situation ?

Thank you

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4002
  • Official Playmaker Support
    • LinkedIn
Re: Best way to initialize a state
« Reply #2 on: August 23, 2013, 02:20:53 PM »
Add Script will add a behaviour to the object at runtime, which is very rarely what you need.

If you have a script that already does what you need, you can add it to the GameObject (at edit time) and then access it from PlayMaker using SendMesssage (calls a method in the script), or Get/Set Property to get/set variables in the script.

2DToolkit also ships with Playmaker actions. Unfortunately I'm not super familiar with 2DToolkit, but I know others here use it with Playmaker...

In general with Playmaker you should try to break complex behaviours down to their simplest parts. These are states. You can think of them as the blocks in a flow chart, the simplest set of steps needed to implement a behavior. Each state can execute actions, the atomic building blocks that you can combine to create interesting behavior. Once you get the hang of this structure you can build quite complex behaviors with it...

rc183

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Best way to initialize a state
« Reply #3 on: August 23, 2013, 02:45:23 PM »
Ok. Thank you for your answer.

I think my best way is to create a setup state and create new actions to properly initialize the complex states and use the default actions available in playmaker for the basic states.

For 2dtoolkit, there is no problem, i have developed new actions to use the new 2dtoolkit UI system.

Thanks !

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Best way to initialize a state
« Reply #4 on: September 10, 2013, 02:06:48 AM »
Hi,

 Don't hesitate to share your custom action for 2dtk if you want.

Bye,

 Jean