playMaker

Author Topic: [SOLVED] How do you receive a variable from a custom class method?  (Read 12433 times)

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
How would I go about receiving a variable from a custom class method?

I've applied a script to an object.  From Playmaker I want to trigger a public method in the attached script.  This method will return an int.  I want to store the returned int in a Playmaker variable so I can use it for comparison.
« Last Edit: May 11, 2012, 09:10:32 PM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

justifun

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 280
Re: How do you receive a variable from a custom class method?
« Reply #1 on: May 10, 2012, 10:12:40 PM »
Here's some info on the subject from a question i asked a long time ago

http://hutonggames.com/playmakerforum/index.php?topic=1143.msg4804

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you receive a variable from a custom class method?
« Reply #2 on: May 10, 2012, 10:49:23 PM »
Thanks for the link. =)

However, if I'm understanding that method correctly, it may be pretty limited and a little complicated for reusing the same method over and over through several varying game objects. 

I feel like there should be a generic action that can call a method (typed in manually for flexibility) and store the returned variable into any variable you specify in the field.  This way you can keep the flow of logic inside the easily editable playmaker GUI and it can be reused for anything at anytime.
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you receive a variable from a custom class method?
« Reply #3 on: May 10, 2012, 11:18:40 PM »
I'm trying to make a custom action to fix this, though it won't be as flexible as I'd like it to be.

Here's the code I currently have:

Code: [Select]
using System;

namespace HutongGames.PlayMaker.Actions{

[ActionCategory(ActionCategory.ScriptControl)]
[Tooltip("Invokes a method and gets the returned variable stored.")]

public class GetReturnedMethodVariable : FsmStateAction{
public FsmGameObject myGameObject;
[UIHint(UIHint.Variable)]
[Tooltip("Store the Current HP of unit.")]
public int storeCurrentHP;


// Put all the default values here
public override void Reset (){

}

// This is where all the magic happens.
public override void OnEnter(){
storeCurrentHP = myGameObject.GetCurrentHealth();

Finish();
}
}

So if I want to access the script attached to a certain object, how would I make this call?
The line "storeCurrentHP = myGameObject.GetCurrentHealth();" does not do this. =(
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4000
  • Official Playmaker Support
    • LinkedIn
Re: How do you receive a variable from a custom class method?
« Reply #4 on: May 10, 2012, 11:48:05 PM »
You shouldn't need a custom action to do this. You should just be able to use Get Property and Set Property.
Just drag the script into the Target Object field, and find the property you want to get/set.

But if you want to write the custom action, you have to use GetComponent then call the method on the component:
http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html


Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you receive a variable from a custom class method?
« Reply #5 on: May 11, 2012, 12:30:58 AM »
I'm having a little bit of an issue trying to figure out how to use the get component line.  Could you possibly give me an example line for how it could work in my code above?  It'd be greatly appreciated! =D
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you receive a variable from a custom class method?
« Reply #6 on: May 11, 2012, 12:47:07 AM »
This is my current code that's not working

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

namespace HutongGames.PlayMaker.Actions{

[ActionCategory("Soul Saga")]
[Tooltip("Invokes a method and gets the returned variable stored.")]

public class GetCurrentHP : FsmStateAction{
public GameObject gameObject;
[UIHint(UIHint.Variable)]
[Tooltip("Store the Current HP of unit.")]
public float storeCurrentHP;


// Put all the default values here
public override void Reset (){

}

// This is where all the magic happens.
public override void OnEnter(){
// Use get component to get access to the attached script
UnitInfo varUnitInfo = gameObject.GetComponent(UnitInfo);

// Now get the current HP!
storeCurrentHP = varUnitInfo.GetCurrentHealth();

Finish();
}
}


My Error:
Assets/Assets/Scripts/Playmaker Custom Actions/Access Variable.cs(25,34): error CS0266: Cannot implicitly convert type `UnityEngine.Component' to `UnitInfo'. An explicit conversion exists (are you missing a cast?)
« Last Edit: May 11, 2012, 12:55:26 AM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4000
  • Official Playmaker Support
    • LinkedIn
Re: How do you receive a variable from a custom class method?
« Reply #7 on: May 11, 2012, 12:58:08 AM »
Try:

UnitInfo varUnitInfo = gameObject.GetComponent<UnitInfo>()

The Unity docs show javascript syntax by default. Click on the Language dropdown in the top right corner of the code box to get samples in C#.

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you receive a variable from a custom class method?
« Reply #8 on: May 11, 2012, 01:03:59 AM »
Thank you very much, that compiles now. =D

How do you use the GetComponent with a FSMGameObject?

Using a normal gameobject doesn't seem to  allow me the flexibility to grab the specific game object i need in the game and pass it around the rest of my FSM.
« Last Edit: May 11, 2012, 01:10:01 AM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 4000
  • Official Playmaker Support
    • LinkedIn
Re: How do you receive a variable from a custom class method?
« Reply #9 on: May 11, 2012, 01:25:55 AM »
Use an FsmGameObject instead of a GameObject:
FsmGameObject gameObject;

Then use the Value property to get the GameObject:
gameObject.Value.GetComponent<UnitInfo>();

Of course you'll need to test if Value is null...

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you receive a variable from a custom class method?
« Reply #10 on: May 11, 2012, 01:32:13 AM »
That worked! Thanks! =D

For some reason the spot where I set the value for the storeCurrentHP in the OnEnter() method is not storing this data in the variable for the FSMFloat.  Using a FSM debug action to check the float variable (in log) in a state later keeps showing the value as 0, even when i switch the line to:
storeCurrentHP = 100;

Any idea why?

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

namespace HutongGames.PlayMaker.Actions{

[ActionCategory("Soul Saga")]
[Tooltip("Invokes a method and gets the returned variable stored.")]

public class GetCurrentHP : FsmStateAction{
public FsmGameObject gameObject;
[UIHint(UIHint.Variable)]
[Tooltip("Store the Current HP of unit.")]
public FsmFloat storeCurrentHP;


// Put all the default values here
public override void Reset (){

}

// This is where all the magic happens.
public override void OnEnter(){
// Use Value andthen getComponent to get access to the attached script
UnitInfo varUnitInfo = gameObject.Value.GetComponent<UnitInfo>();


// Now get the current HP!
//storeCurrentHP = varUnitInfo.GetMaxHealth();
storeCurrentHP = 100;

Finish();
}
}
« Last Edit: May 11, 2012, 01:49:37 AM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you receive a variable from a custom class method?
« Reply #11 on: May 11, 2012, 08:07:10 PM »
Running a unity debug to console shows that the OnEnter() event in the custom action is definitely triggering, but it's not storing the value into the FSM variable for some reason.  Anyone know why?
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you receive a variable from a custom class method?
« Reply #12 on: May 11, 2012, 09:04:03 PM »
In case anyone else runs into this problem:

I opened up the SetIntValue action (script) packaged with Playmaker.  From there I realized that I was treating the FsmFloat object like a real variable, when the fact is I needed to target it's value member.

So to actually set an FSM variable, you need to do something like:
Code: [Select]
FsmFloat storeValue;
float settingValue;
storeValue.Value = settingValue;

Notice that the FsmFloat variable is actually a class, so the variable is stored in it's public "Value" member.
Also notice that when using the actual float variable, it acts as normal.

Thanks to everyone who was helping me through my extreme noobie fumbling. =)
« Last Edit: May 11, 2012, 09:09:36 PM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com