Search Unity

Need help with design

Discussion in 'Scripting' started by warrenbrandt, Sep 9, 2020.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    ok so i have my UFO picking up NPC astronauts and they disappear when collected,fine......

    When collected the NPC calls his or hers destination, at the moment there are only two landing pads.

    So im trying to get the player to be able to teleport the NPC passenger when the UFO is landed at the correct pad.

    i have the NPC destination called in the AstronautPatrol script that controls the NPC movement and selects the destination.

    i have two pads that share a script called LandingPadControl which determines if the ufo is landed with both struts on the pad, its working.

    Then I have a separate script for each landing pad called
    CenturyCity
    ThePark

    the script just contains a string name for the landing pad to be compared against the destination.

    But from here i am just battling with the design.

    Do I do all the checking on the LandingPadControl?

    or should i create another empty object and attach a script called DisembarkCheck or something, and import all the variables there and call a method called CheckDisembark from another script.

    I'm sorry I just lack the experience as my project is growing, i think i have done well to get where i am in my project but right now it feels like im in a tangled mess. I actually broke things down and rearranged scripts tonight and it def seems to have made a difference.

    Its not so much the task i need done that i dont understand, its just design and what should handle what and go where. Maybe i have bitten off too much to swallow and should digress to some smaller projects before continuing on this....
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    WARREN! So glad you've come this far... think of how much you've learned!

    So I would NOT put this in the landing pad control. That should just note that yes, we are landed.

    Landing pad control might also be able to scan for the base identifier script, or you could also have a thing that scans below you looking for colliders that contain that identifier script.

    Like I said a week or so ago, the condition for a passenger to disembark is this:

    - am I really landed

    - am I landed on the pad I want

    To that end you could just have the passengers check in their update loop if those conditions are true, then hop out.

    Now if you have turned OFF the passenger completely in order to make him disappear, that would not work.

    In that case you could have a notion of a "hibernated passenger" that is created the moment the passenger gets aboard, and it does nothing but check for the "should I get out yet?" conditions, and when they are true it teleports the passenger to where you are, turns him visible again, then destroys itself. It is sort of like a bill of lading for the passenger in transit.
     
  3. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Thanks for that Kurt, was browsing your games on play...impressive, you have done a lot, i played this one where you are a hover car and can ramp off the mountains, interesting dynamic.

    "Now if you have turned OFF the passenger completely in order to make him disappear, that would not work." i have disabled the NPC using
    npcAstronaut.SetActive(false);

    so will this cause me a problem? when the conditions are met can i not just make it active again?

    but i feel a bit more confident now and will give it another go, thanks again for all your input!!! appreciate it

    Baie Dankie!!!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    It's not a problem, it's just that any script running on a non-active GameObject will NOT run.

    That's why you would need to just fire up a script and stick it on ANOTHER blank GameObject somewhere that has a reference to the player and know show to .SetActive(true) him when it's time for him to get off.

    Here, check this pattern out: here is how I like to make such scripts that live apart and aside from other parts of the game. This is one that checks one specific thing in the future (such as watch for landing), and then does something (such as teleports and reactivates the player) and goes away:

    https://pastebin.com/ZSAjCW2x

    You basically call the Create() factory method and pass in a function it calls to check, and a function it calls when true, and just forget about it.
     
  5. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    - am I landed on the pad I want?

    this is what i cant get right
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    But you do have the "I am down and stopped" check in right? Give me a sec, I'll wrestle up what I mean and post it.
     
  7. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    I have detection on the pad working well, but i dont have a timer setup. Im starting to think the pad detection needs to be done at the collider.

    aaaaaaaaaaaaaaaaaaaaaah man im so stumped. lol

    thanks kurt appreciate it.
     
    Last edited: Sep 11, 2020
  8. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    This is what im trying but its just not working

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LandingPadControl : MonoBehaviour
    6. {
    7.     private bool isCorrectPad = true;
    8.  
    9.     public AstronautFunctions astronautFunctions;
    10.     public CenturyCity centuryCity;
    11.     public ThePark thePark;
    12.  
    13.     private bool onCorrectPad = false;
    14.     private string astronautDestination;
    15.     private string isCenturyCity;
    16.     private string isThePark;
    17.  
    18.     public void Update()
    19.     {
    20.         astronautDestination = astronautFunctions.npcDestination;
    21.         isCenturyCity = centuryCity.landingPadIs;
    22.         isThePark = thePark.landingPadIs;
    23.     }
    24.    
    25.  
    26.     public LandedLogic landedLogic;
    27.  
    28.  
    29.     private bool isOnPad = false;
    30.  
    31.     public bool leftLegDown;
    32.     public bool rightLegDown;
    33.  
    34.     void OnTriggerEnter2D(Collider2D col)
    35.     {
    36.         CheckPad();
    37.         bool wasAlreadyLanded = rightLegDown && leftLegDown;
    38.  
    39.         if (col.gameObject.CompareTag("GearLeftDetect"))
    40.         {
    41.             leftLegDown = true;
    42.         }
    43.         else if (col.gameObject.CompareTag("GearRightDetect"))
    44.         {
    45.             rightLegDown = true;
    46.         }
    47.  
    48.         //Successful landing on a pad
    49.         if (!wasAlreadyLanded && rightLegDown && leftLegDown && isCorrectPad)
    50.         {
    51.             isOnPad = true;
    52.             landedLogic.SendMessage("LandedSuccessfuly");
    53.         }
    54.     }
    55.  
    56.     //No longer landed on a pad
    57.     void OnTriggerExit2D(Collider2D col)
    58.     {
    59.         bool bothLegsWereDown = rightLegDown && leftLegDown;
    60.         if (col.gameObject.CompareTag("GearLeft"))
    61.         {
    62.             leftLegDown = false;
    63.         }
    64.         else if (col.gameObject.CompareTag("GearRight"))
    65.         {
    66.             rightLegDown = false;
    67.         }
    68.  
    69.         if (bothLegsWereDown && (!rightLegDown || !leftLegDown))
    70.         {
    71.             isOnPad = false;
    72.         }
    73.     }
    74.  
    75.     public void CheckPad ()
    76.     {
    77.         if (astronautDestination == isCenturyCity)
    78.         {
    79.             Debug.Log("at cc");
    80.             onCorrectPad = true;
    81.         }
    82.  
    83.         if(astronautDestination == isThePark)
    84.         {
    85.             Debug.Log("at the park");
    86.             onCorrectPad = true;
    87.         }
    88.  
    89.     }
    90. }
    91.  
     
  9. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Been tied down with work this weekend, very frustrating...lol

    looking at your code...

    this line:

    var destination = hit.collider.GetComponentInParent<DCDestinationMarker>();

    Why do you use var?

    I watched a video on var

    in a nutshell the code on the right of the = sign tells the interpreter what the type is.
    so its not like int mynumber = 1;
    so you are not determining the type by explicitly declaring it, i get that.

    So why do you use var in this line? also i read very bad things about var.
    Is this true? or is it just a case well if you dont know when and why to use it...dont.

    sorry kurt i will be researching any words / code that i dont understand...my plan is to fully understand your project 100% before i do anything further on my own.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Coz I was lazy. :)

    Seriously, that's it. Lazy programmer... right here... me.

    Some people love
    var
    . Some people hate
    var
    .

    Some people actually have valid reasons to argue one way or the other.

    Some other people only imagine their reasons to be valid but when you dig deeper you find they are hallucinating.

    It's just like everything else in life. I've seen valid arguments for both ways.

    It won't change the code produced but it might change the readability for some people.

    Since it is reasonable that someday you'll encounter codebases that use either approach, you may wish to consider getting good at reading either type of approach, just like any other variant way of doing things. It will take time.
     
    warrenbrandt likes this.
  11. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    ok thanks
     
  12. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    var
    in C# is different from
    var
    in say, JavaScript, for instance.

    This is valid in loosely-typed languages like JavaScript:
    Code (JavaScript):
    1. var x = 10;
    2.  
    3. x = 5;
    4.  
    5. x = "Hello";
    6.  
    7. x = true;
    But in C#,
    var
    is still strongly-typed. It's just that the type is determined by whatever comes after the
    =
    sign. You cannot change the type afterwards.
    Code (CSharp):
    1. //x is declared as an int type. It can only remain as an int type.
    2. var x = 10;
    3.  
    4. //This is still valid.
    5. x = 5;
    6.  
    7. //This is not valid.
    8. x = "Hello";
    9.  
    10. //This is not valid either.
    11. x = true;
     
  13. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    good to know thanks vryken!!!