From 246d1952e273557e432f4fa2fc86b7c48aa79a0c Mon Sep 17 00:00:00 2001 From: Hop311 Date: Fri, 7 Apr 2023 23:43:26 +0100 Subject: Fixed weird hashmap lookup crash. --- game/src/GameSession/MapView.gd | 45 +++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/game/src/GameSession/MapView.gd b/game/src/GameSession/MapView.gd index faf90e8..4291191 100644 --- a/game/src/GameSession/MapView.gd +++ b/game/src/GameSession/MapView.gd @@ -44,6 +44,13 @@ var _map_mesh_dims : Vector2 var _mouse_pos_viewport : Vector2 = Vector2(0.5, 0.5) var _mouse_pos_map : Vector2 = Vector2(0.5, 0.5) +# ??? Strange Godot/GDExtension Bug ??? +# Upon first opening a clone of this repo with the Godot Editor, +# if MapSingleton.get_province_index_image is called before MapMesh +# is referenced in the script below, then the editor will crash due +# to a failed HashMap lookup. I'm not sure if this is a bug in the +# editor, GDExtension, my own extension, or a combination of them. +# This was an absolute pain to track down. --- hop311 func _ready(): if _camera == null: push_error("MapView's _camera variable hasn't been set!") @@ -51,24 +58,8 @@ func _ready(): if _map_mesh_instance == null: push_error("MapView's _map_mesh variable hasn't been set!") return - if not _map_mesh_instance.mesh is MapMesh: - push_error("Invalid map mesh class: ", _map_mesh_instance.mesh.get_class(), "(expected MapMesh)") - return - _map_mesh = _map_mesh_instance.mesh - - # Set map mesh size and get bounds - _map_image_size = Vector2(Vector2i(MapSingleton.get_width(), MapSingleton.get_height())) - _map_mesh.aspect_ratio = _map_image_size.x / _map_image_size.y - var map_mesh_aabb := _map_mesh.get_core_aabb() * _map_mesh_instance.transform - _map_mesh_corner = Vector2( - min(map_mesh_aabb.position.x, map_mesh_aabb.end.x), - min(map_mesh_aabb.position.z, map_mesh_aabb.end.z) - ) - _map_mesh_dims = abs(Vector2( - map_mesh_aabb.position.x - map_mesh_aabb.end.x, - map_mesh_aabb.position.z - map_mesh_aabb.end.z - )) + # Shader Material var map_material = _map_mesh_instance.get_active_material(0) if map_material == null: push_error("Map mesh is missing material!") @@ -77,6 +68,7 @@ func _ready(): push_error("Invalid map mesh material class: ", map_material.get_class()) return _map_shader_material = map_material + # Province index texture _map_province_index_image = MapSingleton.get_province_index_image() if _map_province_index_image == null: @@ -84,6 +76,7 @@ func _ready(): return var province_index_texture := ImageTexture.create_from_image(_map_province_index_image) _map_shader_material.set_shader_parameter(_shader_param_province_index, province_index_texture) + # Province colour texture var province_colour_image = MapSingleton.get_province_colour_image() if province_colour_image == null: @@ -92,6 +85,24 @@ func _ready(): var province_colour_texture := ImageTexture.create_from_image(province_colour_image) _map_shader_material.set_shader_parameter(_shader_param_province_colour, province_colour_texture) + if not _map_mesh_instance.mesh is MapMesh: + push_error("Invalid map mesh class: ", _map_mesh_instance.mesh.get_class(), "(expected MapMesh)") + return + _map_mesh = _map_mesh_instance.mesh + + # Set map mesh size and get bounds + _map_image_size = Vector2(Vector2i(MapSingleton.get_width(), MapSingleton.get_height())) + _map_mesh.aspect_ratio = _map_image_size.x / _map_image_size.y + var map_mesh_aabb := _map_mesh.get_core_aabb() * _map_mesh_instance.transform + _map_mesh_corner = Vector2( + min(map_mesh_aabb.position.x, map_mesh_aabb.end.x), + min(map_mesh_aabb.position.z, map_mesh_aabb.end.z) + ) + _map_mesh_dims = abs(Vector2( + map_mesh_aabb.position.x - map_mesh_aabb.end.x, + map_mesh_aabb.position.z - map_mesh_aabb.end.z + )) + func _unhandled_input(event : InputEvent): if event.is_action_pressed(_action_click): # Check if the mouse is outside of bounds -- cgit v1.2.3-56-ga3b1