Search Unity

Lights Out code strategy advice

Discussion in 'Scripting' started by farayman3, Apr 26, 2015.

  1. farayman3

    farayman3

    Joined:
    Apr 11, 2015
    Posts:
    5
    Hello,

    I've been working on my first game (a copy of the "Lights Out" game) for the past few weeks and designed the whole thing without looking for strategies. I thought this would be the best way to start learning the language. I've finished the game now but would like to review what I've done and how I could have done certain aspects better.
    I will start by describing the script:
    • I have a GameManager script that controls my different scripts and methods.
    • It starts by creating a list containing Vector3 values in order to create a grid.
    • Next it instantiates background tiles on each coordinate in the list followed by the instantiation of the GreenLights which are also sprites and are given their x and y coordinate as a name so I can later distinguish them.
    Then for the click input and the logic:
    • A OnMouseDown function returns the name of the GameObject and splits the characters (the x and y value) and sends them as int to the next function.
    • The "ClickGrid" function takes the two integers and gives a grid around them using a code like this:
    Code (CSharp):
    1.         for (i = x-1; i <= x+1; i++){
    2.             for (ii = y-1; ii <= y+1; ii++){
    3.                 if (i>=0 && i<=BoardManager.columns-1 && ii>=0 && ii<=BoardManager.rows-1){
    4.                     if (GameObject.Find ("Wave"+string.Concat (i,ii)) != null){
    • For every tile it brings up it checks the SpriteRenderer component and changes its sprite appropriately.
    • As a win detection I am using System.Linq:
    Code (CSharp):
    1. public void WinDetection(){
    2.     gameObjectArray = GameObject.FindGameObjectsWithTag ("RedLight");
    3.    
    4.     if (gameObjectArray.All (go => go.GetComponent<SpriteRenderer> ().sprite == greenLight)) {
    5.         Debug.Log ("You win");
    6.         Application.LoadLevel (0);
    7.     }
    8. }
    Thats it. I would like to hear if I was going in the right direction. I am pretty sure there will be a better way to keep track of all my instantiated objects and would love to hear them. Also I've been told before that checking all the sprites as a win detection is very "Gimmicky" and I agree but couldn't think of a better way (it certainly sometimes does not detect a win situation and sometimes does when it shouldn't, especially in between click actions).

    Thanks in advance if you are taking the time to think about this!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Your win check seems expensive, checking every light each time. An alternative might be to keep track of the number of lights of each colour and update it every time it changes.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I would not rely on GameObject.Find and GameObject.FindGameObjectsWithTag like that. Instead, since your game manager is creating these things anyway, let it simply keep references to them all in a 2D array.

    Then you can very easily iterate over this array as needed to change or check state, etc.

    Your use of "i" and "ii" for your row/column loop variables gives me the heebie-jeebies. Please consider using the more standard "i" and "j" (or get really wild and crazy, and use "row" and "col") instead.

    And yes, I don't think the game manager should set or check the particular sprite image your various objects are using directly. This will cause you grief when, for example, you decide to add some fancy animation, so the current sprite changes over time.

    Instead, have a Light (or LightState or whatever makes sense to you) component on each of those things, with a public computed property for the state (color). The setter for this should update the sprite, and also keep track of what its current state is in a private property. The getter can just return the value of that property. That way, no matter what you decide to do as far as animation or sub-sprites or use of UI Image objects instead of sprites or whatever else, only this Light class needs to worry about it; the rest of the code doesn't care, and will always get a correctly reported state.

    These are all minor tweaks, offered in the spirit of free advice (worth what you paid for it). Great job on your first game!

    Best,
    - Joe