playMaker

Author Topic: Sending Events From Prefabs  (Read 4540 times)

IceMaker

  • Playmaker Newbie
  • *
  • Posts: 14
Sending Events From Prefabs
« on: May 21, 2014, 08:02:07 AM »


Im bad at explaining things so included a picture

Upon start game FSM 00SetupGame, 01CreatureSpawner, and 02CombatSystem are created.

WipsEnemy prefab is also created, upon hitting the player will send an event to these three prefabs.

The problem is that they are now renamed with (Clone) so the event is never received.

First I tried to store them all as global game objects but doing this per FSM is going to get ridiculous quickly.

And now secondly I'll be trying to rename it without the (Clone) ending.

Is this the best way to go or is there a cleaner solution ...  ;D


... Renaming didn't work either. Kind of figured.

I had to make the FSM's as prefabs as trying to send events from other prefabs isn't possible, in that case global variables must be used.


-------

Solved this issue by instead of sending an event using a global Boolean instead and having the FSM check the state of that Boolean every frame.

This is an okay solution but I wonder, how do experienced programmers normally handle it?


« Last Edit: May 21, 2014, 01:53:19 PM by IceMaker »

jess84

  • Hero Member
  • *****
  • Posts: 515
Re: ridiculous
« Reply #1 on: May 21, 2014, 10:58:05 AM »
Being a clone (an instance of a prefab) doesn't prevent Events from being received, so that's not the problem.

I'm not sure why it's not working though.

You could always set your 02CombatSystem as a Global Variable GameObject, and select that on the dropdown when setting your message to send, rather than dragging in an object to reference.

IceMaker

  • Playmaker Newbie
  • *
  • Posts: 14
Re: Sending Events From Prefabs
« Reply #2 on: May 21, 2014, 01:55:42 PM »


First time I tried a red error message popped up saying, "Prefabs cannot receive messages and upon play being pressed this will reset." or some such thing,

but now I was going to repeat the error to show you the screen shot and... it worked fine (After restarting Unity).

Thought I was losing my mind because it seems like I've done this many times before with no issues.

Must of been a random glitch.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Sending Events From Prefabs
« Reply #3 on: May 21, 2014, 02:21:01 PM »
You must get the instantiated objects at runtime, then send the event to that object. You can't drag a prefab into a slot for some scene object to interact with because it doesn't exist in the game until you instantiate it. Its like asking a person that is sleeping in another room to talk to you. For instance, what if you created multiple clones of the same prefab? How does it know which one to communicate with since they're all unique scene objects now?

Usually whatever fsm creates the objects will also store them as variables at the same time, then you can share that variable (maybe as a global) and other fsm's can find those instantiated objects. You can also use Find Game Object, but its very slow so use it sparingly.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

brendang

  • Junior Playmaker
  • **
  • Posts: 63
    • Out to Play Interactive
Re: Sending Events From Prefabs
« Reply #4 on: July 14, 2016, 07:19:35 PM »
I know this is an old topic, but I was about to post the exact same question. I can create, say, a control panel for a door that has a button that send a message to a specific door (dragged into the 'specify object FSM' slot).  If I drag another instance of the control panel n door into the scene, the panel has no idea which door to speak with, or worse, is speaking to the original door.

How best to handle Send Event to an instance or in this case, from an instance containing the Send Event?   As Lane mentioned, 'Find Game Object' at the head of such an FSM could work I guess, but I understand it's costly.

Thanks,
B-

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Sending Events From Prefabs
« Reply #5 on: July 17, 2016, 07:41:18 AM »
Hi,
from what i understand you are placing the control panel building your scene and not in runtime
If so you can connect them the same way as normal,
So after you have placed the Control panel prefab connect the preffered door in you send event action.

If it is in runtime and the doors are already in the game you could maybe use an array list with your doors in it