Given the following GDScript, finish the NeighborMap keys, such that it appends each…
- User
Given the following GDScript, finish the NeighborMap keys, such that it appends each neighboring vector on the X,Y axis, if the MapGrid contains the grid point as called: @tool extends EditorScript
func Main(Self): #Collect Data var MapGrid = Self.get_meta('Map_Data').duplicate(true) var NeighborMap = Self.get_meta('Neighbor_Map').duplicate(true) var MapSize = Self.get_meta('Map_Size')
#Build Array var MGKeys = MapGrid.keys() for X in range(0, MapSize): for Y in range(0, MapSize): if Vector3i(X,Y,0) not in MGKeys: MapGrid[Vector3i(X,Y,0)] = 0 #Build Neighbor Map MGKeys = MapGrid.keys() for Key in MapGrid.keys(): if Key not in NeighborMap.keys(): NeighborMap[Key] = [] if Vector3i(Key[0]-1,Key[1],Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]-1,Key[1],Key[2])) if Vector3i(Key[0]+1,Key[1],Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]+1,Key[1],Key[2])) if Vector3i(Key[0],Key[1]-1,Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0],Key[1]-1,Key[2])) Assistant
Completed NeighborMap Code
To complete the NeighborMap, you need to check for neighboring vectors on both the X and Y axes. The given code is almost complete, but it's missing the check for the positive Y neighbor. Here's the completed code:
@tool extends EditorScript func Main(Self): # Collect Data var MapGrid = Self.get_meta('Map_Data').duplicate(true) var NeighborMap = Self.get_meta('Neighbor_Map').duplicate(true) var MapSize = Self.get_meta('Map_Size') # Build Array var MGKeys = MapGrid.keys() for X in range(0, MapSize): for Y in range(0, MapSize): if Vector3i(X,Y,0) not in MGKeys: MapGrid[Vector3i(X,Y,0)] = 0 # Build Neighbor Map MGKeys = MapGrid.keys() for Key in MapGrid.keys(): if Key not in NeighborMap.keys(): NeighborMap[Key] = [] if Vector3i(Key[0]-1,Key[1],Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]-1,Key[1],Key[2])) if Vector3i(Key[0]+1,Key[1],Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]+1,Key[1],Key[2])) if Vector3i(Key[0],Key[1]-1,Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0],Key[1]-1,Key[2])) if Vector3i(Key[0],Key[1]+1,Key[2]) in MGKeys: # Check for positive Y neighbor NeighborMap[Key].append(Vector3i(Key[0],Key[1]+1,Key[2]))Explanation
The completed code checks for neighboring vectors on both the X and Y axes by verifying the existence of the following keys in
MapGrid:Vector3i(Key[0]-1, Key[1], Key[2])(negative X neighbor)Vector3i(Key[0]+1, Key[1], Key[2])(positive X neighbor)Vector3i(Key[0], Key[1]-1, Key[2])(negative Y neighbor)Vector3i(Key[0], Key[1]+1, Key[2])(positive Y neighbor)
If a neighboring key exists in
MapGrid, it is appended to the corresponding list inNeighborMap.- User
This is four directional, I need an 8 directional map. Try again, this time including the diagonals.
Assistant
Completed 8-Directional NeighborMap Code
To create an 8-directional NeighborMap, you need to check for neighboring vectors on both the X and Y axes, as well as the diagonals. Here's the completed code:
@tool extends EditorScript func Main(Self): # Collect Data var MapGrid = Self.get_meta('Map_Data').duplicate(true) var NeighborMap = Self.get_meta('Neighbor_Map').duplicate(true) var MapSize = Self.get_meta('Map_Size') # Build Array var MGKeys = MapGrid.keys() for X in range(0, MapSize): for Y in range(0, MapSize): if Vector3i(X,Y,0) not in MGKeys: MapGrid[Vector3i(X,Y,0)] = 0 # Build Neighbor Map MGKeys = MapGrid.keys() for Key in MapGrid.keys(): if Key not in NeighborMap.keys(): NeighborMap[Key] = [] if Vector3i(Key[0]-1,Key[1],Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]-1,Key[1],Key[2])) if Vector3i(Key[0]+1,Key[1],Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]+1,Key[1],Key[2])) if Vector3i(Key[0],Key[1]-1,Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0],Key[1]-1,Key[2])) if Vector3i(Key[0],Key[1]+1,Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0],Key[1]+1,Key[2])) if Vector3i(Key[0]-1,Key[1]-1,Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]-1,Key[1]-1,Key[2])) if Vector3i(Key[0]+1,Key[1]-1,Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]+1,Key[1]-1,Key[2])) if Vector3i(Key[0]-1,Key[1]+1,Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]-1,Key[1]+1,Key[2])) if Vector3i(Key[0]+1,Key[1]+1,Key[2]) in MGKeys: NeighborMap[Key].append(Vector3i(Key[0]+1,Key[1]+1,Key[2]))Explanation
The completed code checks for neighboring vectors on both the X and Y axes, as well as the diagonals, by verifying the existence of the following keys in
MapGrid:Vector3i(Key[0]-1, Key[1], Key[2])(negative X neighbor)Vector3i(Key[0]+1, Key[1], Key[2])(positive X neighbor)Vector3i(Key[0], Key[1]-1, Key[2])(negative Y neighbor)Vector3i(Key[0], Key[1]+1, Key[2])(positive Y neighbor)Vector3i(Key[0]-1, Key[1]-1, Key[2])(top-left diagonal neighbor)Vector3i(Key[0]+1, Key[1]-1, Key[2])(top-right diagonal neighbor)Vector3i(Key[0]-1, Key[1]+1, Key[2])(bottom-left diagonal neighbor)Vector3i(Key[0]+1, Key[1]+1, Key[2])(bottom-right diagonal neighbor)
If a neighboring key exists in
MapGrid, it is appended to the corresponding list inNeighborMap.Alternative Implementation
To make the code more concise, you can use a loop to iterate over the possible neighbor offsets:
for x in range(-1, 2): for y in range(-1, 2): if x == 0 and y == 0: continue var neighbor = Vector3i(Key[0] + x, Key[1] + y, Key[2]) if neighbor in MGKeys: NeighborMap[Key].append(neighbor)This code achieves the same result as the original, but is more concise and easier to maintain.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.