playMaker

Author Topic: How to reach for instanced prefab's De/Activation? [SOLVED]  (Read 2492 times)

Neikke

  • Full Member
  • ***
  • Posts: 134
How to reach for instanced prefab's De/Activation? [SOLVED]
« on: October 08, 2017, 05:24:42 PM »
Hi,

I have player character which collects coins and it works fine (Djaydino - many thanks!) the thing is, it's being spawned at runtime on a specific spot as a prefab actually - not as a original player. And in FSMs, for example, I have a Move Towards action which makes coins go directly to player, and with the instanced Player it's not working though I tried setting player as global GameObject variable and feeding it Target Object to be attracted to. Not working.

Another example, I'm activating a game object set up as magnet to be a child object of Player, and it works fine as original Player, but when instanced - my Activate Game Object event - just won't wake up. Any help is much appreciated!
« Last Edit: October 09, 2017, 03:37:59 PM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: How to reach for instanced prefab's De/Activation?
« Reply #1 on: October 08, 2017, 11:00:44 PM »
Hi.
You should look into pooling instead of instantiating.

But you can do this,

Set a global game object variable "player" then @the start of the game use "set gameobject" and set the player.
Set the move toward target to the global.

For the magnet, are you instantiating the magnet or the player with the magnet included
If only the magnet you should have it already on the player.

Instantiating is an expencive proccess and should be avoided.
« Last Edit: October 08, 2017, 11:02:21 PM by djaydino »

Neikke

  • Full Member
  • ***
  • Posts: 134
Re: How to reach for instanced prefab's De/Activation?
« Reply #2 on: October 09, 2017, 05:34:16 AM »
Hi!

No no, I'm not instantiating. I'm spawning my character with magnet gameobject as a child object but deactivated. And then when player collides with magnet powerup - it's just being activated for a period of time and then deactivates itself.

The problem was that I actually stupidly placed Get Owner action which stored my Magnet as global gameobject variable - to deactivated magnet itself, which prevented it from being initialized as global variable at runtime. So I placed it on an always active character so it can be initialized and called upon when needed! Thanks man!
« Last Edit: October 09, 2017, 05:47:33 AM by Neikke »

Neikke

  • Full Member
  • ***
  • Posts: 134
Re: How to reach for instanced prefab's De/Activation? [SOLVED]
« Reply #3 on: October 15, 2017, 10:38:52 AM »
For some strange reason, it's just stopped working.. Can't initialise deactivated game object at start.

I tried to pool the magnet game object, and at first it works fine, but when I destroy it after some given time - GetOwner action loses deactivated game object with magnet. What am I doing wrong?
« Last Edit: October 15, 2017, 10:45:29 AM by Neikke »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: How to reach for instanced prefab's De/Activation? [SOLVED]
« Reply #4 on: October 15, 2017, 11:51:48 AM »
Hi,
I think you misunderstand the concept of pooling.

When pooling you should not Create/Destroy objects, only activate/deactivate.
(creating and destroying is expensive and should be avoided as much as possible)

Also i am not sure why you are creating the 'Magnet GameObject'
You should have it already on the player @ start. or at least have it in the scene and parent it to the player.  :)

Neikke

  • Full Member
  • ***
  • Posts: 134
Re: How to reach for instanced prefab's De/Activation? [SOLVED]
« Reply #5 on: October 15, 2017, 08:12:12 PM »
Hi,
I think you misunderstand the concept of pooling.

When pooling you should not Create/Destroy objects, only activate/deactivate.
(creating and destroying is expensive and should be avoided as much as possible)

Yeah that's exactly what I wanted to achieve :) I had magnet object parented to my player previously, but I wasn't able to initialise it as global game object variable at start with Get Owner action hanging on deactivated object.. That's why I wanted to go the other way and create the magnet at runtime but that caused different type of problems.. so I still want to find some way to activate/deactivate magnet as gameobject.

Here's my setup:

On coins: I have FSM with trigger event, which on collide with Player Tag - Moves the Coin Toward Player and creates a particle puff and sends +1 to coin manager. So when Player doesn't have Magnet activated - Player collides with coins using his own small (player-sized) collider and though technically it still drags (Moves Toward) coins to player - Player's collider radius is too small for this micro-moving towards player to be visible.

On Player: I have FSM which on collision with Magnet Powerup - activates Magnet GameObject parented to Player and which is just a much larger radius collider to attract coins from long distances.

So the problem I have - I can't feed my parented Magnet to Get Owner action. Debug just shows 'None' in Get Owner script if it starts deactivated..

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: How to reach for instanced prefab's De/Activation? [SOLVED]
« Reply #6 on: October 15, 2017, 09:34:30 PM »
Hi,
You should have the trigger event on the magnet not on the coins.
Many triggers get expencive also.

Just as i showed on the video and use tags for coins / powerups

Neikke

  • Full Member
  • ***
  • Posts: 134
Re: How to reach for instanced prefab's De/Activation? [SOLVED]
« Reply #7 on: October 15, 2017, 09:55:08 PM »
That's perfectly fine - I'll have it on Magnet and remove triggers from coins but the main problem remains - how do I Activate my Magnet if it's disabled and won't initialise itself from the start as global game object variable?

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: How to reach for instanced prefab's De/Activation? [SOLVED]
« Reply #8 on: October 16, 2017, 01:45:31 AM »
Put it in a child object and use activate children?
Available for Playmaker work

Neikke

  • Full Member
  • ***
  • Posts: 134
Re: How to reach for instanced prefab's De/Activation? [SOLVED]
« Reply #9 on: October 16, 2017, 09:23:04 PM »
Hi,
You should have the trigger event on the magnet not on the coins.
Many triggers get expencive also.

Just as i showed on the video and use tags for coins / powerups

I may sound weird but once again I want to thank you for the video you've made in my "How to make magnets" thread, as after watching it for 20 plus times (it was hard for me initially as I didn't know much about PM) and especially after watching your 'PlayMaker Global events and Global Transitions' I finally started to understand what's going on and how to use them! So now, instead of activating deactivating magnets and other powerups, I enable/disable FSMs on them which works just perfectly and is more flexible!

Thanks a lot Dino!

P.S. Krmko, sorry I really went the wrong way initially so now it all works without any extra steps!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: How to reach for instanced prefab's De/Activation? [SOLVED]
« Reply #10 on: October 17, 2017, 05:45:02 AM »
Hi.
No Problem, happy to help :)