Skip to content

Commit

Permalink
Fixing script loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyjor committed Sep 22, 2024
1 parent 0816ea0 commit 775540c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 159 deletions.
37 changes: 4 additions & 33 deletions src/editor/JulGameEditor/Components/ComponentInputs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,6 @@ function show_script_editor(entity, newScriptText)
CImGui.Button("Create New Script") && (push!(entity.scripts, scriptObj(String(text), [])); create_new_script(text);)

script = display_files(joinpath(JulGame.BasePath, "scripts"), "scripts", "Add Script")
if script != ""
#push!(entity.scripts, scriptObj(script, []))
end

for i = eachindex(entity.scripts)
if CImGui.TreeNode("$(i): $(split("$(typeof(entity.scripts[i]))", ".")[end])")
Expand All @@ -553,37 +550,11 @@ function show_script_editor(entity, newScriptText)

if isdefined(entity.scripts[i], Symbol(field))
display_script_field_input(entity.scripts[i], field)
CImGui.Text("$(field): $(getfield(entity.scripts[i], field))")
else
init_undefined_field(entity.scripts[i], field)
end
end
# CImGui.Text(entity.scripts[i].name)

# entity.scripts[i] = scriptObj(entity.scripts[i].name, entity.scripts[i].parameters)
# if CImGui.TreeNode("Script $(i) parameters")
# params = entity.scripts[i].parameters
# CImGui.Button("Add New Script Parameter") && (push!(params, ""); entity.scripts[i] = scriptObj(entity.scripts[i].name, params); break;)

# for j = eachindex(entity.scripts[i].parameters)
# buf = "$(entity.scripts[i].parameters[j])"*"\0"^(64)
# CImGui.Button("Delete $(j)") && (deleteat!(params, j); entity.scripts[i] = scriptObj(currentTextInTextBox, params); break;)
# CImGui.InputText("Parameter $(j)", buf, length(buf))
# currentTextInTextBox = ""
# for characterIndex = eachindex(buf)
# if Int32(buf[characterIndex]) == 0
# if characterIndex != 1
# currentTextInTextBox = String(SubString(buf, 1, characterIndex-1))
# end
# break
# end
# end
# params[j] = currentTextInTextBox
# entity.scripts[i] = scriptObj(entity.scripts[i].name, params)

# end
# CImGui.TreePop()
# end

CImGui.TreePop()
end
end
Expand Down Expand Up @@ -611,7 +582,7 @@ function display_script_field_input(script, field)
x = Cfloat(x)
@c CImGui.InputFloat("$(field)", &x, 1)
setfield!(script, field, ftype(x))
elseif ftype <: Int
elseif ftype <: Int64 || ftype <: Int32 || ftype <: Int16 || ftype <: Int8
x = ftype(getfield(script, field))
@c CImGui.InputInt("$(field)", &x, 1)
setfield!(script, field, x)
Expand All @@ -633,6 +604,6 @@ function init_undefined_field(script, field)
end
end

function scriptObj(name::String, parameters::Array)
() -> (name; parameters)
function scriptObj(name::String, fields::Array)
() -> (name; fields)
end
128 changes: 19 additions & 109 deletions src/engine/SceneManagement/SceneBuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,54 +124,7 @@ module SceneBuilderModule

MAIN.scene.rigidbodies = InternalRigidbody[]
MAIN.scene.colliders = InternalCollider[]
for entity in MAIN.scene.entities
if entity.rigidbody != C_NULL
push!(MAIN.scene.rigidbodies, entity.rigidbody)
end
if entity.collider != C_NULL
push!(MAIN.scene.colliders, entity.collider)
end

if !JulGame.IS_EDITOR
scriptCounter = 1
for script in entity.scripts
params = []
for param in script.parameters
if lowercase(param) == "true"
param = true
elseif lowercase(param) == "false"
param = false
else
try
param = occursin(".", param) == true ? parse(Float64, param) : parse(Int32, param)
catch e
@error string(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)
end
end
push!(params, param)
end

newScript = C_NULL
try
newScript = eval(Symbol(script.name))(params...)
catch e
@error string(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)
end

entity.scripts[scriptCounter] = newScript
newScript.parent = entity
scriptCounter += 1
end
end
end

if JulGame.IS_EDITOR
add_scripts_to_entities(joinpath(BasePath))
end
add_scripts_to_entities(BasePath)

MAIN.assets = joinpath(BasePath, "assets")
JulGame.MainLoop.prepare_window_scripts_and_start_loop(size, isResizable, autoScaleZoom)
Expand All @@ -180,8 +133,8 @@ module SceneBuilderModule
function deserialize_and_build_scene(this::Scene)
scene = deserialize_scene(joinpath(BasePath, "scenes", this.scene))

# println("Changing scene to $this.scene")
# println("Entities in main scene: ", length(MAIN.scene.entities))
@info String("Changing scene to $this.scene")
@info String("Entities in main scene: ", length(MAIN.scene.entities))

for entity in scene[1]
push!(MAIN.scene.entities, entity)
Expand All @@ -208,49 +161,9 @@ module SceneBuilderModule
if entity.collider != C_NULL
push!(MAIN.scene.colliders, entity.collider)
end

if !JulGame.IS_EDITOR
scriptCounter = 1
for script in entity.scripts
params = []
for param in script.parameters
if lowercase(param) == "true"
param = true
elseif lowercase(param) == "false"
param = false
else
try
param = occursin(".", param) == true ? parse(Float64, param) : parse(Int32, param)
catch e
@error string(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)
end
end
push!(params, param)
end

newScript = C_NULL
try
newScript = eval(Symbol(script.name))(params...)
catch e
@error string(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)
end

if newScript != C_NULL
entity.scripts[scriptCounter] = newScript
newScript.parent = entity
end
scriptCounter += 1
end
end
end

if JulGame.IS_EDITOR
add_scripts_to_entities(joinpath(BasePath))
end
add_scripts_to_entities(BasePath)
end

"""
Expand Down Expand Up @@ -281,33 +194,30 @@ module SceneBuilderModule

function add_scripts_to_entities(path::String)
@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)))
for entity in MAIN.scene.entities
scriptCounter = 1
for script in entity.scripts
params = []
for param in script.parameters
if lowercase(param) == "true"
param = true
elseif lowercase(param) == "false"
param = false
else
try
param = occursin(".", param) == true ? parse(Float64, param) : parse(Int32, param)
catch e
@error string(e)
Base.show_backtrace(stdout, catch_backtrace())
rethrow(e)
end
end
push!(params, param)
end
newScript = nothing
try
# TODO: only call latest if in editor and in game mode
newScript = Base.invokelatest(eval, Symbol(script.name))
newScript = Base.invokelatest(newScript, params...)
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)
#setfield!(newScript, key, value)
end
catch e
@error string(e)
Base.show_backtrace(stdout, catch_backtrace())
Expand Down
17 changes: 4 additions & 13 deletions src/engine/SceneManagement/SceneReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module SceneReaderModule
using ...JulGame


function scriptObj(name::String, parameters::Array)
() -> (name; parameters)
function scriptObj(name::String, fields::Array)
() -> (name; fields)
end

export deserialize_scene
Expand All @@ -27,7 +27,7 @@ module SceneReaderModule
entitiesJson = read(filePath, String)

json = JSON3.read(entitiesJson)
entities =[]
entities = []
uiElements = []
res = []
childParentDict = Dict()
Expand All @@ -40,21 +40,12 @@ module SceneReaderModule
push!(components, deserialize_component(component))
end

for script in entity.scripts
scriptParameters = []
for scriptParameter in script.parameters
push!(scriptParameters, scriptParameter)
end
scriptObject = scriptObj(script.name, scriptParameters)
push!(scripts, scriptObject)
end

if haskey(entity, "parent") && entity.parent != ""
childParentDict[string(entity.id)] = entity.parent
end
newEntity = Entity(entity.name, string(entity.id))
newEntity.isActive = entity.isActive
newEntity.scripts = scripts
newEntity.scripts = entity.scripts

for component in components
if typeof(component) == Animator
Expand Down
34 changes: 30 additions & 4 deletions src/engine/SceneManagement/SceneWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ module SceneWriterModule
"""
function serialize_entities(entities::Array, uiElements::Array, camera, projectPath, sceneName)

println("Serializing entities")
@info String("Serializing entities")
entitiesDict = []
uiElementsDict = []

Expand Down Expand Up @@ -219,9 +218,36 @@ module SceneWriterModule
scriptsDict = []

for script in scripts
push!(scriptsDict, Dict("name" => script.name, "parameters" => script.parameters))
fields = Dict{String, Any}()
for i = eachindex(scripts)
scriptName = split("$(typeof(scripts[i]))", ".")[end]
for field in fieldnames(typeof(scripts[i]))
if field == :parent
continue
end
val = nothing
if isdefined(scripts[i], Symbol(field))
val = getfield(scripts[i], field)
else
val = set_undefined_field(scripts[i], field)
end
fields["$(field)"] = val
end
end
push!(scriptsDict, Dict("name" => script.name, "fields" => fields))
end

return scriptsDict
end
end

function set_undefined_field(script, field)
ftype = fieldtype(typeof(script), field)
if ftype == String
return ""
elseif ftype <: Number
return 0
elseif ftype == Bool
return false
end
end
end # module

0 comments on commit 775540c

Please sign in to comment.