Author Topic: TNManager.Create'ed objects doesn't initialize  (Read 2644 times)

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
TNManager.Create'ed objects doesn't initialize
« on: June 12, 2014, 01:59:23 PM »
Hi!

I have an inventory system where I can drag anything from the inventory and into "the open", ie. the terrain, and I want the game to create a "sack" over the network and fill it whatever I'm throwing away.

Easy enough, one would think, but the problem I'm facing is that Create() doesn't behave like Instantiate, as it doesn't really instantiate the object. Consider this code, which is run whenever I drop an item (or items) into "the open."

  1. public void OnDragEnd ( dfControl source, dfDragEventArgs args ) {
  2.         if ( args.Target.GetType().ToString() == "dfPanel" && args.Data is SlotPanelController ) {
  3.  
  4.                 // Create a new ThrowAwaySack
  5.                 TNManager.Create( "Prefabs/ThrowAwaySack", playerCamera.transform.position, Quaternion.identity, false );
  6.  
  7.                 // Populate the ThrowAwaySack's inventory
  8.                 InventoryController throwAwaySackInventoryController = throwAwaySack.GetComponent<InventoryController>();
  9.                 SlotPanelController slotPanelController = (SlotPanelController)args.Data;
  10.  
  11.                 Slot sourceSlot = slotPanelController.slot;
  12.  
  13.                 int itemsAdded = throwAwaySackInventoryController.AddItems( sourceSlot.items[0], sourceSlot.items.Count );
  14.                 sourceSlot.RemoveItems( sourceSlot.items[0], itemsAdded );
  15.  
  16.                 // Mark as dropped
  17.                 args.State = dfDragDropState.Dropped;
  18.  
  19.         }
  20. }
  21.  

Each ThrowAwaySack has an InventoryController attached to it, and with this code the sack itself gets created over the network just fine. However, its InventoryController is never initialized, ie. it never does Awake(), which is crucial for things to work.

With f.ex. PhotonNetwork, I could do a PhotonNetwork.Instantiate() and get an initialized GameObject back, but TNManager.Create() is void, so...?

Any help is appreciated!

Thanks!

Patterrun

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 12
    • View Profile
Re: TNManager.Create'ed objects doesn't initialize
« Reply #1 on: June 13, 2014, 03:39:46 AM »
If you want to work with the GameObject in Create(), you can do that but you will need to follow the instructions in this thread:

http://www.tasharen.com/forum/index.php?topic=5416.0

Basically you create a custom function callback which is called an RCC that is used in CreateEx() (instead of simply Create() ). He also wraps the CreateEx() call in a static function for easier use. In the custom RCC you make you can work with the GameObject that is created in a normal Create() call. It is tedious but it is worth it if you need to work with the GameObjects in Create().
« Last Edit: June 13, 2014, 03:45:06 AM by Patterrun »

toreau

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: TNManager.Create'ed objects doesn't initialize
« Reply #2 on: June 14, 2014, 08:25:48 AM »
Thanks for the reply!

I changed my code to look like this:

  1. void Start () {
  2.         playerCamera = PlayerController.instance.playerCamera;
  3.         throwAwaySackResource = (GameObject)Resources.Load( "Prefabs/ThrowAwaySack" );
  4. }
  5.  
  6. public void OnDragEnd ( dfControl source, dfDragEventArgs args ) {
  7.         if ( args.Target.GetType().ToString() == "dfPanel" && args.Data is SlotPanelController ) {
  8.  
  9.                 // Create a new ThrowAwaySack
  10.                 this.Create( throwAwaySackResource, playerCamera.transform.position, Quaternion.identity, false, args );
  11.  
  12.                 // Mark as dropped
  13.                 args.State = dfDragDropState.Dropped;
  14.  
  15.         }
  16. }
  17.  
  18. static public void Create ( GameObject prefab, Vector3 pos, Quaternion rot, bool persistent, dfDragEventArgs args ) {
  19.         TNManager.CreateEx( 10, persistent, prefab, pos, rot, args );
  20. }
  21.  
  22. [RCC(10)]
  23. static GameObject OnCreate ( GameObject prefab, Vector3 pos, Quaternion rot, dfDragEventArgs args ) {
  24.         GameObject go = Instantiate( prefab, pos, rot ) as GameObject;
  25.  
  26.         // Populate the ThrowAwaySack's inventory
  27.         InventoryController throwAwaySackInventoryController = go.GetComponent<InventoryController>();
  28.         SlotPanelController slotPanelController = (SlotPanelController)args.Data;
  29.        
  30.         Slot sourceSlot = slotPanelController.slot;
  31.        
  32.         int itemsAdded = throwAwaySackInventoryController.AddItems( sourceSlot.items[0], sourceSlot.items.Count );
  33.         sourceSlot.RemoveItems( sourceSlot.items[0], itemsAdded );
  34.  
  35.         return go;
  36. }
  37.  

...but now TNet complains that "The game object was not found in the TNManager's list of objects." I tried adding the ThrowAwaySack prefab to TNManager, and while the error message went away a) I don't think that's the right colution (or is it?) and b) the ThrowAwaySack wasn't instantiated because OnCreate() is never run.

What am I doing wrong?
« Last Edit: June 14, 2014, 08:35:03 AM by toreau »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.Create'ed objects doesn't initialize
« Reply #3 on: June 15, 2014, 06:18:19 AM »
Instead of passing the game object reference, pass the string "Prefabs/ThrowAwaySack". TNet is telling you the game object you're passing to the create function is not on the "known prefabs" list. Game object references are not shared between computers. You need ways to identify them -- either by adding them to TNManager's list of objects, or by passing the actual path to the object in the Resources folder.