playMaker

Author Topic: Pool Manager 2 spawn  (Read 14028 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Pool Manager 2 spawn
« on: January 12, 2012, 03:19:40 AM »
Hi,

 After this post http://hutonggames.com/playmakerforum/index.php?topic=1004.0

 Please find a pool Manager 2 spawn custom action.

[EDIT] TESTED, so it works now.


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;


namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Pool Manager 2")]
[Tooltip("Use Spawn() instead of Unity's Instantiate(). The function signature is the same but the return type is different and Spawn() will use an instance from the pool if one is available.")]
public class PoolManagerSpawn : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.FsmString)]
[Tooltip("Pool name")]
public FsmString poolName;

[RequiredField]
[UIHint(UIHint.FsmGameObject)]
[Tooltip("GameObject or prefab to spawn")]
public FsmGameObject gameObject;

[UIHint(UIHint.FsmGameObject)]
[Tooltip("Use this to define the transform of the spawned GameObject when created")]
public FsmGameObject spawnTransform;

[UIHint(UIHint.FsmGameObject)]
[Tooltip("Store the returned the spawned GameObject.")]
public FsmGameObject spawnedGameObject;


[Tooltip("Event triggered if spawn was successful")]
public FsmEvent successEvent;

[Tooltip("Event triggered if spawn was not successful. It fails if the 'limit' option was used and the limit was reached.")]
public FsmEvent failEvent;

public override void Reset()
{
poolName = null;
gameObject = null;
spawnTransform = null;

spawnedGameObject = null;
successEvent = null;
failEvent = null;
}

public override void OnEnter()
{
DoSpawn();
}

void DoSpawn()
{
if (poolName.Value == "")
{
return;
}

if (gameObject.Value == null)
{
return;
}

Vector3 pos = Vector3.zero;
Quaternion quat =  Quaternion.identity;

GameObject go = spawnTransform.Value;

if (go !=null){
pos = go.transform.position;
quat = go.transform.rotation;
}

Transform result = PoolManager.Pools[poolName.Value].Spawn(gameObject.Value.transform,pos,quat);

if ( result != null){
spawnedGameObject.Value = result.gameObject;
Fsm.Event(successEvent);
}else{
Fsm.Event(failEvent);
}

}
}
}



 Bye,

 Jean
« Last Edit: January 18, 2012, 01:58:36 AM by jeanfabre »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Pool Manager 2 spawn
« Reply #1 on: January 18, 2012, 01:46:37 AM »
Ok,

 I edited the action. I had few glitches. Now it works and I tested it.

I'll add in the coming days more actions to create pools and manage them at runtime as well and basically port the entire api of pool manager 2.

WARNING: it's not working with the beta of playmaker. Pool Manager 2 and playmaker beta have a conflict. So make sure you use the official release of playmaker. It does work with unity beta tho ( so much beta at the moment.. :) )

 Bye,

 Jean

speedything

  • Playmaker Newbie
  • *
  • Posts: 13
Re: Pool Manager 2 spawn
« Reply #2 on: February 02, 2012, 05:37:29 PM »
Thanks for these Pool Manager actions Jean. I'd hacked something together myself for basic spawn/despawn but haven't gone any further and your code is much better and fully functioning anyway.

Really looking forward to seeing the rest of the actions.

justifun

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 280
Re: Pool Manager 2 spawn
« Reply #3 on: February 06, 2012, 11:13:20 AM »
Looking forward to testing this out, thanks jeanfabre!

Mark_T

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 72
Re: Pool Manager 2 spawn
« Reply #4 on: February 23, 2012, 07:06:35 PM »
Hi JeanFabre,

I tried to use the Pool Manager 2 spawn action but I can`t make it working.
The scene I tested is working with the SimpleSpawner.cs script provided by the Pool Manager 2 demo scene.
To the empty game object with the SimpleSpawner.cs script attached I attached a PSM with the PM2 Spawn action in the first state and a second state with the wait action (wait=1s). I deactivated the SimpleSpawner.cs script and I can see the instances in the pool, but no instance is spawned. If I activate the SimpleSpawner.cs, everything is fine. I created a string variable for the pool, a GameObject variable for the spawned object and an "ok" event to fire the transition to the wait action. I get no errors, but the "ok" event is not event fired and the second "Wait" state is not activated.

I`m using Unity 3.4.2, Playmaker 1.2.1.
Do you mind to take a look to the scene?

Thanks in advance for your time and help.






jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Pool Manager 2 spawn
« Reply #5 on: February 24, 2012, 12:48:54 AM »
Hi,

 send it to me, and I'll have a look yes.

Bye,

 Jean

justifun

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 280
Re: Pool Manager 2 spawn
« Reply #6 on: February 24, 2012, 03:41:29 PM »
I Thought that poolmanager 2 didnt work with the beta version of playmaker. or are you not using the latest beta.

Mark_T

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 72
Re: Pool Manager 2 spawn
« Reply #7 on: February 24, 2012, 04:56:14 PM »
For this one I`m using Unity 3.4.2, Playmaker 1.2.1.
I know that Pool Manager 2 doesn`t work yet with Playmaker 1.3

markinjapan

  • Full Member
  • ***
  • Posts: 103
Re: Pool Manager 2 spawn
« Reply #8 on: February 25, 2012, 04:50:49 PM »
Hi,

I've just been using this to spawn a lot of bullets. Problem is, I get to my limit and then I can't spawn any more. I use the Destroy action to destroy the bullets but after reading the Pool Manager docs I should be using a Despawn behaviour?

Just wondering if I'm doing it correctly or if there was a Pool Manager Despawn action on the way?

Thanks

Mark_T

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 72

markinjapan

  • Full Member
  • ***
  • Posts: 103
Re: Pool Manager 2 spawn
« Reply #10 on: February 25, 2012, 05:10:10 PM »
Excellent :)

Just thought I'd reply to my last mail.

I made a mistake, I was spawning a gameobject that was in the world and it was set to destroy itself after a few seconds, so there was nothing to spawn after the original got destroyed.

Human error :/

Anyway, thanks for that.

Ggblake

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Pool Manager 2 spawn
« Reply #11 on: November 03, 2012, 09:09:40 AM »
Code to spawn many objects randomly and code to depawn entire pool.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Pool Manager 2 spawn
« Reply #12 on: November 05, 2012, 01:18:19 AM »
Hi,

 One thing. Your despawnAll action is hardcoding the pool name to "Pickups", that's not really what you want, instead it should be a fsmString exposed in the action interface so that we can choose our own pools.

bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Pool Manager 2 spawn
« Reply #13 on: November 05, 2012, 04:39:57 AM »
Hi,

 Also, in your random spawn action, you should also provide flexibility in the area of randomness and not just hard code the size of the area. then it will be more flexible.

 bye,

 Jean

derkoi

  • Full Member
  • ***
  • Posts: 187
Re: Pool Manager 2 spawn
« Reply #14 on: December 14, 2012, 08:53:42 AM »
Could someone please edit this spawn action to spawn at a vector3 variable instead of a game object transform please? I tried myself but made a mess of it.  :'(

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;


namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Path-o-logical/Pool Manager 2")]
[Tooltip("Use Spawn() instead of Unity's Instantiate(). The function signature is the same but the return type is different and Spawn() will use an instance from the pool if one is available.")]
[HelpUrl("https://hutonggames.fogbugz.com/default.asp?W849")]
public class PmtSpawn : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.FsmString)]
[Tooltip("Pool name")]
public FsmString poolName;

[RequiredField]
[UIHint(UIHint.FsmGameObject)]
[Tooltip("GameObject or prefab to spawn")]
public FsmGameObject gameObject;

[UIHint(UIHint.FsmGameObject)]
[Tooltip("Use this to define the transform of the spawned GameObject when created")]
public FsmGameObject spawnTransform;

[UIHint(UIHint.FsmGameObject)]
[Tooltip("Store the returned the spawned GameObject.")]
public FsmGameObject spawnedGameObject;


[Tooltip("Event triggered if spawn was successful")]
public FsmEvent successEvent;

[Tooltip("Event triggered if spawn was not successful. It fails if the 'limit' option was used and the limit was reached.")]
public FsmEvent failEvent;

public override void Reset()
{
poolName = null;
gameObject = null;
spawnTransform = null;

spawnedGameObject = null;
successEvent = null;
failEvent = null;
}

public override void OnEnter()
{
DoSpawn();
}

void DoSpawn()
{
if (poolName.Value == "")
{
return;
}

if (gameObject.Value == null)
{
return;
}

Vector3 pos = Vector3.zero;
Quaternion quat =  Quaternion.identity;

GameObject go = spawnTransform.Value;

if (go !=null){
pos = go.transform.position;
quat = go.transform.rotation;
}

Transform result = PoolManager.Pools[poolName.Value].Spawn(gameObject.Value.transform,pos,quat);

if ( result != null){
spawnedGameObject.Value = result.gameObject;
Fsm.Event(successEvent);
}else{
Fsm.Event(failEvent);
}

}
}
}