playMaker

Author Topic: Character Spawn Points  (Read 1770 times)

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Character Spawn Points
« on: August 31, 2019, 09:59:24 AM »
Hey there!

I decided to create spawn point objects which create a game object such as a character when the scene loads then destroys itself. I wanted to use these instead of placing the characters directly in the scene to streamline level designing and make things quicker while I work.



However I think I'm not going about this process properly because when I run a scene in editor that includes a few of these spawn points, it tends to crash Unity. In the profile, I see a big spike at the start so I thought to put a random wait between 0 and 1 on the spawn points to space out the object creation. This works okay for now but I can't help think there is a better way to set this sort of thing up.

Another thing that concerns me is I'm creating the player and camera this way but I'm not sure if that could cause problems not having a camera and stuff in the scene while the scene starts up.

Has anyone tried this kind of thing before and would anyone be able to offer some incite to me?

Thanks :)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Character Spawn Points
« Reply #1 on: September 01, 2019, 12:53:26 PM »
Maybe the crash is caused by physics? Are the spawn points set on trigger? Are they touching or going partially through the floor?
Or maybe when it tries to create the player, it loops like crazy. Add a next frame event in some of your states just to see what goes on.

Unity doesn't like not having a camera but that's something you can easily and quickly verify with comparing two scenes, a main one and your level, the first having no camera and the level having one and look at the problems you may encounter.
You can assign cameras on the fly so maybe there won't be any big issue there.

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Character Spawn Points
« Reply #2 on: September 02, 2019, 07:57:58 AM »
Hey, sorry for not getting back to you sooner! I wanted to make sure I had the time to test all these things out before getting back to you.

Maybe the crash is caused by physics? Are the spawn points set on trigger? Are they touching or going partially through the floor?
[...]

The spawn points only spawn a single character when the scene loads then destroy themselves. There are no colliders or anything on the spawn points themselves; they are essentially empty objects with a cube mesh renderer attached. However when the characters spawn, I imagine all their collisions happen at once with the ground so in addition to spacing out the spawns in time, I also added 1 to their Y position as they are created so they aren't all colliding with the floor at once.

[...]
Or maybe when it tries to create the player, it loops like crazy. Add a next frame event in some of your states just to see what goes on.
[...]

The player works the same as the other spawners as kind of a one and done on the first frame so there shouldn't be any looping going on but I will add in some of these "Next Frame Event" actions to make sure everything isn't going off on the same frame.

[...]
Unity doesn't like not having a camera but that's something you can easily and quickly verify with comparing two scenes
[...]

I compared this and didn't see much of a difference. Later on I'll be doing more camera switching within the scenes so there will be multiples anyway.

--

I'm not seeing any crashing now after making these changes so I think it had to do with all the FSMs and physics and everything happening all at the same time and potentially on the same frame. During my searching I also came across the term "Object Pooling" which may also be able to help with this if I can find out a way to do it in PlayMaker.

Thanks for all the suggestions :)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Character Spawn Points
« Reply #3 on: September 02, 2019, 01:17:19 PM »
Ah yes object pooling is quite an important thing to use. You can either create the instances when launching the application or if there aren't that many and you won't to look at them during editing, create a few at first.
There are plenty of free solutions for pooling including an old package for Playmaker.

It's nice that your problem went away but I have the feeling that you just masked the true reason.
If a FSM was looping on itself too many times, it would be written in the Console.
If the crash was caused by something else, and you "solved" it, it would still be good to understand what was going on because from what you describe, there should not be that much of a heavy duty process going on. Eventually, just load elements sequentially and wait to spawn other stuff after that.

Now that Unity won't crash, take a look at the Profiler in Deep Profile mode. It will give you a hint as to what kind of Playmaker function is called too frequently and hits the CPU, GPU (in case of physics) or perhaps (but unlikely) the Memory.

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Character Spawn Points
« Reply #4 on: September 03, 2019, 12:29:14 PM »
[...]
Now that Unity won't crash, take a look at the Profiler in Deep Profile mode. It will give you a hint as to what kind of Playmaker function is called too frequently and hits the CPU, GPU (in case of physics) or perhaps (but unlikely) the Memory.

I'm not too familiar with the Profiler but it looks like where there are big spikes, it's due to "PlayMakerFSM.Update()" inside the BehaviorUpdate loading all the objects and starting the FSMs. I dug through there a bit and saw there's these "List`1.get_Item()" things being called ~3000 times and "FsmEvent.get_eventList()" being called ~7000 times but according to the Total %, they don't account for much of it. I'm not sure what that means.



To me, all the other items in there seem expected like FSMs setting and getting values and so on. Nothing jumps out to me as something is looping or anything like that. And after the initial spikes while the items are drawn in, the Profiler goes back to a low idle. In the picture, you can see the first spike as the player and camera loading in and the later spikes as the NPCs being instantiated. I can imagine if all those went off at the same time as they were, it might exceed the max memory and lock the editor up.

--

It's clear that I need more experience with reading the Profiler so I'll keep tabs on it as I work and get a grasp of what is normal and abnormal.

Thanks for the tips :)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Character Spawn Points
« Reply #5 on: September 03, 2019, 02:04:40 PM »
Loading times can take a lot of time, depending on what you try to charge into the memory and what you want to show.
I see huge spikes but it's normal if there's a lot of construction going on. What you don't want is redundancy, i.e. processes doubling or tripling the amount of work needed.
The next phase would be to fully sequentialize your loading process to spread the computing over more time, it would divide the spikes into smaller ones and make it even easier for you to see what is going on.
There's a Sequence Event action that covers this. Each time the flow enters the state, it triggers the next transition. You don't need to declare them in the action, it's automatic.
Do you use asynchronous loading of your levels?

acornbringer

  • Junior Playmaker
  • **
  • Posts: 75
    • Acornbringer.com
Re: Character Spawn Points
« Reply #6 on: September 04, 2019, 07:30:48 AM »
Scene loading isn't something I have looked into yet but I will definitely do the research before I start building scenes and linking everything together. I never heard of this Sequence Event or "asynchronous loading" before now but I'll make those the first things I look into before I start.

Thanks again for the pointers! This will be a big help :)