playMaker

Author Topic: Gameobject to Object for 'Set property'  (Read 12142 times)

adgrist

  • Playmaker Newbie
  • *
  • Posts: 28
Gameobject to Object for 'Set property'
« on: February 05, 2014, 06:37:24 AM »
This one has me stumped! So I have assets being instantiated from asset bundles, meaning they cannot be set as IsStatic. To do this at runtime I need to use Set Property. So I have tried to find the GameObject, Get Component from the GameObject and store with an Object Variable set to GameObject. This unfortunately does not work. Dragging the GameObject into the FSM and using set property works fine.

So the issue here seems to be, how can I get a GameObject in to an Object Variable at runtime?

adgrist

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Gameobject to Object for 'Set property'
« Reply #1 on: February 05, 2014, 01:48:30 PM »
Just seen someone has the same issue as me but i think the guys trying to help may have been missing his point http://hutonggames.com/playmakerforum/index.php?topic=6258.0

Object has the option for 'Game Object' but using get component from a GameObject will not work :-(

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Gameobject to Object for 'Set property'
« Reply #2 on: February 05, 2014, 02:25:25 PM »
So you have assets in .unitypackage files that you're pulling out at runtime and storing in an array with arrayMaker and want to set them to Static?

Why not just set them to Static and repackage them? If its not at runtime, why not set them to Static in the inspector window?
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

adgrist

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Gameobject to Object for 'Set property'
« Reply #3 on: February 05, 2014, 02:30:32 PM »
Thanks Lane. Nothing to do with Array maker though, that was just a reference to a similar issue.

I am instantiating from asset bundles, which loses the Static setting when instantiated so I cannot set this in the editor. It can be done through Object.GameObject, but I cannot get my runtime models into an Object Variable :-(

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Gameobject to Object for 'Set property'
« Reply #4 on: February 05, 2014, 02:34:35 PM »
Sure you can.

Just make a new Object variable, set it to UnityEngine.GameObject, use Set Property and isStatic is right there.

Now you just need to tell GetComponent to fill that Object variable from the GameObject's you're instantiating and iterate through them.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

adgrist

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Gameobject to Object for 'Set property'
« Reply #5 on: February 05, 2014, 02:40:02 PM »
It doesn't do it. Get Component 'Object.GameObject' from a GameObject returns 'None'. If I drag and drop the GameObject into the FSM manually you're right it totally works. But it won't work when trying it at runtime.

have you tried it out?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Gameobject to Object for 'Set property'
« Reply #6 on: February 05, 2014, 03:19:07 PM »
http://docs.unity3d.com/Documentation/ScriptReference/GameObject-isStatic.html

Hmm looks like a bigger problem is that the isStatic flag is editor only. It shouldn't even change at runtime, according to docs...

weird? it seems to change though. I wonder what the side effects are...
« Last Edit: February 05, 2014, 03:20:39 PM by Lane »
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Gameobject to Object for 'Set property'
« Reply #7 on: February 05, 2014, 04:14:23 PM »
Pretty strange! I think you're right.

Even though the Object variables are identical, it doesn't work if you get them at runtime.

Since that's the case it appears to be some glitch in the Get Component action, unless I'm missing something. Repro scene attached.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

adgrist

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Gameobject to Object for 'Set property'
« Reply #8 on: February 06, 2014, 03:33:03 AM »
Thanks for giving that a go Lane. Will be disappointing if there is no other option at runtime if Is Static is basically disabled. Using Assetbundles is pretty key for us so its frustrating to not be able to optimise as much as we would like.

Maybe Jean has an opinion on the GameObject to Object issue within playmaker though?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Gameobject to Object for 'Set property'
« Reply #9 on: February 07, 2014, 07:25:50 AM »
Hi,

 What are the repro steps? I have the scene but not sure what's not working here.

bye,

 Jean

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Gameobject to Object for 'Set property'
« Reply #10 on: February 07, 2014, 09:19:05 AM »
Hi,

 What are the repro steps? I have the scene but not sure what's not working here.

bye,

 Jean

One cube has been manually filled with a variable in the editor, the other gets it at runtime. Getting it at runtime does not seem to work. They're the same variable.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Gameobject to Object for 'Set property'
« Reply #11 on: February 10, 2014, 07:01:03 AM »
Hi,

Ok, I think I know why:

 a GameObject is not a component, it's the object that has component attached, and as such is not a valid "type" to use with Unity "GetComponent()" method.

if you try do that forcing the type explicitly, it will fire the following:

Assets/PlayMaker/Actions/GetComponent.cs(59,61): error CS0309: The type `UnityEngine.GameObject' must be convertible to `UnityEngine.Component' in order to use it as parameter `T' in the generic type or method `UnityEngine.GameObject.GetComponent<T>()'

So, I think that this is by design and not a bug.

I have added a check to that "Get Component" action to properly account for this case.

 Can you have a go and tell me if that works for you ( I tested and it does, but just to make sure I understand what you are after).

 Also, what's the point of cube 3 test? do you really need to access a Unityengine.Object?!?

bye,

 Jean

ps: until attachment is back again, please find the code source to replace.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;
using System;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.UnityObject)]
[Tooltip("Gets a Component attached to a GameObject and stores it in an Object variable. NOTE: Set the Object variable's Object Type to get a component of that type. E.g., set Object Type to UnityEngine.AudioListener to get the AudioListener component on the camera.")]
public class GetComponent : FsmStateAction
{
        [Tooltip("The GameObject that owns the component.")]
public FsmOwnerDefault gameObject;

[UIHint(UIHint.Variable)]
        [Tooltip("Store the component in an Object variable.\nNOTE: Set theObject variable's Object Type to get a component of that type. E.g., set Object Type to UnityEngine.AudioListener to get the AudioListener component on the camera.")]
public FsmObject storeComponent;

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

public override void Reset()
{
gameObject = null;
storeComponent = null;
everyFrame = false;
}

public override void OnEnter()
{
DoGetComponent();

if (!everyFrame)
{
Finish();
}
}

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

void DoGetComponent()
{
if (storeComponent == null)
{
return;
}

GameObject targetObject = Fsm.GetOwnerDefaultTarget(gameObject);

if (targetObject == null)
{
return;
}

if (storeComponent.ObjectType == typeof(GameObject))
{
storeComponent.Value = targetObject;
}else{
storeComponent.Value = targetObject.GetComponent(storeComponent.ObjectType);
}

}
}
}

adgrist

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Gameobject to Object for 'Set property'
« Reply #12 on: February 12, 2014, 01:15:41 PM »
Just got back from holiday and that works great Jean. The reason for trying to access the GameObject in this was is because it comes from an AssetBundle and that wont enable us to assign IsStatic. Your changes have made it now possible at runtime - Although IsStatic doesn't have any effect until we change scene (With do not destroy on the GameObject) or reload the scene itself. Not sure if there is another way of doing this?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Gameobject to Object for 'Set property'
« Reply #13 on: February 12, 2014, 01:40:17 PM »
Hi,

the thin is, I don't think you understand the principle behind "isStatic", there is no point in setting that at runtime, since it's a perfomance helper that you should define during editing.

https://docs.unity3d.com/Documentation/Components/class-GameObject.html

in there, they explain the mean of thie static check box and it's really something to define during editing, not at runtime.

 I may be wrong, but there is no effect when you set that up at runtime ( at least from a performance point of view)

Bye,

 Jean

adgrist

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Gameobject to Object for 'Set property'
« Reply #14 on: February 12, 2014, 02:00:10 PM »
I do understand the principle of IsStatic or I wouldn't be trying to apply it! As I just stated too, there seems to be no performance benefit at first as expected, until I change scenes, then there is a performance benefit and the models appear to be getting batched.

Also I have said a couple of times in this thread, that we are using AssetBundles to instantiate these models. So how would you suggest I set this up in the editor and still have it applied once instantiated from the bundle?