Skip to content

Commit

Permalink
Don't create a VM for objects with no code
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrw committed Feb 3, 2021
1 parent 7623768 commit 7cba4fd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
17 changes: 14 additions & 3 deletions enupkg/bot.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down Expand Up @@ -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:
Expand Down
20 changes: 14 additions & 6 deletions enupkg/builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,25 @@ 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
self.script_index = max_grid_index
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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7cba4fd

Please sign in to comment.