Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lua inject]How to accomplish an async on a game thread #692

Open
AnnznnA opened this issue Oct 23, 2024 · 9 comments
Open

[Lua inject]How to accomplish an async on a game thread #692

AnnznnA opened this issue Oct 23, 2024 · 9 comments
Labels
question Further information is requested

Comments

@AnnznnA
Copy link

AnnznnA commented Oct 23, 2024

I don't know how to lay out the lua environment for ue4ss and the coroutines don't seem to work
lua_pcall returned LUA_ERRRUN => attempt to yield across a C-call boundary
And ExecuteAsync is difficult to get the return value, and it is not safe to execute on the main thread of the game

@igromanru
Copy link
Contributor

Isn't it obvious why you can't call ExecuteAsync in ExecuteInGameThread?
You can do it only the other way around.
Also provide code if you want help...

@AnnznnA
Copy link
Author

AnnznnA commented Oct 24, 2024

Isn't it obvious why you can't call ExecuteAsync in ExecuteInGameThread? You can do it only the other way around. Also provide code if you want help...

local dsth = function()
    
end

function Test()
    local res = nil
    res = dsth(param)
    return res
end

function TestAsync()
    local res = nil
    ExecuteAsync(function()
        res = dsth(param)
    end)
    return res
end

The first piece of code give me a valid return value,However, this will cause some frame drops because it is not asynchronous
The second one is asynchronous, but I can't get the right result with him

A mechanism is needed to achieve something similar
await/async or coroutines

@igromanru
Copy link
Contributor

You obviously don't understand how variable scope and asynchronous programming works.
And I'm not planning to spoon feed you. Learn the basics first.
Then you will understand why TestAsync() returns nil.
There is no await/async in Lua and ExecuteAsync is a simply self made function that executes the code in a separate thread.
Besides, I doubt that your approach is right for what you want to do, to get information from the code that was executed in ExecuteAsync, you have to work with callbacks or global variables.

@AnnznnA
Copy link
Author

AnnznnA commented Oct 24, 2024

You obviously don't understand how variable scope and asynchronous programming works. And I'm not planning to spoon feed you. Learn the basics first. Then you will understand why TestAsync() returns nil. There is no await/async in Lua and ExecuteAsync is a simply self made function that executes the code in a separate thread. Besides, I doubt that your approach is right for what you want to do, to get information from the code that was executed in ExecuteAsync, you have to work with callbacks or global variables.

I know why the return value is wrong.

I'm just giving you an example, there are many ways to implement asynchronously, including coroutines and multithreading, etc., at present, UE4SS's Lua can't get blocking results at the end of asynchronous, on the other hand, unlua can complete asynchronous through coroutines, as I said in the title

During the development of game mods, a lot of content is not suitable for running outside of the main thread of the game, and it only provides ExecuteAsync in the documentation

@AnnznnA
Copy link
Author

AnnznnA commented Oct 24, 2024

you have to work with callbacks or global variables.

So, do you suggest I use code like this? Instead of allowing the next statement to wait for the result of the asynchronous execution, the code continuously determines whether a global variable has been modified to determine whether the asynchronous execution is complete

You also mean suggesting that I use it for tail processing of callback functions

@AnnznnA
Copy link
Author

AnnznnA commented Oct 24, 2024

I don't know how to lay out the lua environment for ue4ss and the coroutines don't seem to work lua_pcall returned LUA_ERRRUN => attempt to yield across a C-call boundary And ExecuteAsync is difficult to get the return value, and it is not safe to execute on the main thread of the game

I have a complex plan, I can complete them in the blueprint, then dump them during game runtime, and implement them by calling my own blueprint function through Lua,but this is quite complicated, and I don't like him

@igromanru
Copy link
Contributor

igromanru commented Oct 24, 2024

I don't know how to lay out the lua environment for ue4ss and the coroutines don't seem to work lua_pcall returned LUA_ERRRUN => attempt to yield across a C-call boundary And ExecuteAsync is difficult to get the return value, and it is not safe to execute on the main thread of the game

I have a complex plan, I can complete them in the blueprint, then dump them during game runtime, and implement them by calling my own blueprint function through Lua,but this is quite complicated, and I don't like him

I still have no idea what exactly you try to accomplish, but to come back to your original question of the issue:
How to accomplish an async on a game thread?
You can't do it in UE4SS Lua.
ExecuteInGameThread is already an async call. All it does, is waiting for the next possible game Tick and execute the code there.
It happens through a hooked game function that gets called each frame, ExecuteInGameThread puts the callback into a thread safe query that gets processed each frame in the hook. You can't execute async code there and expect it to be in the context of the game thread.

@AnnznnA
Copy link
Author

AnnznnA commented Oct 24, 2024

I don't know how to lay out the lua environment for ue4ss and the coroutines don't seem to work lua_pcall returned LUA_ERRRUN => attempt to yield across a C-call boundary And ExecuteAsync is difficult to get the return value, and it is not safe to execute on the main thread of the game

I have a complex plan, I can complete them in the blueprint, then dump them during game runtime, and implement them by calling my own blueprint function through Lua,but this is quite complicated, and I don't like him

I still have no idea what exactly you try to accomplish, but to come back to your original question of the issue: How to accomplish an async on a game thread? You can't do it in UE4SS Lua. ExecuteInGameThread is already an async call. All it does, is waiting for the next possible game Tick and execute the code there. It happens through a hooked game function that gets called each frame, ExecuteInGameThread puts the callback into a thread safe query that gets processed each frame in the hook. You can't execute async code there and expect it to be in the content of the game thread.

Thanks for the reply, here I have a question, I executed a LoadAsset function, in the hook callback and ExecuteInGameThread why in the former it can be executed normally, how did he do it, the latter gets stuck, I can understand that there is not enough tick allocation

@AnnznnA
Copy link
Author

AnnznnA commented Oct 24, 2024

I don't know how to lay out the lua environment for ue4ss and the coroutines don't seem to work lua_pcall returned LUA_ERRRUN => attempt to yield across a C-call boundary And ExecuteAsync is difficult to get the return value, and it is not safe to execute on the main thread of the game

I have a complex plan, I can complete them in the blueprint, then dump them during game runtime, and implement them by calling my own blueprint function through Lua,but this is quite complicated, and I don't like him

I still have no idea what exactly you try to accomplish, but to come back to your original question of the issue: How to accomplish an async on a game thread? You can't do it in UE4SS Lua. ExecuteInGameThread is already an async call. All it does, is waiting for the next possible game Tick and execute the code there. It happens through a hooked game function that gets called each frame, ExecuteInGameThread puts the callback into a thread safe query that gets processed each frame in the hook. You can't execute async code there and expect it to be in the context of the game thread.

I don't know how to lay out the lua environment for ue4ss and the coroutines don't seem to work lua_pcall returned LUA_ERRRUN => attempt to yield across a C-call boundary And ExecuteAsync is difficult to get the return value, and it is not safe to execute on the main thread of the game

I have a complex plan, I can complete them in the blueprint, then dump them during game runtime, and implement them by calling my own blueprint function through Lua,but this is quite complicated, and I don't like him

I still have no idea what exactly you try to accomplish, but to come back to your original question of the issue: How to accomplish an async on a game thread? You can't do it in UE4SS Lua. ExecuteInGameThread is already an async call. All it does, is waiting for the next possible game Tick and execute the code there. It happens through a hooked game function that gets called each frame, ExecuteInGameThread puts the callback into a thread safe query that gets processed each frame in the hook. You can't execute async code there and expect it to be in the content of the game thread.

Thanks for the reply, here I have a question, I executed a LoadAsset function, in the hook callback and ExecuteInGameThread why in the former it can be executed normally, how did he do it, the latter gets stuck, I can understand that there is not enough tick allocation

Of course, I didn't do it to load assets, I just did some explosion calculations, but he seems to have affected the playback of the game's background music and caused the game to stutter

@UE4SS UE4SS added the question Further information is requested label Dec 12, 2024
@UE4SS-RE UE4SS-RE deleted a comment from Meep111 Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants