playMaker

Author Topic: How to Get value from script  (Read 10871 times)

bloodymin

  • Playmaker Newbie
  • *
  • Posts: 31
How to Get value from script
« on: May 12, 2013, 01:19:42 PM »
What i'm trying to do is
If my C#script code has int value  something  like "speed or health"
and i want to call this value to my playmaker fsm for use.

Is there a other way to handle this problem.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: How to Get value from script
« Reply #1 on: May 12, 2013, 02:53:34 PM »
Is your C# script a MonoBehavior on a GameObject?

If so, use Get Property to access its variables...

bloodymin

  • Playmaker Newbie
  • *
  • Posts: 31
Re: How to Get value from script
« Reply #2 on: May 12, 2013, 10:30:49 PM »
my code is like this


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

public class SUNE : MonoBehaviour {
public static int POTIONHAVE;
void Start () {
AndroidPurchases.Load ();
if(AndroidPurchases.HasPurchased("POTION")){
POTIONHAVE = 4;}
}
}

// Use this for initialization

What i want it to do with this, if user successfully purchased item than i want them store in some kind of int number. then move this number to playmaker FSM. Turn this value into Golbal value to using in my another scene stuff, such as calling GUI, image, and etc. 

Now, i'm trying to use "get property command" what should i select on property section?

bloodymin

  • Playmaker Newbie
  • *
  • Posts: 31
Re: How to Get value from script
« Reply #3 on: May 12, 2013, 10:32:53 PM »
i saw another youtube tutorial about "send message" action.  Can i use it for this cases?

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: How to Get value from script
« Reply #4 on: May 12, 2013, 10:40:34 PM »
Send Message calls a method in your script, but you can't get a return value. So I think you need Get Property in this case.

Here's a tutorial on Get Property:

It doesn't work with Static properties, so you'll need to remove static from POTIONHAVE.


bloodymin

  • Playmaker Newbie
  • *
  • Posts: 31
Re: How to Get value from script
« Reply #5 on: May 12, 2013, 11:55:23 PM »
Code: [Select]
using UnityEngine;
using System.Collections;

public class SUNE : MonoBehaviour {
public  int POTIONHAVE;
void Start () {
AndroidPurchases.Load ();
if(AndroidPurchases.HasPurchased("POTION")){
POTIONHAVE = 4;}
}
}

// Use this for initialization

I'm really sorry, i watched youtube and read others but still confusing.

I changed code like this and drag drop into playfsm.

select get property action

Object Type seemed to be fine it is shown as UnityEditor.MonoScript

But I didn't see anything on Property section drop down box.

in there what i see is name, name.length, text, text.length. 

I still confusing to get integer from my code. 

Should i add code?

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: How to Get value from script
« Reply #6 on: May 13, 2013, 01:47:32 AM »
Drag the SUNE component added to a GameObject into Set Property.

MonoBehaviours only run when added to a GameObject.

The type should say SUNE.

I'm guessing since the type said UnityEditor.MonoScript that you dragged the script file into the target. Not a component.

bloodymin

  • Playmaker Newbie
  • *
  • Posts: 31
Re: How to Get value from script
« Reply #7 on: May 13, 2013, 09:32:46 AM »
Thank you.

Xander Davis

  • Playmaker Newbie
  • *
  • Posts: 24
Re: How to Get value from script
« Reply #8 on: August 13, 2013, 02:40:31 PM »
I have a script attached to a game object with an FSM.

Code: [Select]
#pragma strict

public var CurrentLevel:int;

function Start ()
{
    CurrentLevel = Application.loadedLevel;
}

I'm trying to get the CurrentLevel int property from the script via the FSM on the same object.  I tried Get Property and dragged the game object into it (prefab from the Library), and it didn't show CurrentLevel in the list of options to choose from.

How can I get CurrentLevel?
« Last Edit: August 13, 2013, 03:47:20 PM by Xander Davis »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: How to Get value from script
« Reply #9 on: August 13, 2013, 09:53:48 PM »
You have to drag the actual component into Get Property, not the GameObject. You should then see CurrentLevel.

If the component is on a different GameObject than the FSM you can use Lock in the PlayMaker Editor toolbar to lock the FSM selection while you drag the component in.

wisitnik

  • Playmaker Newbie
  • *
  • Posts: 1
Re: How to Get value from script
« Reply #10 on: August 14, 2013, 06:33:36 AM »
In C# SetLevelVar.cs
Code: [Select]
using UnityEngine;
using System.Collections;

public class SetLevelVar : MonoBehaviour
{
    public int CurrentLevel;
    public string VarName = "Var Name";
    public PlayMakerFSM PlayMaker; // Need to drag component to this var

    void Start()
    {
        CurrentLevel = Application.loadedLevel;
        PlayMaker.FsmVariables.GetFsmInt(VarName).Value = CurrentLevel;
    }
}

Action for PlayMaker GetLoadedLevel.cs and place file at "Assets\PlayMaker\Actions\Application"
Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

[ActionCategory(ActionCategory.Application)]
public class GetLoadedLevel : FsmStateAction
{
    public FsmInt Store;
    public FsmEvent FinishEvent;

public override void OnEnter()
{
        Store.Value = Application.loadedLevel;
        if (FinishEvent != null) Fsm.Event(FinishEvent);
}
}

You just use a script for pass value to your Var.