Forum begins after the advertisement:


[Part 3] CodeMonkey’s Grid class for the Inventory System

Home Forums Video Game Tutorial Series Creating an Underwater Survival Game in Unity [Part 3] CodeMonkey’s Grid class for the Inventory System

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #11617
    Terence
    Keymaster

    As of 5 July 2023, CodeMonkey has updated their Grid script, so that it is now different from the one that we have used when we created the Part 3 video. Hence, if you download the latest version of their Grid script while following our tutorial, you will run into errors when trying to use it together with our script.

    If you are following our tutorial, please use the Grid script below, instead of the latest one on the CodeMonkey website:

    /* 
        ------------------- Code Monkey -------------------
    
        Thank you for downloading this package
        I hope you find it useful in your projects
        If you have any questions let me know
        Cheers!
    
                   unitycodemonkey.com
        --------------------------------------------------
     */
    
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    //using CodeMonkey.Utils;
    
    public class Grid {
    
        //event handler
        public event EventHandler OnGridObjectChanged;
        public class OnGridObjectChangedEventArgs : EventArgs {
            public int x;
            public int y;
        }
    
        private int width;
        private int height;
        private float cellSize;
        private Vector3 originPosition;
        public TGridObject[,] gridArray { get; private set; }
    
        //class constructor
        public Grid(int width, int height, float cellSize, Vector3 originPosition, Func, int, int, TGridObject> createGridObject) {
            this.width = width;
            this.height = height;
            this.cellSize = cellSize;
            this.originPosition = originPosition;
    
            //create grid array to store each grid item
            gridArray = new TGridObject[width, height];
    
            for (int x = 0; x < gridArray.GetLength(0); x++) {
                for (int y = 0; y < gridArray.GetLength(1); y++) {
                    //this function just creates a default grid object for each spot in the grid with the position labelled correctly
                    gridArray[x, y] = createGridObject(this, x, y);
                }
            }
    
            bool showDebug = false;
            //this draws out the grid so u can see it
            if (showDebug) {
                TextMesh[,] debugTextArray = new TextMesh[width, height];
    
                for (int x = 0; x < gridArray.GetLength(0); x++) {
                    for (int y = 0; y < gridArray.GetLength(1); y++) {
                        //debugTextArray[x, y] = UtilsClass.CreateWorldText(gridArray[x, y]?.ToString(), null, GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 30, Color.white, TextAnchor.MiddleCenter);
                        Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
                        Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);
                    }
                }
                Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
                Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
    
                OnGridObjectChanged += (object sender, OnGridObjectChangedEventArgs eventArgs) => {
                    //debugTextArray[eventArgs.x, eventArgs.y].text = gridArray[eventArgs.x, eventArgs.y]?.ToString();
                };
            }
        }
    
        //functions to return values
        public int GetWidth() {
            return width;
        }
    
        public int GetHeight() {
            return height;
        }
    
        public float GetCellSize() {
            return cellSize;
        }
    
        public Vector3 GetWorldPosition(int x, int y) {
            return new Vector3(x, y) * cellSize + originPosition;
        }
    
        //returns grid postion of a world space value
        public void GetXY(Vector3 worldPosition, out int x, out int y) {
            x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
            y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
        }
    
        //this sets a grid section to the class u created
        //grid based on grid postion
        public void SetGridObject(int x, int y, TGridObject value) {
            if (x >= 0 && y >= 0 && x < width && y < height) {
                gridArray[x, y] = value;
                TriggerGridObjectChanged(x, y);
            }
        }
    
        //this calls the event handler that the grid value changed
        public void TriggerGridObjectChanged(int x, int y) {
            OnGridObjectChanged?.Invoke(this, new OnGridObjectChangedEventArgs { x = x, y = y });
        }
    
        //set grid based on world position
        public void SetGridObject(Vector3 worldPosition, TGridObject value) {
            GetXY(worldPosition, out int x, out int y);
            SetGridObject(x, y, value);
        }
    
        //get the object in a certain grid position
        public TGridObject GetGridObject(int x, int y) {
            if (x >= 0 && y >= 0 && x < width && y < height) {
                return gridArray[x, y];
            } else {
                return default(TGridObject);
            }
        }
    
        //get grid object from world position
        public TGridObject GetGridObject(Vector3 worldPosition) {
            int x, y;
            GetXY(worldPosition, out x, out y);
            return GetGridObject(x, y);
        }
    
        //check if a grid position exists
        public bool IsValidGridPosition(Vector2Int gridPosition) {
            int x = gridPosition.x;
            int y = gridPosition.y;
    
            if (x >= 0 && y >= 0 && x < width && y < height) {
                return true;
            } else {
                return false;
            }
        }
    
    }
    #11618
    Terence
    Keymaster

    The reason why CodeMonkey’s new Grid doesn’t work now is because in the past, the Grid class was a generic class. This means that, to use and initialise it, you would have to specify a type to use it with:

    new Grid<SomeType>( args... );

    As of July 2023, the code was changed so that the Grid class is no longer a generic, and can now be initialised using:

    new Grid( args... );

    This messes up the entire structure of our code, which is why if you try to use the new Grid script, it will cause your code to be unable to compile.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: