Search Unity

About scripting levels

Discussion in 'General Discussion' started by luigi7, Jan 2, 2020.

  1. luigi7

    luigi7

    Joined:
    Jun 30, 2015
    Posts:
    99
    Hello,
    I am developing my first game and I am tring to figure out if there are patterns or practices to script a level.
    I have some things that will happen (spawn enemies, pause, cinematics....) from the start to the end of the level.
    At the moment I have a JSON containing the script that the game loads and use to produce the level progress, it works but the code is a huge if sequence hard to maintain.
    Does anyone have some experience about?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Probably. But is impossible to say, just few line of text, how things can improve in your case.

    You either need post some snippets of your scripts, or describe workflow in more details.

    Just to be aware, there isn't single 'best' approach. There is many.
     
    luigi7 and Joe-Censored like this.
  3. luigi7

    luigi7

    Joined:
    Jun 30, 2015
    Posts:
    99
    Yes, I agree but I look for an approach, not code.
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,139
    Approaches all depend on what you're doing and what you're specifically trying to accomplish, hence

    and

     
    luigi7 and Joe-Censored like this.
  5. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    Well I was using scripts on GameObject that were enabled / activated by timers or game events.
     
    luigi7 likes this.
  6. luigi7

    luigi7

    Joined:
    Jun 30, 2015
    Posts:
    99
    @Murgilod it would be useless to paste code here, I can code by myself but I am not sure mine it's a good way to proceed.
    Writing about workflow, I can describe better what I'm going to build:
    A level is a timed sequence of elements like spawns, pauses for free space ingame, and cinematics.
    At the moment as the scene is loaded, a level manager loads a json containing the sequence of elements pointed before and runs the coroutine that simply does what it' a written in json.
    As i said, the coroutine it's difficult to read because of the length and the amount of nested if.
    I am looking for a better approach for this kind of problem, that's it.
     
  7. luigi7

    luigi7

    Joined:
    Jun 30, 2015
    Posts:
    99
    Ok, but is the sequence scripted somewhere?
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    Your approach sounds reasonable but it should be possible without long code and nested ifs. Most obvious would be to break portions out into separate functions.
     
    akaabc and luigi7 like this.
  9. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    We have not used any script in a sens of data in a text-file.
    If we needed a sequence based on timer so one action last for x seconds and next for y than you can create a component that have array of times and objects to enable at particular point in time.
    It usually was a relative counter, where time starts when object is enabled. That way you will be able to chain some actions.

    It was an older version of unity but we have also used animation to call functions (special system for this was made) at particular time. It was useful for in game cinematics.

    You could try it with cinemachine.
     
    luigi7 likes this.
  10. luigi7

    luigi7

    Joined:
    Jun 30, 2015
    Posts:
    99
    Sounds smart:)
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    I like this idea. You could make a serializable struct of a
    Code (csharp):
    1. Component myEventComponent;
    2. float delayTime; //delay time until the next event
    Then you can make an array of these and drag 'em all in the editor.

    Then your coroutine could just be
    Code (csharp):
    1.  
    2. for (i=0;i<eventArray.Length;i++)
    3. {
    4.       eventArray[i].myEventConponent.enabled =true;
    5.       yield return new WaitForSeconds( eventArray[i].delayTime);
    6. }
    7.  
    And you're done!
     
    luigi7 likes this.
  12. luigi7

    luigi7

    Joined:
    Jun 30, 2015
    Posts:
    99
    And every component should be a thing happening in the level, it's a great idea, but I'd need something else maybe because I fear the serialization of those components.
    I need something to load at runtime, not hardcoded into a GO.
     
  13. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    What do you mean by "fear the serialization"? Do you mean that you don't know how to do it, or that you think assigning a lot of things through the inspector is going to be cause some sort of problem?
     
  14. luigi7

    luigi7

    Joined:
    Jun 30, 2015
    Posts:
    99
    @kdgalla I tried only a few times, not that great experience but I did it, then I used an external asset to implement save/load: i found out it's not always straight forward to serialize the references of GO/prefab instances or custom classes. Maybe i'm overrating the problem however.
     
  15. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,634
    With this method, I don't think you would need to save any of these objects at run time. Your sequence is already part of the scene, so you'd just load the scene. You'd just need to save what scene was loaded and the value of i (the array index that's next to be done). Then you can easily resume the sequence where you left off.
     
    luigi7 likes this.