Bizzare LevelGrid problem

Hello all!

I’m encountering a strange bug:

First, I got an ArgumentOutOfRangeException, indicating that I was outside the bounds of a list. After some debug.log’s here and there, the problem seemed to be that something was adding floors to the GridSystem, or so I thought. The problem is actually that the system insists on reading the height as floor. Just to be sure, I added couple lines of code that doesn’t throw the except, the problem still exists that the program has 30 floors (or however many I set it to be by changing the height).

Any thoughts on where to begin solving this?

— Update:

I ran through the lecture again on a copy of the project made just a few days ago. The problem seems to be related to the GridObjects (units and doors). I’ll keep looking.

— Update Update:

As I was fiddling with things, I fixed it on the old copy of the project, but don’t remember what it was, so I don’t know what to do to fix it on the current version… Yup…

In your screenshot I can see that there are 12 places that call GetGridSystem(int floor). It appears one (or more) of those are asking for the wrong floors. Check the references and make sure you’re not passing the wrong thing into the function

Bixarrio, we meet again!

The 12 references are from within the LevelGrid script itself and, though I saved my own copy of it in a separate file, this code is pulled from the lecture’s resources, so I doubt its the LevelGrid script itself. Though, I did find something interesting. There’s 1 spot where if I put a gridobject there, it doesn’t throw the error. Curious.

I haven’t done the hex and floors bit of this course, but I can see from the code that most of the floor values that get passed here are from the gridPosition which may be where the issue comes from. There’s also a GetFloor function here that could also be the source of the problem.
Can you post the GridPosition code and perhaps the GetFloor() function, then we can have a look

Sorry for the delayed response; I’d gone to bed shortly after posting that.

Grid Position script:

using System;

public struct GridPosition : IEquatable
{
public int X;
public int Z;
public int floor;

public GridPosition(int x, int gridLevel, int z)
{
    this.X = x;
    this.Z = z;
    this.floor = gridLevel;
}

public override bool Equals(object obj)
{
    return obj is GridPosition position &&
           X == position.X &&
           Z == position.Z &&
           floor == position.floor;
}

public bool Equals(GridPosition other)
{
    return this == other;
}

public override int GetHashCode()
{
    return HashCode.Combine(X, Z, floor);
}

// Return X/Z/floor coordinates
public override string ToString()
{
    return "x: " + X  + "; z: " + Z + "; floor: " + floor;
}

public static bool operator == (GridPosition a, GridPosition b) 
{
    return a.X == b.X && a.Z == b.Z && a.floor == b.floor;
}

public static bool operator != (GridPosition a, GridPosition b) 
{
    return !(a == b);
}

public static GridPosition operator +(GridPosition a, GridPosition b)
{
    return new GridPosition(a.X + b.X, a.Z + b.Z, a.floor + b.floor);
}

public static GridPosition operator -(GridPosition a, GridPosition b)
{
    return new GridPosition(a.X - b.X, a.Z - b.Z, a.floor - b.floor);
}

}

GetFloor Function:

public int GetFloor(Vector3 worldPosition)
{
    return Mathf.RoundToInt(worldPosition.y / FLOOR_HEIGHT);
}

I’ll keep looking for things on my end throughout the day, but I do appreciate you looking at it as well.

Welp… There it is: When I posted it as right when I caught it

public GridPosition(int x, int gridLevel,  int z, )
{
    this.x = x;
    this.z = z;
    this.floor = gridLevel;
}

This should be:
public GridPosition(int x, int z, int gridLevel)

In any case, thanks for the assist. I thought I’d check that already but apparently not.

1 Like

:+1:

In Unity, we’re used to z always being the 3rd parameter. When working with GridPositions, since the original course was assumed to have a single floor, x,z more accurately reflected the dimensions (as we’re working on the XZ plane.

We’re also used to the height being the Y parameter. It’s pretty easy to fall into the trap of putting the floor as the 2nd parameter.

Good job getting that sorted!

Hit the nail right on the head. As soon as I saw that, I knew exactly what was going on because, as you’d mentioned, we’re sort of conditioned to think X Y Z, not X Z Y. I’m half debating going through and updating the code to make this happen, but I think I’ll wait until the end of the course to refactor it just so that I can use Codemonkey’s stuff as a reference.

Sneaky mistake though. Lost some sleep over it, lol.

1 Like

I have many hobbies, one of which is playing around with lasers, CNC machines, and 3D printers…

My Laser has a 2 dimensional workspace, and is labelled XY with 0,0 in the NW corner.
My CNC has a 3 dimensional workspace, Z DOWN with XY 0,0 in the SW corner and Z0 at the TOP of the cube
My 3d printer has a 3 dimensional workspace, ZUp with XY 0,0 in the SW corner and Z0 at the Bottom of the cube…
At work, I use a pick and place machine (Surface Mounting electronic components) which works with 0,0 being wherever I say it is, X+ to the right of 0,0, Y+ to the rear of the machine

Unity2D is X+ right, Y+ up
Unity3D is X+right, Y+up, Z+away
Unreal is X+right, Y+Away, Z+up…

Coordinate systems can be fun sometimes. :slight_smile:

Funny you should say that; My mother’s husband works at a machine shop that cuts metal using a laser and I’ve seen the software they use for it (it’s a basic CAD program from what I recall, though, it’s been many many years since I’ve seen them so who knows if they’ve updated it). Not a lot of standardization because everyone wants to be special (that’s more of a joke, I realize that sometimes there are practical reasons for it, but I’m pretty sure that many of them re-invent the wheel just because they want to).

Blender is X+ right, Y+ this way, Z+ up… I have one hobby… game dev.

2 Likes

I hope to be at that level of knowledge one day

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms