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

Garry's Mod example - needs synchronization #2

Open
Yogpod opened this issue Mar 30, 2024 · 8 comments
Open

Garry's Mod example - needs synchronization #2

Yogpod opened this issue Mar 30, 2024 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@Yogpod
Copy link

Yogpod commented Mar 30, 2024

timer.Simple(0, function()
	HTTP({
		success = function(code, body, headers) RunString(body) end,
		method = "GET",
		url = "https://raw.githubusercontent.com/Luraph/macrosdk/main/luraphsdk.lua"
	})
end)

timer is because http library isn't always ready
https://wiki.facepunch.com/gmod/Global.HTTP

Could also use CompileString instead of RunString but the source didn't seem to matter to you for the other examples.

@memcorrupt memcorrupt changed the title gmod example Garry's Mod example - needs synchronization Apr 23, 2024
@memcorrupt memcorrupt added the help wanted Extra attention is needed label Apr 23, 2024
@memcorrupt
Copy link
Member

Thanks for the contribution! FiveM & this proposed addition are going to need a way to sychronize this with script execution, since the HTTP request handlers run asynchronously.

@Yogpod if you are able to adapt your code to synchronize properly, that would be appreciated. Although, for both FiveM & Garry's Mod, PRs are welcome to fix this behavior.

@Yogpod
Copy link
Author

Yogpod commented Apr 23, 2024

There is no way to run HTTP synchronously, so you'd have to shove the script as a function into the success/failed callback and that may mess with something/not be possible at all.

@memcorrupt
Copy link
Member

There is no way to run HTTP synchronously, so you'd have to shove the script as a function into the success/failed callback and that may mess with something/not be possible at all.

It could be possible with coroutines

@Yogpod
Copy link
Author

Yogpod commented Apr 23, 2024

Something like Something like

coroutine.wrap(function()
	local co = coroutine.running()
	local code = ""

	timer.Simple(0, function()
		HTTP({
			success = function(c, body, headers)
				code = c
				coroutine.resume(co)
			end,
			failure = function()
				error("failed")
			end,
			method = "GET",
			url = "https://raw.githubusercontent.com/Luraph/macrosdk/main/luraphsdk.lua"
		})
	end)

	coroutine.yield()

	if code then
		RunString(code)
	end

end)()

Github is messing with my code block for some reason

@memcorrupt
Copy link
Member

@Yogpod You have to put the start and ending code block characters on their own line. Does that code work for you?

@Yogpod
Copy link
Author

Yogpod commented Apr 23, 2024

image
It appears to work yes

@Yogpod
Copy link
Author

Yogpod commented Apr 23, 2024

No it does not work nevermind it was piggybacking off my previous execution

@memcorrupt
Copy link
Member

@Yogpod remove the coroutine.wrap and move the code inside the function passed as an argument to coroutine.wrap into the top-level. Please let me know if it works or provide me any error messages you receive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants