Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save children to scene to avoid subscene reloading #173

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions addons/road-generator/nodes/road_container.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export(float) var density:float = -1.0 setget _set_density
## Generate procedural road geometry
## If off, it indicates the developer will load in their own custom mesh + collision.
export(bool) var create_geo := true setget _set_create_geo
# If true, then all children are added in the editor to avoid re-building.
# TODO: or maybe name this "persist_children"? to help clarify it means they'll be saved in the scene
# Then, need extra logic to ensure regneerate is SKIPPED if this is true and it's a saved scene instnace
# but NOT skip generation steps if it's in a live scene.
export(bool) var visible_children := false setget _set_visible_children
# If create_geo is true, then whether to reduce geo mid transform.
export(bool) var use_lowpoly_preview:bool = false
## Whether to create approximated curves to fit along the forward, reverse, and center of the road.
Expand Down Expand Up @@ -152,6 +157,14 @@ func is_subscene() -> bool:
return filename and self != get_tree().edited_scene_root


func has_visible_children() -> bool:
return debug_scene_visible or visible_children


func get_owner() -> Node:
return self.owner if is_instance_valid(self.owner) else self


#gd4
#func _get_configuration_warnings() -> PackedStringArray:
func _get_configuration_warning() -> String:
Expand Down Expand Up @@ -300,6 +313,14 @@ func _set_create_geo(value: bool) -> void:
call_deferred("_dirty_rebuild_deferred")


func _set_visible_children(value: bool) -> void:
if value == visible_children:
return
visible_children = value
_dirty = true
call_deferred("_dirty_rebuild_deferred")


func _set_create_edge_curves(value: bool) -> void:
create_edge_curves = value
if create_edge_curves:
Expand Down Expand Up @@ -891,8 +912,8 @@ func _process_seg(pt1:RoadPoint, pt2:RoadPoint, low_poly:bool=false) -> Array:
# connected to two road points, it will only be placed as a parent of
# one of them
pt1.add_child(new_seg)
if debug_scene_visible:
new_seg.owner = self.owner
if has_visible_children():
new_seg.owner = self.get_owner()
new_seg.low_poly = low_poly
new_seg.start_point = pt1
new_seg.end_point = pt2
Expand Down
26 changes: 13 additions & 13 deletions addons/road-generator/nodes/road_segment.gd
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func _init(_container):

func _ready():
do_roadmesh_creation()
if container.debug_scene_visible and is_instance_valid(road_mesh):
road_mesh.owner = container.owner
if container.has_visible_children() and is_instance_valid(road_mesh):
road_mesh.owner = container.get_owner()


# Workaround for cyclic typing
Expand Down Expand Up @@ -102,8 +102,8 @@ func add_road_mesh() -> void:
road_mesh = MeshInstance.new()
add_child(road_mesh)
road_mesh.name = "road_mesh"
if container.debug_scene_visible and is_instance_valid(road_mesh):
road_mesh.owner = container.owner
if container.has_visible_children() and is_instance_valid(road_mesh):
road_mesh.owner = container.get_owner()


func remove_road_mesh():
Expand Down Expand Up @@ -331,8 +331,8 @@ func generate_lane_segments(_debug: bool = false) -> bool:
if not is_instance_valid(ln_child) or not ln_child is RoadLane:
ln_child = RoadLane.new()
_par.add_child(ln_child)
if container.debug_scene_visible:
ln_child.owner = container.owner
if container.has_visible_children():
ln_child.owner = container.get_owner()
ln_child.add_to_group(container.ai_lane_group)
ln_child.set_meta("_edit_lock_", true)
ln_child.auto_free_vehicles = container.auto_free_vehicles
Expand Down Expand Up @@ -649,7 +649,7 @@ func _update_curve():
_set_curve_point(curve, end_point, end_mag, _end_flip_mult)

# Show this primary curve in the scene hierarchy if the debug state set.
if container.debug_scene_visible:
if container.has_visible_children():
var found_path = false
var path_node: Path
for ch in self.get_children():
Expand All @@ -662,7 +662,7 @@ func _update_curve():
if not found_path:
path_node = Path.new()
self.add_child(path_node)
path_node.owner = container.owner
path_node.owner = container.get_owner()
path_node.name = "RoadSeg primary curve"
path_node.curve = curve

Expand Down Expand Up @@ -959,7 +959,7 @@ func _insert_geo_loop(
# Prepare attributes for add_vertex.
# Long edge towards origin, p1
#st.add_normal(Vector3(0, 1, 0))
quad(
_quad(
st,
[
Vector2(uv_l, uv_y_end),
Expand Down Expand Up @@ -1042,7 +1042,7 @@ func _insert_geo_loop(
uv_r = 0.0 * uv_width
# LEFT (between pos:_s and _m, and between uv:_l and _m)
# The flat part of the shoulder on both sides
quad(
_quad(
st,
[
Vector2(uv_m if dir == 1 else uv_l, uv_y_end),
Expand All @@ -1059,7 +1059,7 @@ func _insert_geo_loop(

# The gutter, lower part of the shoulder on both sides.
if dir == 1:
quad(
_quad(
st,
[
Vector2(uv_l, uv_y_end),
Expand All @@ -1074,7 +1074,7 @@ func _insert_geo_loop(
start_loop + start_basis * (pos_near_l + gutr_near.x) * dir + Vector3(0, gutr_near.y, 0),
])
else:
quad(
_quad(
st,
[
Vector2(uv_m, uv_y_end),
Expand All @@ -1093,7 +1093,7 @@ func _insert_geo_loop(
# Generate a quad with two triangles for a list of 4 points/uvs in a row.
# For convention, do cloclwise from top-left vert, where the diagonal
# will go from bottom left to top right.
static func quad(st:SurfaceTool, uvs:Array, pts:Array) -> void:
static func _quad(st:SurfaceTool, uvs:Array, pts:Array) -> void:
# Triangle 1.
#gd4
#st.set_uv(uvs[0]) # here and below
Expand Down