0

I am working on this game and I am getting the error. "fields must be fully assigned before control is returned to the caller". I just can't seem to figure this out, its driving me crazy. Here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;
using System.IO;

namespace Box2
{
struct Level

{
    private Block[,] grid;

    public int Height
    {
        get {
            return grid.GetLength(1);
        }
    }
    public int Width {
        get
        {
            return grid.GetLength(0);
        }
    }

    public enum BlockType
    {
        Solid,
        Empty,
        Platform,
        Ladder,
        LadderPlatform
    }

      struct Block
    {
        private BlockType type;
        private int posX, posY;
        private bool solid, platform, ladder;

        public BlockType Type
        {
            get { return type; }
        }
        public int X
        {
            get { return posX; }
        }
        public int Y
        {
            get { return posY; }
        }
        public bool IsSolid
        {
            get { return solid; }
        }
        public bool IsPlatform
        {
            get { return platform; }
        }
        public bool IsLadder
        {
            get { return ladder; }
        }

        private Block[,] grid;
        private string filename;
        public Point playerStartaPos;

        public Block(BlockType type, int x, int y)
        {
            this.posX = x;
            this.posY = y;
            this.type = type;
            this.ladder = false;
            this.solid = false;
            this.platform = false;

            switch (type)
            {
                case BlockType.Ladder:
                    ladder = true;
                    break;
                case BlockType.LadderPlatform:
                    ladder = true;
                    platform = true;
                    break;
                case BlockType.Solid:
                    solid = true;
                    break;
                case BlockType.Platform:
                    platform = true;
                    break;
                default:
                    break;
            }
        }


        public Block this[int x, int y]
        {
            get
            {
                return grid[x, y];
            }
            set
            {
                grid[x, y] = value;
            }
        }
        public string FileName
        {
            get { return filename; }
        }

        public void Level (int width, int height)
        {
            grid = new Block[width, height];
            filename = "none";
            playerStartaPos = new Point(1,1);

            for (int x=0; x < width; x++)
            {
                for (int y =0; y< height; y++)
                {
                    if (x == 0 || y ==0 || x ==width -1 || y == height -1)
                    {
                        grid[x, y] = new Block(BlockType.Solid, x, y);
                    }
                    else
                    {
                        grid[x, y] = new Box2.Level.Block(BlockType.Empty, x, y);
                    }
                }
            }
        }
    }
}

}

  • 1
    Welcome to Stack Overflow. Unfortunately, you've posted nearly 150 lines of code, which makes it much harder to pinpoint the error. Please reduce it to a [mcve] - you may well find that you find the problem while you're doing so. – Jon Skeet Nov 17 '16 at 19:36

2 Answers2

0

Its means that all the fields in you struct should be initialized in you constructor

you forgot to initialize these fields in your Block struct

        private Block[,] grid;
        private string filename;
        public Point playerStartaPos;

but I recommend you to use a class in this case When to use struct?

Community
  • 1
  • 1
Pepernoot
  • 3,409
  • 3
  • 21
  • 46
  • are you sure you initialized them in your constructor – Pepernoot Nov 17 '16 at 19:45
  • Yeah it should look like this then right? struct Block { private BlockType type; private int posX, posY; private bool solid, platform, ladder; private Block[,] grid; private string filename; public Point playerStartaPos; –  Nov 17 '16 at 19:47
  • Add this to you Block constructor `this.grid = null; this.filename = null; this.playerStartaPos = null;` – Pepernoot Nov 17 '16 at 19:50
  • The part that i am getting an error at is at the `public Block(BlockType type, int x, int y)` –  Nov 17 '16 at 20:23
0

I know this is a week old now but I think I should give my two cents.

The reason you're getting these errors is because when you create a struct in C# you must initialize all fields inside the constructor. I'm not 100% on how great your knowledge of C# is, so I've taken the liberty of fixing up your code for you. Note, however that things could be written a bit neater and you should strive for beautiful code structure.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;

namespace Box2
{
    struct Level
    {
        private Block[,] grid;

        public int Height
        {
            get
            {
                return grid.GetLength(1);
            }
        }
        public int Width
        {
            get
            {
                return grid.GetLength(0);
            }
        }

        public enum BlockType
        {
            Solid,
            Empty,
            Platform,
            Ladder,
            LadderPlatform
        }

        struct Block
        {
            private BlockType type;
            private int posX, posY;
            private bool solid, platform, ladder;

            public BlockType Type
            {
                get { return type; }
            }
            public int X
            {
                get { return posX; }
            }
            public int Y
            {
                get { return posY; }
            }
            public bool IsSolid
            {
                get { return solid; }
            }
            public bool IsPlatform
            {
                get { return platform; }
            }
            public bool IsLadder
            {
                get { return ladder; }
            }

            private Block[,] grid;
            private string filename;
            public Point playerStartaPos;

            public Block(BlockType type, int x, int y)
            {
                this.grid = null;
                this.filename = null;
                this.playerStartaPos = Point.Empty;

                this.posX = x;
                this.posY = y;
                this.type = type;
                this.ladder = false;
                this.solid = false;
                this.platform = false;

                switch (type)
                {
                    case BlockType.Ladder:
                        ladder = true;
                        break;
                    case BlockType.LadderPlatform:
                        ladder = true;
                        platform = true;
                        break;
                    case BlockType.Solid:
                        solid = true;
                        break;
                    case BlockType.Platform:
                        platform = true;
                        break;
                    default:
                        break;
                }
            }


            public Block this[int x, int y]
            {
                get
                {
                    return grid[x, y];
                }
                set
                {
                    grid[x, y] = value;
                }
            }
            public string FileName
            {
                get { return filename; }
            }

            public void Level(int width, int height)
            {
                grid = new Block[width, height];
                filename = "none";
                playerStartaPos = new Point(1, 1);

                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        if (x == 0 || y == 0 || x == width - 1 || y == height - 1)
                        {
                            grid[x, y] = new Block(BlockType.Solid, x, y);
                        }
                        else
                        {
                            grid[x, y] = new Box2.Level.Block(BlockType.Empty, x, y);
                        }
                    }
                }
            }
        }
    }
}

If this helped your solve your question please remember to accept my answer by clicking the tick off to the left of my answer.

Also, just one last thing. I noticed this code came from a platformer tutorial on Youtube and I would just like to say if you're keen on learning C# with OpenTK, these are by far the best references you'll ever get:

(this one is C++, but still is worth an extensive look!)

Learning OpenGL

This one is C#

Learning OpenTK

Lastly, there is a tutorial on Youtube called ' 3D Game Engine Tutorial' by BennyBox, he takes you on a journey creating your very own 3D Game Engine and even supplies you with the code for each tutorial. Be sure to check it out.

Have a great day!