playMaker

Author Topic: Vector3Int in custom actions  (Read 2535 times)

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
Vector3Int in custom actions
« on: March 06, 2018, 05:48:27 PM »
Hi,
I'm trying my hands on writing a custom action and I got a problem.
I'm trying write for the Unity function SetTile(Vector3Int position, Tilemaps.TileBase tile). But I'm not sure how I can get the Vector3Int, but I get this: "Unsupported parameter type: UnityEngine.Vector3Int"

How would I go about to do this? The Action itself seems fairly simple (not sure if it will work).

Any help appreciated!

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2016. All rights reserved.
using UnityEngine;
using UnityEngine.Tilemaps;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Tilemaps")]
[Tooltip("Sets a tile at the given XYZ coordinates of a cell in the tile map.")]

public class SetTile : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(Tilemap))]
[Tooltip("Tilemap to place the tile on.")]
public Tilemap tilemap;

[RequiredField]
[Tooltip("The cell position to place the tile.")]
public Vector3Int vector3IntValue;

[RequiredField]
[Tooltip("The tile to set.")]
public RuleTile ruleTile;

public override void Reset()
{
tilemap = null;
vector3IntValue = new Vector3Int(0,0,0);
ruleTile = null;
}

public override void OnEnter()
{
tilemap.SetTile (vector3IntValue, ruleTile);
}

public override void OnUpdate()
{
}
}
}
« Last Edit: March 06, 2018, 07:34:14 PM by DanielThomas »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Vector3Int in custom actions
« Reply #1 on: March 07, 2018, 03:19:51 AM »
Hi.
I think vector3int is not supported yet to display the variable (PM 1.8.9) , but it can be used private.
It might be already included in the playmaker beta, i will ask Alex.

for now you have 2 options.

You can get each vector separately (FsmInt)
Or you can use a fsmvector3
on the code below both are implemented :


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2018. All rights reserved.
//--- __ECO__ __PLAYMAKER__ __ACTION__ ---//

using UnityEngine;
using UnityEngine.Tilemaps;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Tilemaps")]
[Tooltip("Sets a tile at the given XYZ coordinates of a cell in the tile map.")]

public class SetTile : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(Tilemap))]
[Tooltip("Tilemap to place the tile on.")]
public Tilemap tilemap;

        public FsmInt vector3X;
        public FsmInt vector3Y;
        public FsmInt vector3Z;
        [Tooltip("If a vector 3 is set, the above variables will be ignored")]
        public FsmVector3 vector3Variable;

        [RequiredField]
[Tooltip("The cell position to place the tile.")]
private Vector3Int vector3IntValue;

[RequiredField]
[Tooltip("The tile to set.")]
        [ObjectType(typeof(UnityEngine.Tilemaps.TileBase))]
        public FsmObject ruleTile;

public override void Reset()
{
            vector3X = new FsmInt { UseVariable = true };
            vector3Y = new FsmInt { UseVariable = true };
            vector3Z = new FsmInt { UseVariable = true };
            vector3Variable = new FsmVector3 { UseVariable = true };
            tilemap = null;
ruleTile = null;
}

public override void OnEnter()
{
            if (vector3Variable == null)
            {
                vector3IntValue = new Vector3Int(vector3X.Value, vector3Y.Value, vector3Z.Value);
            }
            else
            {
                vector3IntValue = new Vector3Int(Mathf.RoundToInt(vector3X.Value), Mathf.RoundToInt(vector3Y.Value), Mathf.RoundToInt(vector3Z.Value));
            }
            tilemap.SetTile (vector3IntValue, ruleTile.Value as TileBase);
}

public override void OnUpdate()
{
}
}
}
« Last Edit: March 07, 2018, 04:40:12 AM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Vector3Int in custom actions
« Reply #2 on: March 07, 2018, 04:43:43 AM »
Hi,
I also made a package for easier download,
if more people are interested i can add it to the ecosystem.

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
Re: Vector3Int in custom actions
« Reply #3 on: March 07, 2018, 06:06:31 AM »
Thanks djaydino, very educational!
 
I changed this line to use the vector3 variable:
Code: [Select]
vector3IntValue = new Vector3Int(Mathf.RoundToInt(vector3Variable.Value.x), Mathf.RoundToInt(vector3Variable.Value.y), Mathf.RoundToInt(vector3Variable.Value.z));
So it uses the vector3 position.
I will keep testing this and let you guys know if everything works as intended.

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
Re: Vector3Int in custom actions
« Reply #4 on: March 07, 2018, 05:53:31 PM »
I guess I can continue this thread as this seems related.
I'm trying to make another action for WorldToCell:
file:///C:/Program%20Files/Unity/Editor/Data/Documentation/en/ScriptReference/GridLayout.WorldToCell.html

Unity tells me "Assets/Add-Ons/PlayMaker/PlayMaker Custom Actions/Tilemaps/WorldToCell.cs(71,29): error CS0029: Cannot implicitly convert type `UnityEngine.Vector3Int' to `HutongGames.PlayMaker.FsmVector3'
"

Here is the script
Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2018. All rights reserved.
//--- __ECO__ __PLAYMAKER__ __ACTION__ ---//

using UnityEngine;
using UnityEngine.Tilemaps;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Tilemaps")]
[Tooltip("Converts a world position to cell position.")]
//public Vector3Int WorldToCell(Vector3 worldPosition);

public class WorldToCell : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.Variable)]

[CheckForComponent(typeof(GridLayout))]
[Tooltip("Gridlayout to use.")]
public GridLayout gridLayout;

[UIHint(UIHint.Variable)]
[Tooltip("World position to convert.")]
public FsmVector3 vector3Variable;
        public FsmFloat vector3X;
public FsmFloat vector3Y;
public FsmFloat vector3Z;

[UIHint(UIHint.Variable)]
[Tooltip("Store the converted cell position.")]
public FsmVector3 storeResult;

[Tooltip("Repeat every frame.")]
public bool everyFrame;

private Vector3Int vector3IntValue;
private Vector3Int storeVector3;

public override void Reset()
{
gridLayout = null;
vector3Variable = new FsmVector3 { UseVariable = true };
vector3X = new FsmFloat { UseVariable = true };
vector3Y = new FsmFloat { UseVariable = true };
vector3Z = new FsmFloat { UseVariable = true };
storeResult = new FsmVector3 { UseVariable = true };
everyFrame = false;
}

public override void OnEnter()
{
DoWorldToCell();

if (!everyFrame)
Finish ();
}

public override void OnUpdate()
{
DoWorldToCell();
}

void DoWorldToCell()
{
if (vector3Variable == null) // If user isn't using a vector3 variable, use XYZ values for world position.
vector3IntValue = new Vector3Int(Mathf.RoundToInt(vector3X.Value), Mathf.RoundToInt(vector3Y.Value), Mathf.RoundToInt(vector3Z.Value));

else // Use the vector3 for world position.
vector3IntValue = new Vector3Int(Mathf.RoundToInt(vector3Variable.Value.x), Mathf.RoundToInt(vector3Variable.Value.y), Mathf.RoundToInt(vector3Variable.Value.z));

storeResult = gridLayout.WorldToCell(vector3IntValue);
}
}
}

DanielThomas

  • Beta Group
  • Full Member
  • *
  • Posts: 150
Re: Vector3Int in custom actions
« Reply #5 on: March 07, 2018, 06:02:30 PM »
Got it, the fsm variable neede .Value..

storeResult.Value = gridLayout.WorldToCell(vector3IntValue);

Learning, Learning..

For some reason the "Set Tile" Action doesn't doesn't let the state go to the next state with FINISHED transition. Adding a "next frame event" does the job, but thinking if there is something wrong with my Action code? (the Set Tile is alone in the state)
« Last Edit: March 07, 2018, 06:43:30 PM by DanielThomas »