WeaselGames / godot_luaAPI

Godot LuaAPI
https://luaapi.weaselgames.info
Other
361 stars 27 forks source link

Running a constantly updated function in lua #154

Closed JekSun97 closed 10 months ago

JekSun97 commented 11 months ago

I use library for godot 3.5+

I need to make the Update function in Lua, something like _process in a Godot, so that it is called all the time and at the same time the game itself does not freeze. What options can you offer? My code looks something like this:

LuaObject.do_string(" function Update()

update...

end ")

Trey2k commented 11 months ago

The easiest way to do an update loop would probably be to have a function like your Update function, check if it exists and if it does call it each interval.

So,

extends Node

var lua = Lua.new()

func _ready() -> void:
    lua.do_string("""
    updates=0
    function Update(delta)
        updates = updates + 1
        print("Update #"..updates)
        print("Delta: "..delta)
    end
    """)

func _process(delta: float) -> void:
    if lua.function_exists("Update"):
        lua.call_function("Update", [delta])
Trey2k commented 10 months ago

Closing this for now. if you still have any questions, feel free to join our Discord, reopen this issue or a new one.