0

I am working on a tile map (of my own code). Each hexagon has an Dictionary that holds its individual data. I need to check to see if each tile meets some conditions...

if (hex["neighbors_index"]["North_index"] != null && hex["neighbors_index"]["North_index"] > 0 && hexagon_grid_data["tile_" + str(hex["neighbors_index"]["North_index"])]["framed"] == false):
#code
pass

This seems messy and hard to follow the way the "variable" repeats.

Is there a way that I can set up an "advanced" IF statement that will allow me to reference the variable only one time and then test it againts the conditions, or do I simply need to convert it to a Match test?

example of a solution I am fishing for...

if (hex["neighbors_index"]["North_index"] != null && > 0 && hexagon_grid_data["tile_" + str(hex["neighbors_index"]["North_index"])]["framed"] == false):
#code
pass

In this case, I entered the varaible of hex["neighbors_index"]["North_index"] and did the conditions as != null && > 0

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

I tried the short hand of...

if (hex["neighbors_index"]["North_index"] != null && > 0...)

I recieved an error stating...

res://Script(s)/HEXAGON_Polygon2D.gd:546 - Parse Error: Expected expression after "&&" operator.

Paul T.
  • 4,703
  • 11
  • 25
  • 29

1 Answers1

0

You could use plain old variables to improve readability:

var north_index = hex["neighbors_index"]["North_index"]
var framed = hexagon_grid_data[str("tile_", north_index)]["framed"]
if (north_index != null && north_index > 0 && not framed):
    pass

The above has two additional changes.

  1. The str() function accepts a variable number of arguments and returns the concatenated result. We can use this to slightly simplify the join of "tile_" and north index.

  2. boolean == false is equal to not boolean. Less characters and if not framed reads a bit better.

Do note that framed is accessed using north_index before a null check in this example code.


You can separate the expression onto multiple lines:

if (
        hex["neighbors_index"]["North_index"] != null &&
        hex["neighbors_index"]["North_index"] > 0 &&
        hexagon_grid_data[str("tile_", hex["neighbors_index"]["North_index"])]["framed"] == false
):
    pass

Or to be more extreme, you can create a sort of expression wrapper / optional object.

class ExprWrapper:
    var value
    func _init(v): value = v
    func eq(v):
        if value != v: value = null
        return self
    func neq(v):
        if value == v: value = null
        return self
    func gt(v):
        if value <= v: value = null
        return self
    func lt(v):
        if value >= v: value = null
        return self
    func falsy() -> bool:
        return value == null or value == false
    func truthy() -> bool:
        return value != null

func expr(v): return ExprWrapper.new(v)

if (
        expr(hex["neighbors_index"]["North_index"]).neq(null).gt(0).truthy() &&
        hexagon_grid_data[str("tile_", hex["neighbors_index"]["North_index"])]["framed"] == false
):
    pass

hola
  • 3,150
  • 13
  • 21
  • I do like the short hand, but since I only get a few hours a week to work on my video game as a hobby I like being verbose about my programming so I know, more directly, where things are sourcing from. For me personally, it's more efficient to say `hex["neighbors_index"]["North_index"]` so I know it's coming from the hex array and accessing its 'neighbors_index' sub array. Somtimes there is almost a 2 week gap before I can work on it again. The boolean is pretty cool, I was unaware that we could structure it that way. Plus 1 for introducing me to that! I appreciate it! – eric czimcharo Aug 18 '23 at 05:21
  • Fair enough. I added two alternatives. Good luck and happy programming! – hola Aug 18 '23 at 22:02