Skip to content

Commit

Permalink
Fix script loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Feb 15, 2025
1 parent 8259eab commit e05f749
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/JulGame.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ module JulGame
using SimpleDirectMediaLayer
const SDL2 = SimpleDirectMediaLayer
MAIN = nothing
IS_WEB = false

IS_WEB::Bool = false
IS_EDITOR::Bool = false
IS_EDITOR_PLAY_MODE::Bool = false
IS_DEBUG::Bool = false
IS_PACKAGE_COMPILED::Bool = false

DELTA_TIME = 0.0
# TODO: Create a globals file

SCENE_CACHE::Dict = Dict{String, Any}()
IMAGE_CACHE::Dict = Dict{String, Any}()
FONT_CACHE::Dict = Dict{String, Any}()
Expand Down
5 changes: 5 additions & 0 deletions src/editor/JulGameEditor/Components/ComponentInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,16 @@ function show_script_editor(entity, newScriptText)

script = display_files(joinpath(JulGame.BasePath, "scripts"), "scripts", "Add Script")
if script != ""
@debug("Adding script: $(script) to: $(entity.name)")
Base.include(JulGame.ScriptModule, joinpath(JulGame.BasePath, "scripts", "$(script).jl"))
module_name = getfield(JulGame.ScriptModule, Symbol("$(script)Module"))
constructor = Base.invokelatest(getfield, module_name, Symbol(script))
newScript = Base.invokelatest(constructor)
newScript.parent = entity
# TODO: Get this working
# if JulGame.IS_EDITOR_PLAY_MODE
# JulGame.initialize(newScript)
# end
push!(entity.scripts, newScript)
end

Expand Down
2 changes: 2 additions & 0 deletions src/editor/JulGameEditor/Components/FileFinderMenu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ function display_files(base_path::String, file_type::String, title::String = "",
extension = ".$(split(file, ".")[end])"
if extension in extensions
if CImGui.MenuItem(file)
println("selected: $(file)")
value = "$(joinpath(base_path, file))"
if file_type == "scripts"
value = split(file, ".")[1]
end
break
end
end
end
Expand Down
35 changes: 22 additions & 13 deletions src/editor/JulGameEditor/Editor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ module Editor

scrolling = Ref(ImVec2(0.0, 0.0))
zoom_level = Ref(1.0)
playMode = false

animation_window_dict = Ref(Dict())
animator_preview_dict = Ref(Dict())
Expand Down Expand Up @@ -174,7 +173,7 @@ module Editor
end

try
if !playMode && currentSelectedProjectPath[] != "" && unsafe_string(SDL2.SDL_GetWindowTitle(window)) != "$(windowTitle) - $(currentSelectedProjectPath[])"
if !JulGame.IS_EDITOR_PLAY_MODE && currentSelectedProjectPath[] != "" && unsafe_string(SDL2.SDL_GetWindowTitle(window)) != "$(windowTitle) - $(currentSelectedProjectPath[])"
newWindowTitle = "$(windowTitle) - $(currentSelectedProjectPath[])"
SDL2.SDL_SetWindowTitle(window, newWindowTitle)
end
Expand Down Expand Up @@ -247,10 +246,10 @@ module Editor
try
prevSceneWindowSize = sceneWindowSize

wasPlaying = playMode
wasPlaying = JulGame.IS_EDITOR_PLAY_MODE
if show_modal(confirmation_modal)
playMode = !playMode
if playMode
JulGame.IS_EDITOR_PLAY_MODE = !JulGame.IS_EDITOR_PLAY_MODE
if JulGame.IS_EDITOR_PLAY_MODE
startTime[] = SDL2.SDL_GetTicks()

# Animate the text in the window title
Expand All @@ -259,11 +258,11 @@ module Editor
end

sceneWindowSize = show_scene_window(currentSceneMain, sceneTexture, scrolling, zoom_level, duplicationMode, camera)
if playMode != wasPlaying && currentSceneMain !== nothing
if playMode
if JulGame.IS_EDITOR_PLAY_MODE != wasPlaying && currentSceneMain !== nothing
if JulGame.IS_EDITOR_PLAY_MODE
JulGame.MainLoopModule.start_game_in_editor(currentSceneMain, currentSelectedProjectPath[])
currentSceneMain.scene.camera = gameCamera
elseif !playMode
elseif !JulGame.IS_EDITOR_PLAY_MODE
JulGame.MainLoopModule.stop_game_in_editor(currentSceneMain)
JulGame.change_scene(String(currentSceneName))
end
Expand Down Expand Up @@ -606,18 +605,30 @@ module Editor
if length(filesToReload[]) > 0
for file in filesToReload[]
classname = split(file, ".")[begin]
Base.include(JulGame.ScriptModule, joinpath(JulGame.BasePath, "scripts", file))
try
Base.include(JulGame.ScriptModule, joinpath(JulGame.BasePath, "scripts", file))
catch e
@error "Error reloading file: $(file)"
continue
end
for entity in currentSceneMain.scene.entities
i = 1
for script in entity.scripts
script_name = split("$(typeof(script))", ".")[end]
if script_name == classname
try
println("reloading script: $(script_name)")
@debug("reloading script: $(script_name)")
module_name = getfield(JulGame.ScriptModule, Symbol("$(classname)Module"))
constructor = Base.invokelatest(getfield, module_name, Symbol(script_name))

new_script = Base.invokelatest(constructor)
entity.scripts[i] = new_script
entity.scripts[i].parent = entity

# TODO: Get this working
# if JulGame.IS_EDITOR_PLAY_MODE
# JulGame.initialize(new_script)
# end

# Copy all fields from old_script to the new script
for fieldname in fieldnames(typeof(entity.scripts[i]))
Expand All @@ -632,8 +643,6 @@ module Editor
end
end

entity.scripts[i] = new_script
entity.scripts[i].parent = entity
println("script reloaded successfully")
catch e
@error "Error reloading script: $(script_name): $(first(string(e), 1000))"
Expand All @@ -655,8 +664,8 @@ module Editor
backup_file_name = backup_file_name = "$(replace(currentSceneName, ".json" => ""))-backup-$(replace(Dates.format(Dates.now(), "yyyy-mm-ddTHH:MM:SS"), ":" => "-")).json"
@debug string("Backup file name: ", backup_file_name)
SceneWriterModule.serialize_entities(currentSceneMain.scene.entities, currentSceneMain.scene.uiElements, gameCamera, currentSelectedProjectPath[], backup_file_name)
@error "Error in renderloop!" exception=e
Base.show_backtrace(stderr, catch_backtrace())
@warn "Error in renderloop!" exception=e
finally
#TODO: fix these: ImGui_ImplSDLRenderer2_Shutdown();
# ImGui_ImplSDL2_Shutdown();
Expand Down
6 changes: 5 additions & 1 deletion src/engine/SceneManagement/SceneBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ module SceneBuilderModule
@debug string("Path: ", path)
@debug string("Entities: ", length(MAIN.scene.entities))
if !JulGame.IS_PACKAGE_COMPILED
foreach(file -> Base.include(JulGame.ScriptModule, file), filter(contains(r".jl$"), readdir(joinpath(path, "scripts"); join=true)))
foreach(file -> try
Base.include(JulGame.ScriptModule, file)
catch e
println("Error including $file: ", e)
end, filter(contains(r".jl$"), readdir(joinpath(path, "scripts"); join=true)))
end

for entity in MAIN.scene.entities
Expand Down

0 comments on commit e05f749

Please sign in to comment.