playMaker

Author Topic: Custom Action - OverlapSphere  (Read 4584 times)

Nightreaver

  • Playmaker Newbie
  • *
  • Posts: 2
Custom Action - OverlapSphere
« on: January 10, 2013, 04:02:43 PM »
Heyho

I want to create a function that uses the Physics.OverlapSphere as a main part to collect gameObjects.
it should make use of playmaker arraymaker-Addon to collect them and taking action to each in an FSM.

So, here is what i got so far

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;
using System.Collections.Generic;


[ActionCategory(ActionCategory.Physics)]
public class GetObjectsInRange : FsmStateAction
{

public FsmOwnerDefault scanOrigin;

public FsmFloat scanRange;

[UIHint(UIHint.Layer)]
[Tooltip("Pick only from these layers.")]
public FsmInt[] layerMask;

[Tooltip("Invert the mask, so you pick from all layers except those defined above.")]
public FsmBool invertMask;

public FsmEvent ErrorEvent;

PlayMakerArrayListProxy colliders;

[RequiredField]
[Tooltip("The gameObject with the PlayMaker ArrayList Proxy component")]
[CheckForComponent(typeof(PlayMakerArrayListProxy))]
public FsmOwnerDefault arrayListOwner;

public override void Reset()
{
ErrorEvent = null;
scanRange = 125;
layerMask = new FsmInt[0];
invertMask = false;
}

public override void OnEnter()
{
GameObject go = Fsm.GetOwnerDefaultTarget(scanOrigin);
GameObject listOwner = Fsm.GetOwnerDefaultTarget(arrayListOwner);

float range = scanRange.Value;

Collider [] colliders = Physics.OverlapSphere(go.transform.position, range, ActionHelpers.LayerArrayToLayerMask(layerMask, invertMask.Value));

if (colliders.Length == 0) {
Fsm.Event(ErrorEvent);
}


foreach (Collider col in colliders)
{
Debug.Log(col.transform.position);
//FsmGameObject referenceObject = col.gameObject;
//listOwner.transform.GetComponent(PlayMakerArrayListProxy).Add(referenceObject, "gameObject");
}

Debug.Log(colliders);

Finish();
}


}

Play Maker Array List Proxy Script and FSM are attached to a single object, which is surrounded by different other objects.

Things that i'm actually unsure about are:

"colliders"-Type
how to store it as/in an PlayMakerArrayList ?

anyone can help?

Thank You
« Last Edit: January 10, 2013, 04:09:05 PM by Nightreaver »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Custom Action - OverlapSphere
« Reply #1 on: January 17, 2013, 01:39:05 AM »
Hi,

 Ok, I have modified the action so that it works. A number of errors was there. not just related to ArrayMaker usage, but you were not far :)


-- when you build a playmaker Action, it's always good to start off a template, either using the PlayMaker "Custom Action wizard" or simply copy paste an existing action from the official set.

-- when you work with ArrayMaker, it's easier if you extend the action with "ArrayListActions" instead of "FsmStateAction". It will give you straight away all the functions you need to work with ArrayList.

-- use "SetUpArrayListProxyPointer(Fsm.GetOwnerDefaultTarget(arrayListOwner),arrayListReference.Value" to initiate the action properly so that arrayMaker is ready to work

-- use isProxyValid() always to make sure arrayMaker is still ok to work

-- finally, use "proxy", this is your arrayList.

 If you have more questions about how to work it out, let me know :)


Code: [Select]
using UnityEngine;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("ArrayMaker/ArrayList")]
[Tooltip("get Objects listed by an OverlapSphere cast")]
public class GetObjectsInRange : ArrayListActions
{

public FsmOwnerDefault scanOrigin;

public FsmFloat scanRange;

[UIHint(UIHint.Layer)]
[Tooltip("Pick only from these layers.")]
public FsmInt[] layerMask;

[Tooltip("Invert the mask, so you pick from all layers except those defined above.")]
public FsmBool invertMask;

public FsmEvent ErrorEvent;

PlayMakerArrayListProxy colliders;

[RequiredField]
[Tooltip("The gameObject with the PlayMaker ArrayList Proxy component")]
[CheckForComponent(typeof(PlayMakerArrayListProxy))]
public FsmOwnerDefault arrayListOwner;

[Tooltip("Author defined Reference of the PlayMaker ArrayList Proxy component (necessary if several component coexists on the same 'arrayListOwner' GameObject)")]
public FsmString arrayListReference;


public override void Reset()
{
arrayListOwner = null;
arrayListReference = null;

ErrorEvent = null;
scanRange = 125;
layerMask = new FsmInt[0];
invertMask = false;
}


public override void OnEnter()
{

if ( SetUpArrayListProxyPointer(Fsm.GetOwnerDefaultTarget(arrayListOwner),arrayListReference.Value) )
FindObjectInRange();

Finish();
}

void FindObjectInRange()
{
if (! isProxyValid() )
{
return;
}

GameObject go = Fsm.GetOwnerDefaultTarget(scanOrigin);
GameObject listOwner = Fsm.GetOwnerDefaultTarget(arrayListOwner);

float range = scanRange.Value;

Collider [] colliders = Physics.OverlapSphere(go.transform.position, range, ActionHelpers.LayerArrayToLayerMask(layerMask, invertMask.Value));

if (colliders.Length == 0) {
Fsm.Event(ErrorEvent);
}


foreach (Collider col in colliders)
{
Debug.Log(col.transform.position);

//FsmGameObject referenceObject = col.gameObject;
//listOwner.transform.GetComponent(PlayMakerArrayListProxy).Add(referenceObject, "gameObject");
proxy.Add(col.gameObject,"gameObject");
}



Debug.Log(colliders);

Finish();
}
}
}

bye,

 Jean

greg

  • Junior Playmaker
  • **
  • Posts: 68
Re: Custom Action - OverlapSphere
« Reply #2 on: April 11, 2013, 08:30:43 AM »
Thanks for this jean, just came in very handy. Should be part of the default actions, as well as arrayMaker in my opinion.