playMaker

Author Topic: GameObject is Null - is it ok to check for it Every Frame?  (Read 3316 times)

Neikke

  • Full Member
  • ***
  • Posts: 134
GameObject is Null - is it ok to check for it Every Frame?
« on: October 23, 2017, 08:53:28 PM »
I have Magnet and Shield bars indicating their respective states in game, the problem is when my character dies they stay there forever till they deplete over time. And I want them to disappear the moment Player dies (is being destroyed).

So I kind of achieved it by adding GameObject is Null and turning on its Every Frame parameter. So it just checks every frame if my Player isn't dead, and if it is (Is Null) - then does what I want. I just wanted to know if this is a "performance friendly" solution as there is this every frame check involved. I'm aiming for Windows build. Thanks!

Ofonna

  • Full Member
  • ***
  • Posts: 230
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #1 on: October 24, 2017, 01:03:30 AM »
that might not be a huge problem depending on your game,... and your platform is also quite okay but it's still inefficient, you could simply have a global event in your coins fsm, the moment your player dies, use the send event action then select broadcast all and that should solve the problem

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #2 on: October 24, 2017, 03:31:21 AM »
Hi,

 while it's ok if you don't abuse it, it's still far from ideal.

 instead, you should send a global event "PLAYER / DIED" from the fsm that knows about the death of the player, somewhere there is an fsm that is responsible for handling the logic of the death, this fsm should then be responsible to fire that global event for any other interested fsm to know about this.

 then other fsm simply have that global event as a global transition on a state and will be informed properly without messing with checking for null on a gameobject.

 Bye,

 Jean

Neikke

  • Full Member
  • ***
  • Posts: 134
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #3 on: October 24, 2017, 05:20:08 AM »
you could simply have a global event in your coins fsm, the moment your player dies, use the send event action then select broadcast all and that should solve the problem

Thanks for the reply Carmichael! I suppose you meant on Enemies, rather than on Coins, right? Or am I missing something?

Gonna read about Broadcast action now. Thanks!

O

Neikke

  • Full Member
  • ***
  • Posts: 134
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #4 on: October 24, 2017, 05:30:26 AM »
somewhere there is an fsm that is responsible for handling the logic of the death

Hi jeanfabre! The problem is in my case Player's death is not handled by FSM - it can be triggered by two scripts (I'm using Corgi engine from Asset store):

1. "Death bounds" (which checks if the Player is within the Collider bounds and if he leaves it (on collider exit) destroys it)
2. "Kill on Touch" which is assigned on all Enemies and does the same - destroys it.

What would be the best way to solve this? Do I need to Call Method action or Invoke Method as I was advised earlier in other thread? Thanks!

Ofonna

  • Full Member
  • ***
  • Posts: 230
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #5 on: October 24, 2017, 05:35:00 PM »
you could simply have a global event in your coins fsm, the moment your player dies, use the send event action then select broadcast all and that should solve the problem

Thanks for the reply Carmichael! I suppose you meant on Enemies, rather than on Coins, right? Or am I missing something?

Gonna read about Broadcast action now. Thanks!

O

i just re read your question and realised i read it wrong, you just stated that you're not using playmaker to check health so you might have no choice than to check every frame, if your performance drops you could check every second by having two states, one state waits for a second and the other checks if player is null.
alternatively,
if you have some coding ability then you could send event directly from your script whenever player isdead

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #6 on: October 25, 2017, 05:10:01 AM »
Hi,

 then from these script s( did you wrote them?) you should like Carmichael stated broadcast a PlayMaker event.

OR even better, you create a Unity event that your script will call and then in the inspector you will be able to plug that unity event to anything, including firing a PlayMaker event. but it iwll be wired visually ( like UI button OnClick event for example)

 Bye,

 Jean

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #7 on: October 25, 2017, 06:23:33 AM »
Try to avoid using thing that go every frame all of the time unless it's necessary, it's false logic. As they proposed, just send an event (before player dies) that will remove the bars, turn their renderer off, remove them out of camera frustum, whatever you think of.
Available for Playmaker work

Neikke

  • Full Member
  • ***
  • Posts: 134
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #8 on: October 26, 2017, 06:55:51 AM »
Try to avoid using thing that go every frame all of the time unless it's necessary, it's false logic. As they proposed, just send an event (before player dies) that will remove the bars, turn their renderer off, remove them out of camera frustum, whatever you think of.

Thanks! Yes, now I see that it wasn't a very good idea to use EveryFrame in this case!

Hi,

then from these script s( did you wrote them?) you should like Carmichael stated broadcast a PlayMaker event.

OR even better, you create a Unity event that your script will call and then in the inspector you will be able to plug that unity event to anything, including firing a PlayMaker event. but it iwll be wired visually ( like UI button OnClick event for example)

 Bye,

 Jean

No I didn't write them, I bought them from Asset store. So I hope to be able to use some of these scripts functionality and connect them to Playmaker as triggers for other events. I was wondering about Unity event you mentioned.. Not sure I understand.. could you please give some more info on how to do/use it? Thanks!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: GameObject is Null - is it ok to check for it Every Frame?
« Reply #9 on: October 27, 2017, 02:04:46 AM »
Hi,

 to create a UnityEvent you need something like this:

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class NewBehaviourScript : MonoBehaviour {


public UnityEvent OnEvent;

// Update is called once per frame
void Update () {

if (Input.anyKeyDown) {
OnEvent.Invoke ();
}
}
}

then you can target PlayMakerFsm Component and call the SendEvent(string) method.

Bye,

 Jean