From 7cba4fdf7429e4f8b747bf31f9c3c6954824f716 Mon Sep 17 00:00:00 2001 From: Scott Wadden Date: Tue, 2 Feb 2021 22:15:37 -0400 Subject: [PATCH] Don't create a VM for objects with no code --- enupkg/bot.nim | 17 ++++++++++++++--- enupkg/builder.nim | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/enupkg/bot.nim b/enupkg/bot.nim index f8969dff..3f48c250 100644 --- a/enupkg/bot.nim +++ b/enupkg/bot.nim @@ -92,15 +92,26 @@ gdobj NimBot of KinematicBody: err e.msg trigger("script_error") + proc default_script: string = + join_path(config.lib_dir, "enu", "default_bot.nim") + + proc is_script_loadable(): bool = + if self.enu_script != "none" and file_exists(self.enu_script): + let + default_code = read_file(self.default_script).strip + current_code = read_file(self.enu_script).strip + + result = current_code != "" and current_code != default_code + proc load_script() = trace: - if self.enu_script == "none": - return self.callback = nil try: if self.engine.is_nil: self.engine = Engine() + if not self.is_script_loadable: + return if not self.paused and not self.engine.initialized: debug &"Loading {self.enu_script}" @@ -138,7 +149,7 @@ gdobj NimBot of KinematicBody: inc max_bot_index self.name = "Bot_" & $self.script_index self.set_script() - copy_file join_path(config.lib_dir, "enu", "default_bot.nim"), self.enu_script + copy_file self.default_script, self.enu_script method ready*() = trace: diff --git a/enupkg/builder.nim b/enupkg/builder.nim index 72ffdf6f..69bbc39f 100644 --- a/enupkg/builder.nim +++ b/enupkg/builder.nim @@ -65,6 +65,9 @@ gdobj Builder of Spatial: proc set_script() = self.enu_script = join_path(config.script_dir, &"grid_{self.script_index}.nim") + proc default_script: string = + join_path(config.lib_dir, "enu", "default_grid.nim") + proc setup*(translation: Vector3) = self.translation = translation self.original_translation = translation @@ -72,7 +75,15 @@ gdobj Builder of Spatial: inc max_grid_index self.name = "Builder_" & $self.script_index self.set_script() - copy_file join_path(config.lib_dir, "enu", "default_grid.nim"), self.enu_script + copy_file self.default_script, self.enu_script + + proc is_script_loadable(): bool = + if self.enu_script != "none" and file_exists(self.enu_script): + let + default_code = read_file(self.default_script).strip + current_code = read_file(self.enu_script).strip + + result = current_code != "" and current_code != default_code proc save_blocks*() = let data = if self.draw_mode == VoxelMode: @@ -356,15 +367,12 @@ gdobj Builder of Spatial: self.load_script() proc load_script() = - if self.enu_script == "none": - # can't use empty string because it gets set as nil, which is no longer valid nim. - # can probably be fixed in godot-nim - return - debug &"Loading {self.enu_script}. Paused {self.paused}" self.callback = nil self.blocks_remaining_this_frame = 0 try: if self.engine.is_nil: self.engine = Engine() + if not self.is_script_loadable: + return if not (self.paused or self.engine.initialized): with self.engine: load(self.enu_script, config.lib_dir)