Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Official Spawn System

Discussion in 'Open Projects' started by Muffio, Sep 30, 2020.

  1. Muffio

    Muffio

    Joined:
    Jun 18, 2017
    Posts:
    3
    Hi!
    I've noticed that the Spawn System card seems to be a high priority.
    (https://open.codecks.io/unity-open-project-1/decks/15/card/15q-spawn-system).

    I'd like to work on this mechanic if possible.

    Although, some clarification would be much appreciated:
    What exactly is a "game level" in this context? Is this an independent scene, or can it be multiple places in the same scene?

    There appears to be a separate "Scene loading system" card. Do these two systems work together, or independently?

    Thanks :)
     
  2. ProgrammerTurtle

    ProgrammerTurtle

    Joined:
    Oct 8, 2018
    Posts:
    28
    Hey there,

    I would like to work on the https://open.codecks.io/unity-open-project-1/decks/15/card/15q-spawn-system

    If accepted by Unity, I would like to get some little bit more specifics on how exactly you want it to work like if the spawn is purely for scene changes or you want spawning/de-spawning to be done during runtime too.

    If not specified I can just go wild and go free-style trying to cover all the features that I can think of.

    Since there isn't any principles provided(at least from what I've seen.) I am going to with KISS if stated otherwise.
     
  3. rsaz

    rsaz

    Joined:
    Aug 7, 2017
    Posts:
    3
  4. ProgrammerTurtle

    ProgrammerTurtle

    Joined:
    Oct 8, 2018
    Posts:
    28
    Here is the solution I have come up with https://github.com/EmreB99/open-project-1/blob/master/UOP1_Project/Assets/Scripts/SpawnManager.cs . I can send a request if approved @cirocontinisio


    In addition of spawning the character on scene changes + with a publicly callable SpawnCharacter method for runtime spawnings, I also implemented an event system which gets invoked after a character is spawned in scene.
    The event takes the GameObject that was spawned as a parameter for furthermore runtime checks. This is an utility that I use on my own personal projects to check from my game manager which player is spawned, do the spawned player has any specific components, tags etc than act accordingly.
     
    Last edited: Sep 30, 2020
  5. RKS13D

    RKS13D

    Joined:
    May 20, 2019
    Posts:
    6
    I am starting to work on Spawn System what do you guys think of a Singleton approach something like this...
     

    Attached Files:

  6. NicknEmart

    NicknEmart

    Joined:
    Sep 9, 2019
    Posts:
    46
    I don't know wether to blame my poor english or my kinda poor Unity knowledge, but I don't understand what this feature is supposed to do. What exactly means to "spawn" a player? Isn't the player... already spawned, when we start the scene with the gameobject inside?

    I'm sorry if mine is a dumb question, I want to contribute but I don't know how to
     
  7. Muffio

    Muffio

    Joined:
    Jun 18, 2017
    Posts:
    3
    Yeah, there seems to be a bit of uncertainty as to how the player will be transported around the world.
    I assume there are the following 2 possibilities:
    1) The spawn system must move the player between different scenes.
    In this case, "spawning" the player would refer to instantiating a new player into the newly loaded scene.
    OR
    2) The spawn system is responsible for teleporting the player between different areas in the same scene.
    In this case, "spawning" would simply refer to teleporting the player around.

    I assumed it was the latter, and have submitted a pull request with my implementation of that system, but am not certain if that is how it will work in the final game.
     
  8. NicknEmart

    NicknEmart

    Joined:
    Sep 9, 2019
    Posts:
    46
    Thanks for the reply. I thought about that second option, but in that case we wouldn't need a separate scene just to handle scene loading, since we will be having very few scenes. But at this point I'm starting to think that I just really don't know much about the engine because this isn't the only card that I don't understand.
    That said, let's try and see what I can do. If it goes wrong, I'm just going to do some story and narrative stuff instead :D
     
  9. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    Usually a spawn system would populate the world with enemies, power ups, wildlife, npc's, etc obects or even weather systems, particles, etc, and manage which are active or disabled as the player moves around the level. It may handle loading the level (if you had multiple), creating the right player character (if you had more than 1 character to choose from), randomize where dynamic things are created each time the game runs, etc.

    If the game world is simple and its PC its probably fine to arrange meshes for the level through the editor but usually in games you're conscious of performance and load items as needed with a spawner. If it were mobile or webgl and you had 100+ rigged bipeds, or even monobehaviours, in the level, you only take the hit of animating/rendering/running the ones around the player, not the ones that are offscreen across the map, to keep performance up.

    Regarding scenes:
    Scenes in Unity can be layered, so you could have all your UI stuff in a scene, have the level's terrain and static meshes as a scene, and additively combine them into the live scene, its good for some things because you can load and unload them to free up memory when you're done with that content. Often its for massive worlds but it can also be used for treating tutorials as a module you load/unload while the player is in the game.

    Sometimes its nice to split UI into a scene for separation of duties, someone can be working on that scene and own everything in it, while someone else is working on the game world without having to merge each others work together every time they commit the monolithic scene.
     
    365667276 likes this.
  10. stickylab

    stickylab

    Joined:
    Mar 16, 2016
    Posts:
    88
    maybe yes
     
  11. RKS13D

    RKS13D

    Joined:
    May 20, 2019
    Posts:
    6
    My Idea about system is something like this:
    Have a Player Prefab variable
    StartLoaction transform Variable
    List of CheckPoints location (checkpoints will be game object with a physics collider set to triggered)
    lastCheckPoint Variable

    Function ScanScene() called when new scence is open or if scenes are merged
    first search for startlocation
    the fuction will scan for if all the Check points and add it to Check points list

    Function UpdateCheckPoints()
    this will update the last checkoint reached by the player

    Function Spawn()
    to spawn player is not there on startLocation
    or if exists but dead then spawn on the Last Checkpoint

    As the System is will be inherited from singleton so it will be scene Independent

    https://open.codecks.io/unity-open-project-1/decks/15/card/15q-spawn-system
     
  12. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    884
    This is the official thread to discuss the Spawn System (roadmap card).

    Please start with simple implementations that generally stick to what the card on the roadmap is saying. There will be time to expand the functionality.
     
    luisquid likes this.
  13. LayalHF

    LayalHF

    Joined:
    Jun 26, 2019
    Posts:
    1
    Hello,
    I would like to work on the Spawn System
    Will make it simple:
    1. spawn point prefab
    2. function that spawns the player prefab where ever the spawn point location is and in the right direction
     
  14. LLargemaNN

    LLargemaNN

    Joined:
    Apr 10, 2017
    Posts:
    1
    I would like to work on it too. So I just start to work on the spawn system and I send a pull request on github?
     
  15. Wodopo

    Wodopo

    Joined:
    Feb 19, 2013
    Posts:
    4
    Hello everyone!

    I would like to discuss a phew points about the Card and Game Flow Chart.
    My interpretation:
    • The card suggests it's a Player spawn system, rather than a Generic Enemy/Item spawn system.
    • The way the game flow chart is presented it looks like designer placed enemies. Not that it can't make use of a spawn system, it’s just that, based on the card, seems like a different task.
    What do you all think?
     
  16. Ciremia

    Ciremia

    Joined:
    Feb 4, 2020
    Posts:
    12
  17. Wodopo

    Wodopo

    Joined:
    Feb 19, 2013
    Posts:
    4
    I agree with you. Even though I'm one of does that got excited and started working before reading everything. :p
     
  18. rsaz

    rsaz

    Joined:
    Aug 7, 2017
    Posts:
    3
    I requested to work on this functionality but didn't start until they assign me to the task. I agree with you, will be waste of time and maybe frustrating if you work in something that is not even going to be considered. :)
     
  19. ThePineappleDev

    ThePineappleDev

    Joined:
    Mar 26, 2015
    Posts:
    8
    The contribution guidelines also encourage people to work together on features.

    The people in the Inventory system thread are having a nice discussion over the different ways to implement it. I think that would be fruitful here too (https://forum.unity.com/threads/inventory-system.980646/)

    In that sense It might also make sense to have several people work on/discuss the same fork, rather than everyone forking for a specific feature seperately.

    Based on that (I don't mean to Hijack anything or kick anyone's shins), im going to go out and propose the beginnings of a simple solution to start off the discussion.

    Based on the card I think a few things can be highlighted:
    -Needs to be scene independent, and possibly one Prefab only with no dependencies.
    Based on this I think a simple solution would be to use the position of the prefab in the scene as an indication as to where the player will be spawned. This would fit decently well with the requirement of 'just having this prefab in allows the developer to just press play and test the game' . An alternative is keeping the spawn coordinate(s) stored in the separate instances of the prefab. Especially when a scene can end up with more possible spawn locations.

    This is part of the issue I think, there are some scenes where the player can be spawned in two or three locations, dependon on what 'direction' they entered the scene from. How will we know what position to spawn the player in if they also recommend we don't have any dependencies? Will we be able to monitor the game state or ' previous scene' at some point?

    I think based on the card and the current requirements a simple solution with a (list of) coordinate(s) will work for now, but determining the spawn location is a problem for the near future.
     
    Last edited: Oct 3, 2020
    cirocontinisio likes this.
  20. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    464
    In my game I solved this by creating a simple scriptable object.
    Code (CSharp):
    1. public class SceneIdentifier : ScriptableObject {}
    That just identifies scene.

    Also, in each scene there is one instance of component:
    Code (CSharp):
    1. public class SetCurrentScene : MonoBehaviour
    2. {
    3.   [SerializeField] private SceneIdentifier sceneIdentifier;
    4.  
    5.   void Start()
    6.   {
    7.      AkaGameState.LastVisitedScene = sceneIdentifier;
    8.   }
    9. }
    Also, for each "door" that leads to current scene there is another component
    Code (CSharp):
    1. public class PlayerSpawnPoint : MonoBehaviour
    2. {
    3.   [SerializeField] private SceneIdentifier fromScene;
    4. }
    And finally player spawn system:
    Code (CSharp):
    1. public class PlayerSpawnSystem
    2. {
    3.   void OnSceneLoaded()
    4.   {
    5.      var lastScene = AkaGameState.LastVisitedScene;
    6.      var spawnPoint = FindSpawnPointFromScene(lastScene);
    7.      player.transform.position = spawnPoint.transform.position
    8.   }
    9. }
    That's all pseudocode. I hope you get the main idea. Feel free to ask any question.
     
    Last edited: Oct 3, 2020
    cirocontinisio likes this.
  21. ThePineappleDev

    ThePineappleDev

    Joined:
    Mar 26, 2015
    Posts:
    8
    Yes that does indeed seem like a possible solution for determining where the player comes from, however I dont think it falls under the scope for this specific issue. I was just saying that the solution for the spawn system should be designed such that its relatively easy to expand it's functionality in the future.

    As a side note since scenes already have identifiers (I think) I dont think a new class would be needed for this?
     
  22. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    464
    Yes, you can use a scene name or path as an identifier. But using assets you have more freedom.

    For example, if there are several ways to enter scene1 from scene2, then using scene path we can't do anything about it. But using SO assets allows as to make a really flexible system.

    And now I understand that it's better to identify doors between scenes rather than scenes themselves.
     
    ThePineappleDev likes this.
  23. ThePineappleDev

    ThePineappleDev

    Joined:
    Mar 26, 2015
    Posts:
    8
    Multiple entrances does make sense, I hadn't considered that aspect, considering them doorways also helps for the understanding I think.

    I do still think that it's not necessary to implement that part yet, as they are encouraging us to stick to the basic requirements of the cards.
     
  24. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    464
    I've modified code from my previous message. It's not functionally working. It's just to show the concept.
    Code (CSharp):
    1. public class DoorIdentifier : ScriptableObject {}
    2.  
    3. public class Door : MonoBehaviour
    4. {
    5.     // scene where this door leads
    6.     [SerializeField] private string sceneName;
    7.     [SerializeField] private DoorIdentifier doorIdentifier;
    8.  
    9.     // Trigger used just for the example
    10.     void OnTriggerEnter(Collider other)
    11.     {
    12.         if (other is Player) // pseudocode
    13.         {
    14.             GoInto();
    15.         }
    16.     }
    17.  
    18.     public void GoInto()
    19.     {
    20.         PlayerSpawnSystem.LastUsedDoor = doorIdentifier;
    21.         SceneManager.LoadScene(sceneName);
    22.     }
    23. }
    24.  
    25. public class PlayerSpawnSystem
    26. {
    27.     public DoorIdentifier LastUsedDoor {get; set;}
    28.  
    29.     void OnSceneLoaded()
    30.     {
    31.         var spawnPoint = FindSpawnPointFromDoor(LastUsedDoor);
    32.         player.transform.position = spawnPoint.transform.position
    33.     }
    34. }
    35.  
    36. // Represents point in the scene, where player will be placed after scene is loaded
    37. public class PlayerSpawnPoint : MonoBehaviour
    38. {
    39.     [SerializeField] private DoorIdentifier fromDoor;
    40.  
    41.     public DoorIdentifier FromDoor => fromDoor;
    42. }
    In my opinion, it perfectly meets the card's requirements.

    Also, working such way allows us to have player prefab in each scene in edit mode and not to spawn it at runtime. The benefits are that you can modify your player instance for each scene specifically (for debugging or gameplay purposes)
     
    Last edited: Oct 3, 2020
  25. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    884
    Hey all, I finally found time to analyze all PRs and merge one. Apologies if the card wasn't 100% clear. I will add more details on next iterations, this was only v1 of this system and I feel like we have another 10 v's to cover.
    In fact, let's keep this thread open.

    @jordyvandertang and @MUGIK were really spot on though. Let me create the new cards for new tasks first.
     
  26. javimtib92

    javimtib92

    Joined:
    Aug 15, 2020
    Posts:
    7
    @cirocontinisio I added a very small change to the editor tool that @jamercer (I'm not able to tag this user) did to be able to select spawn location on click in the scene editor.

    Basically if you wanted to select a spawn location during play (which didn't make sense but I tried just for testing) the gizmos that displayed where the spawn location was going to be placed was updating it's position fast, along with the mouse move, but instead if you did the same when not in play mode the gizmos was laggy. I just added a call to HandleUtility.Repaint when there's a mouse move event in order to update the gizmos position in the scene.

    PR is here https://github.com/UnityTechnologies/open-project-1/pull/152
     
  27. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    884
    Saw it! Thanks Javier, I'll merge it soon.
     
  28. NicknEmart

    NicknEmart

    Joined:
    Sep 9, 2019
    Posts:
    46
    Can I ask someone here how do I spawn a player in a different location than (0, 0, 0)? I tried moving the "Location 01" (child of SpawnSystem) around but it doesn't do anything. I've also tried to mode the "EditModeDummy" but still nothing happens. What am I missing?
     
  29. Wodopo

    Wodopo

    Joined:
    Feb 19, 2013
    Posts:
    4
    Hi,
    Does it show any messages on the console?
    The SpawnSystem should look something like the image bellow. If it doesn't you can drag and drop the Player Prefab and right click on the Spawn System (Script) label and select Attempt Auto-Fill to fill the rest.

    upload_2020-11-13_14-6-17.png
     
  30. NicknEmart

    NicknEmart

    Joined:
    Sep 9, 2019
    Posts:
    46
    It looks exactly like this. I'm not even in a new scene, I was just testing the scenes that already existed, and I didn't add/remove anything
     
  31. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    884
    Ah that's weird, I'll take a look at it later (or if somebody wants to...)

    The EditModeDummy is just an empty object for Cinemachine, to have a target to be able to set it up without having the player in the scene.
    It doesn't affect the spawning in any way.
     
  32. Wodopo

    Wodopo

    Joined:
    Feb 19, 2013
    Posts:
    4
    Sorry for the late reply.

    My fork wasn't up to date, so I wasn't able to replicate the bug described before.

    Found that disabling the PigChef prefab's Animator causes it to spawn normaly again. Leaving it ON and turning ON Apply Root Motion also works, but cases the state machine to go haywire.

    Not sure if a bug or just work in progress, as I barely had time to keep up with the development.

    Edit: Leaving the Animator ON and Apply Root Motion OFF and going around the Animator Controller unchecking "Write Defaults" solves the problems.

    Edit 2: Further testing shows that it only works when I edit the Idle and Walking at runtime, changing Motion -> Root Motion Node: Root/Hips. But it's not persistent after reloading.

    Edit 3: Ignoring 'Edit 2' and just changing in the inspector the Culling Mode during Play Mode gets the desired behaviour. Again, not persistent.

    Edit 4: This is the last one, hopefully. A gif's resolution was too crapy, so here is an unlisted youtube video:
     
    Last edited: Nov 16, 2020
    cirocontinisio likes this.
  33. cirocontinisio

    cirocontinisio

    Unity Technologies

    Joined:
    Jun 20, 2016
    Posts:
    884