Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by znSawyer · Aug 02, 2015 at 08:10 AM · c#listclasseslogicfor loop

Card/Tile Game, loops dont update element0 of playerHand properly

In Unity 2D: I have a Tile class, PlayerTileTray class, PlayerTileTrayManager class and a TileManager class.

TileManager creates a List and adds elements to the list, setting the Tile variables. TileManager has a Deal Method with an OnTileSent(Tile sentTile) event that sends the top tile as a parameter.

PlayerTileTrayManager listens to the event using the OnTileSent Method. PlayerTileTrayManager's job is to take that tileSent and place it in one of several _trays in a PlaceTileInFirstEmptySlotOfTrays method. I am currently using an array of trays called _tray[].

If an next element of the _tray array is null, I Instantiate a gameObject Prefab representing the tray and add a PlayerTileTray instance to the prefab as _tray[trayIndex].

The PlayerTileTray has an array of bools,slotFull[], to show whether a slot is full. In the PlayerTileTrayManager: I loop through _tray and then slots of that tray to call the UpdateTrayWithTile method of PlayerTileTray UpdateTrayWiltTile adds the tile to a List in _tray, then trys to update the _tray bool variables emptyTray, fullTray and slotFull[].

The main problem I am having I am currently using a nested for loop. I have tried do while loops. The first bool element slotFull[0] never gets set to true despite how I try to make it true. When the first tile is dealt, slotFull[0] stays false. When the second tile is dealt both slotFull[0] and slotFull[1] are true;

The results of everything else is achieved or will be if I can fix the first element of slotFull updating properly..

alt text TileManager.cs

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TileManager :MonoBehaviour
 {    
     //For testing
     private int _topTile = 0;
     private List<Tile> _fullStackTiles = new List<Tile>();
     private static int _stockTileCount = 16;
     private static int _numberOfTileSets = 4;
 
     public int tilesToDeal = 1;
     public static Dictionary <string,Tile> stockTiles = new Dictionary <string,Tile>();
     public static List<Tile> shuffledTiles = new List<Tile>() ;
     public static int maxTiles = _numberOfTileSets * _stockTileCount;
 
     / **Singleton Stuff and Delegate,Event and Publisher Method removed for Brevity */
       void Awake(){
         _instance = this;  //  <--Singleton
     }
     void Start(){
         AssignTiles ();
         createFullStackWithSetOf(_numberOfTileSets);
         shuffledTiles = _fullStackTiles;
 
         //Shuffle is located in MyExtensions
         shuffledTiles.Shuffle();
     }
     void AssignTiles(){/* Method Contents removed for Brevity */ }
 
     void createFullStackWithSetOf(int numSets){
         //Create a stack of tiles with _numOfTileSets of stockTiles
         foreach(KeyValuePair<string,Tile> t in stockTiles){
             for(int i = 0; i < numSets; i++)
                 _fullStackTiles.Add(t.Value);
         }
     }
     public void Deal(int numIilesToDeal){
         for(int i = 0; i < numIilesToDeal; i++){
             //TileSend method is near the beginning of this script in the Delegate #region
             //TileSend (shuffledTiles[_topTile]);
             OnTileSent(shuffledTiles[_topTile]);
             shuffledTiles.RemoveAt(_topTile);
 
             //create node for instantiated tile
             //insert tile into Node
         }
     }
 }

PlayerTileTrayManager

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerTileTrayManager : MonoBehaviour {
 
     private static int _maxNumberOfTrays = TileManager.maxTiles / PlayerTileTray.trayMax;
     private List<Tile> _playerTiles = new List<Tile>();
     private GameObject _tilePrefab;
     private PlayerTileTray[] _tray = new PlayerTileTray[8];
 
     public Tile tileFromDraw;
     public GameObject TrayPrefab;
     List <GameObject> _trayObject  = new List<GameObject>();
     
     void OnEnable(){
         TileManager.Instance.TileSent += OnTileSent;
     }
     void OnDisable(){
         TileManager.Instance.TileSent -= OnTileSent;
     }
 
     void OnTileSent(object source, Tile tileSent){
         _playerTiles.Add (tileSent);
         PlaceTileInFirstEmptySlotOfTrays (tileSent);
         
         GameObject go = PrefabMap.Instance.GetValueByKey(tileSent.tileName);
         Instantiate (go,Vector3.zero,Quaternion.identity);
 
         //move _tilePrefab to childLocation
     }
     void PlaceTileInFirstEmptySlotOfTrays(Tile placeTile)
     {
         for(int trayIndex = 0;trayIndex < _maxNumberOfTrays;trayIndex++){
             //for(int slotIndex = 0; slotIndex < PlayerTileTray.trayMax; slotIndex ++)
             int slotIndex = 0;
             do{
                 if(_tray[trayIndex] == null)
                 {
                     _trayObject.Add (Instantiate (TrayPrefab, Vector3.zero, Quaternion.identity) as GameObject);
                     _tray[trayIndex] = _trayObject[trayIndex].GetComponent<PlayerTileTray>();
                 }
                 if (!_tray[trayIndex].fullTray)
                 {
                     if(!_tray[trayIndex].fullSlot[slotIndex])
                     {
                         _tray[trayIndex].UpdateTrayWithTile(placeTile);
                         Debug.Log (placeTile.tileName + "was placed in the " + trayIndex + " trayIndex iteration and in the " + 
                                    slotIndex + " slotIndex iteration.");
                         return;
                     }
                 }
                 slotIndex++;
             }while(slotIndex < PlayerTileTray.trayMax);
         }
     }
     public void showHand(){/* Not relevant to the Question*/    }
     //CycleTrays
 }

PlayerTileTray.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class PlayerTileTray :MonoBehaviour{
 
     public static int trayMax = 8;
 
     public bool[] fullSlot;
     public bool fullTray;
     public bool emptyTray;
 
     public Tile tileFromTrayManager;
     public List<Tile> tilesInTray = new List<Tile>();
 
     public PlayerTileTray(){
         fullSlot = new bool[trayMax];
         fullTray = false;
         emptyTray = true;
     }
     void Start(){
         for(int i = 0; i < trayMax; i++)
             fullSlot[i] = false;
     }
     public void UpdateTrayWithTile(Tile tileToTray){
         tilesInTray.Add (tileToTray);
         for (int i = 0; i < tilesInTray.Count; i++)
             fullSlot[i] = true;
         if(tilesInTray.Count >= 0)
             emptyTray = false;
         if(tilesInTray.Count >= 8)
             fullTray = true;
     }
 }
 

 


lopptrouble.jpg (53.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by znSawyer · Aug 04, 2015 at 10:39 PM

In the PlayerTileTray, I initaized my Boolean array in Start(). I changed this to Awake and code worked as expected.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to make a for loop for a class in C# 1 Answer

Argument out of range when checking at List[0], C# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need my function to work with different lists of different values (classes) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges