playMaker

Author Topic: SmartPool released with custom actions  (Read 22162 times)

Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
SmartPool released with custom actions
« on: January 01, 2013, 10:22:17 AM »
Hi,

our pooling solution SmartPool is live now at the AssetStore. It's custom actions are available at our website.

Jake

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: SmartPool released with custom actions
« Reply #1 on: January 02, 2013, 06:16:06 AM »
Hi Jake,

 added the link to the add ons on the wiki:

https://hutonggames.fogbugz.com/default.asp?W714

bye,

 Jean

gamedivision

  • Full Member
  • ***
  • Posts: 227
Re: SmartPool released with custom actions
« Reply #2 on: January 17, 2013, 03:05:21 PM »
ok i have a problem with my spawned object ive stored in a globalgameobject variable.the cube itself has added force attached to it,now the initial cube reacts to the force once spawned but all subsequent cubes just fall with no force at all
the easiest way to explain would be, the re-used cube doesn't react to the force added to the prefab after the initial spawn.

any ideas
 :)

Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
Re: SmartPool released with custom actions
« Reply #3 on: January 17, 2013, 04:31:08 PM »
Hi,

hmm, hard to guess what's going on. Is it possible that you send me a package that demonstrates the problem to jake<at>fluffyunderware<dot>com ? I'll look into it asap...

Thanks
Jake


gamedivision

  • Full Member
  • ***
  • Posts: 227
Re: SmartPool released with custom actions
« Reply #4 on: January 17, 2013, 07:23:08 PM »
Hi jake thanks for the speedy reply,I'm gona clean up the scene and when I finish work tomorrow ill post you it,I don't think it's anything to do with smartpool,I think spawning a particle explosion would do the same thing once the animation has gone through its cycle even though your repspawning the same object it wouldn't reset the animation

julius

  • Playmaker Newbie
  • *
  • Posts: 15
Re: SmartPool released with custom actions
« Reply #5 on: February 01, 2013, 10:08:52 PM »
Hey, in the SmartPool script on the asset store it has a #if UNTIY_4_0  instead of #if UNITY_4_0. Gave me a bunch of errors.

Also I wanted the create object functionality for setting a Spawn Point/Position/Rotation the network stuff is a little beyond me at the moment, but I added the rest. I haven't fully tested this script but the mini tests I've done work.

Code: [Select]

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("SmartPool")]
    [Tooltip("Spawn an object managed by a SmartPool")]
    public class SmartPoolSpawn : FsmStateAction
    {
        [Tooltip("The SmartPool being adressed")]
        [RequiredField]
        public FsmString smartPool;

        [UIHint(UIHint.Variable)]
        [Tooltip("Optionally store the created object.")]
        public FsmGameObject storeObject;

[Tooltip("Optional Spawn Point.")]
public FsmGameObject spawnPoint;

[Tooltip("Position. If a Spawn Point is defined, this is used as a local offset from the Spawn Point position.")]
public FsmVector3 position;

[Tooltip("Rotation. NOTE: Overrides the rotation of the Spawn Point.")]
public FsmVector3 rotation;

public override void Reset()
{
smartPool = null;
storeObject = null;
spawnPoint = null;
position = new FsmVector3 { UseVariable = true };
rotation = new FsmVector3 { UseVariable = true };
}


        public override void OnEnter()
        {
            if (!smartPool.IsNone)
{
                var go = SmartPool.Spawn(smartPool.Value);
storeObject.Value= go;

var spawnPosition = Vector3.zero;
var spawnRotation = Vector3.up;

if (spawnPoint.Value != null)
{
spawnPosition = spawnPoint.Value.transform.position;

if (!position.IsNone)
{
spawnPosition += position.Value;
}

spawnRotation = !rotation.IsNone ? rotation.Value : spawnPoint.Value.transform.eulerAngles;
go.transform.position = spawnPosition;
go.transform.localEulerAngles = spawnRotation;
}
else
{
if (!position.IsNone)
{
spawnPosition = position.Value;
go.transform.position = spawnPosition;
}

if (!rotation.IsNone)
{
spawnRotation = rotation.Value;
go.transform.localEulerAngles = spawnRotation;
}
}
}


Finish();
}
    }
}


Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
Re: SmartPool released with custom actions
« Reply #6 on: February 03, 2013, 01:57:17 AM »
Hi Julius,

thanks for pointing this out, I fixed and resubmitted it!

Regarding the Spawn-Action: the Spawn-Action returns the spawned object, so why don't you just initialize it in the State you're spawning it? Alternatively, OnEnable() will be called for the spawned object, so you can add any intitialization-code or calls to pM there.

Jake
« Last Edit: February 03, 2013, 03:13:47 AM by Jake »

julius

  • Playmaker Newbie
  • *
  • Posts: 15
Re: SmartPool released with custom actions
« Reply #7 on: February 07, 2013, 06:21:35 PM »
Hey Jake,

I'm hoping to be able to do the functionality via playmaker. I'm still pretty new to playmaker and unity so let me know if I'm completely off.

By initialize do you mean transforming the the deactivated smartpool object into place before running the SmartPool.Spawn()? or is there a way to use Instantiate without making a clone (which is still a costly behavior correct). If the former then I still couldn't use a playmaker action as I don't have access to the array in smart pool to find out which gameobject is the next array.pop. I wouldn't mind making a simple lookup function in smart pool to find the next gameobject to spawn if that is the case! I just want to make sure I'm not misunderstanding something as it is quite possible.

OnEnable() sets off the START state in a playmaker action so I could add the position rotation ect on the prefab via a playmaker FSM and it would effectively set those variable up, but I would still have to do that on the prefab and couldn't do it via the action above.

Anther question I had about SmartPool is how it handles child transformation changes. Looking though the code I saw the that it resets the main GameObject position to zero, but does it do anything else to reset the GameObject to its prefab state?

« Last Edit: February 07, 2013, 06:36:06 PM by julius »

Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
Re: SmartPool released with custom actions
« Reply #8 on: February 08, 2013, 05:52:44 AM »
H julius,

I'm not quite sure if I understand you right, but let me clarify two things:

1.) Smartpool "prepares" the pool at start (=instantiating n clones of the prefab until MinPoolSize is reached and disable the new GameObjects instantly, so no FSM/scripts will run). When you call Spawn(), one Object from the pool will be enabled (FSM's of this Object will start running as well as any other scripts attached!) and a reference returned to you. Besides setting localPosition to zero no other initialization happens.

2.) When you need to run logic for an object after spawning it, you have two options (related to playMaker): a.) in the FSM where you call the "SmartPool Spawn" action, store the returned object in a variable and do something with it afterwards (that's what I do in the example scene coming with pmSmartPool) or b.) Add an FSM to your prefab. After Spawn, the FSM of that object will start running and you can do some stuff there.

To sum this up, there's no need to know which object will be spawned next by SmartPool, you really shouldn't care about it.

Hope that helps
Jake


brendang

  • Junior Playmaker
  • **
  • Posts: 63
    • Out to Play Interactive
Re: SmartPool released with custom actions
« Reply #9 on: February 16, 2013, 06:02:50 PM »
Argh,  Pooling is still voodoo to me, and I own SmartPool!

I'd like to describe to uses I imagine using the plugin for and maybe Jake or someone with pooling chops could tell me if these uses are what the pooling plugin would be used for.

1 - I have a 2D sidescroller. It's a bird flying over water. As the bird flies along, I'd like a random number (between 1-5) of random fish (there will be 12 different fish models/prefabs)  to be jumping out of the water for the bird to eat, within the Camera's view (the camera travels along with the bird). 

2- Imagine a room with several doors. Randomly, a random selection of 'monsters' (basically 12-20 2d sprites), would come crashing out of one or more of the doors toward camera for you to shoot.

I guess I'm wondering if 'pools' should be composed of collections of different fish or monsters or cubes or whatever.  Can they be given random forces ( like the fish jump with different combinations of rigidbody.AddForce on X & Y for different heights and such).

Hopefully this makes some sense.

B.

Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
Re: SmartPool released with custom actions
« Reply #10 on: February 17, 2013, 03:43:40 AM »
Hi,

keep in mind that a pooling manager does nothing more than serve objects. Without a pooling manager, you keep references to your prefabs somewhere and instantiate and destroy them as needed. With a pooling manager you do exactly the same, but instead of instantiating and destroying you use Spawn() and Despawn(). With SmartPool, you don't neccessarely need to keep references to the prefabs, because you can optionally get them by their name.

So for each prefab, add a SmartPool object, assign the prefab, set properties and replace the instantiate and destroy commands with calls to SmartPool. In your first example, create 12 Smartpool components, assign the 12 Prefabs and name the pools by number, e.g. Fish0, Fish1, Fish2 etc...

In your code, when you need a random fish, do "myFish=SmartPool.Spawn("Fish"+Mathf.Random(0i,11i));" to get a random fish. After receiving the fish, initialize it like you would without SmartPool and you're done. Wen you don't need the fish anymore, use "SmartPool.Despawn(myFish)" to return it to the pool.

Jake


brendang

  • Junior Playmaker
  • **
  • Posts: 63
    • Out to Play Interactive
Re: SmartPool released with custom actions
« Reply #11 on: February 17, 2013, 07:53:57 PM »
In your code, when you need a random fish, do "myFish=SmartPool.Spawn("Fish"+Mathf.Random(0i,11i));" to get a random fish. After receiving the fish, initialize it like you would without SmartPool and you're done. Wen you don't need the fish anymore, use "SmartPool.Despawn(myFish)" to return it to the pool.

Jake

What code?  I thought the purpose of integrating SmartPool with PM was using actions?   I'm sure this makes complete sense to you, which is handy, since you wrote it. but it's a wee bit vague to me.   I think the issue is you are not discussing this in "Playmaker terms" your instructions. Have you worked with the Playmaker actions much as they relate to SmartPool?

Also, the pm_example project comes in broken?   I start with a new project, import Playmaker, Import SmartPool and then import the Playmaker actions for SmartPool and this is how it shows up in the Inspector. And nothing seems to have an FSM attached.



Clearly I'd hook the bullet into the bullet, and the Brick into the brick. but which script goes into the PlayMakerGUI and which into the Mono Behavior beneath the two SmartPool scripts attached to the SmartPools Gameobject?
« Last Edit: February 17, 2013, 08:20:57 PM by brendang »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: SmartPool released with custom actions
« Reply #12 on: February 17, 2013, 09:00:39 PM »
Regarding the Missing (Mono Script). Sometimes Unity loses references to scripts. You can restore the reference by selecting the browse button and selecting the appropriate component: PlayMakerFSM for the FSMs and PlayMakerGUI for the GUI component.

But it looks like the example project is missing other references too... so there might be other problems with the example project...

Jake

  • Junior Playmaker
  • **
  • Posts: 61
    • Fluffy Underware
Re: SmartPool released with custom actions
« Reply #13 on: February 18, 2013, 03:22:36 AM »
Hi,

I've updated pmSmartPool (download here), the examples should work now. I exported the package with Unity 3.5 and playMaker 1.4.x and I've tested it by creating a new project in Unity 4, imported playMaker 1.5 + SmartPool 1.01 + pmSmartPool.

All you have to do after loading a scene (e.g. pmExample) is to add the missing playmakerFSM script to the SmartPool gameobject (like Alex said). After adding the script, click on "edit" before you play the scene, otherwise you'll get a runtime error.

I also added a scene pmRandomSpawn showing how to spawn a random object.

Jake

PS: Sorry for not discussing this in playMaker terms. I'm a coder and from my point of view an action is equivalent to a few lines of code. I primarily use playMaker to create higher level FSMs while most functionality is done in code. I'll try to think more action focused in my future answers, promised!

brendang

  • Junior Playmaker
  • **
  • Posts: 63
    • Out to Play Interactive
Re: SmartPool released with custom actions
« Reply #14 on: February 18, 2013, 08:46:33 AM »
Really appreciate the help help, Jake.  coffee first, Playmaker and Smartpool right after.


best,
B.