Hi everyone, I’m from the gamedev sphere and have my eye on the Odin language. Decided to see if it’s possible to do what I’ve done before in other langs. While I more or less understand how manual memory management works, I don’t have enough practice with it: some high-level stuff like closures that grab fields etc I’m missing, but fully understand why it’s not here.
For example I like to create stacks of actions that are sequentially executed: handy when working on scripts like dialogs or cinematics.
In an ideal example I would try to allocate enough memory in advance, maybe not use pointers and pass everything through handles, but I want to know how correct the code below is:
ActionWait :: struct{
timer: f32
}
ActionEcho :: struct{
text: string
}
Action :: struct{
p: uintptr,
execute: proc(p: uintptr) -> bool
}
actions : [3]Action
actionsLen : int
wait :: proc(time: f32){
a:= new(ActionWait)
a.timer = time
actions[actionsLen].p = cast(uintptr)a
actions[actionsLen].execute = proc(p: uintptr) -> bool {
timer := cast(^ActionWait)p
timer.timer -= 1
fmt.println(timer.timer)
if timer.timer == 0{
free(timer)
actionsLen -=1
return true
}
else {
return false
}
}
actionsLen += 1
}
print :: proc(text: string){
a:= new(ActionEcho)
a.text = text
actions[actionsLen].p = cast(uintptr)a
actions[actionsLen].execute = proc(p: uintptr) -> bool {
echo := cast(^ActionEcho)p
fmt.println(echo.text)
free(echo)
actionsLen -=1
return true
}
actionsLen += 1
}
execute :: proc(){
actions[0].execute(actions[0].p)
}
main :: proc(){
wait(4)
print("hello!")
index := 0
for {
a := actions[index]
if a.execute(a.p){
index += 1
}
if index == 2 do break
}
}