Skip to content

Commit

Permalink
Working on fixing script loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Sep 23, 2024
1 parent f3903bf commit 667f6c0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/editor/JulGameEditor/Components/ComponentInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ function show_script_editor(entity, newScriptText)
CImGui.SameLine()
if CImGui.Button("Create New Script")
create_new_script(text)
include.(filter(contains(r".jl$"), readdir(joinpath(BasePath, "scripts"); join=true)))
include(joinpath(JulGame.BasePath, "scripts", "$(text).jl"))
newScript = Base.invokelatest(eval, Symbol(text))
newScript = Base.invokelatest(newScript)
newScript.parent = entity
Expand All @@ -547,7 +547,7 @@ function show_script_editor(entity, newScriptText)

script = display_files(joinpath(JulGame.BasePath, "scripts"), "scripts", "Add Script")
if script != ""
include.(filter(contains(r".jl$"), readdir(joinpath(BasePath, "scripts"); join=true)))
include(joinpath(JulGame.BasePath, "scripts", "$(script).jl"))
newScript = Base.invokelatest(eval, Symbol(script))
newScript = Base.invokelatest(newScript)
newScript.parent = entity
Expand Down
26 changes: 16 additions & 10 deletions src/engine/SceneManagement/SceneBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,26 +196,32 @@ module SceneBuilderModule
@info string("Adding scripts to entities")
@info string("Path: ", path)
@info string("Entities: ", length(MAIN.scene.entities))
include.(filter(contains(r".jl$"), readdir(joinpath(path, "scripts"); join=true)))
include.(filter(contains(r".jl$"), readdir(joinpath(path, "scripts"); join=true)))

for entity in MAIN.scene.entities
scriptCounter = 1
for script in entity.scripts
newScript = nothing
try
# TODO: only call latest if in editor and in game mode
#include(joinpath(BasePath "scripts"))
newScript = Base.invokelatest(eval, Symbol(script.name))
newScript = Base.invokelatest(newScript)
for (key, value) in script.fields
println("Key: $key, Value: $value")
println(newScript)
ftype = fieldtype(typeof(newScript), Symbol(key))
if ftype == Float64
value = Float64(value)
elseif ftype == Int32
value = Int32(value)
end

Base.invokelatest(setfield!, newScript, key, value)
ftype = nothing
try
ftype = fieldtype(typeof(newScript), Symbol(key))
if ftype == Float64
value = Float64(value)
elseif ftype == Int32
value = Int32(value)
end

Base.invokelatest(setfield!, newScript, key, value)
catch e
@warn string(e)
end
#setfield!(newScript, key, value)
end
catch e
Expand Down
2 changes: 0 additions & 2 deletions src/engine/SceneManagement/SceneReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module SceneReaderModule
function deserialize_scene(filePath)
try
entitiesJson = read(filePath, String)

json = JSON3.read(entitiesJson)
entities = []
uiElements = []
Expand All @@ -34,7 +33,6 @@ module SceneReaderModule

for entity in json.Entities
components = []
scripts = []

for component in entity.components
push!(components, deserialize_component(component))
Expand Down
1 change: 1 addition & 0 deletions src/engine/SceneManagement/SceneWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ module SceneWriterModule
push!(scriptsDict, Dict("name" => scriptName, "fields" => fields))
end

println(scriptsDict)
return scriptsDict
end

Expand Down

0 comments on commit 667f6c0

Please sign in to comment.