[DiscordArchive] I am using this code inside a chat handler script. The POST call returns (or I can force it to fail)
[DiscordArchive] I am using this code inside a chat handler script. The POST call returns (or I can force it to fail)
Archived author: DaveY • Posted: 2024-02-16T03:27:35.766000+00:00
Original source
I am using this code inside a chat handler script. The POST call returns (or I can force it to fail) but it looks like the `player` object has gone out of scope.
```if msg == "test" then
player:SendBroadcastMessage("still in scope 1")
HttpRequest("POST", "http://localhost:9000/go", jsonString, "application/json", function(status, body, headers)
player:SendBroadcastMessage("still in scope 2")
print(body)
if(body == nil or body == '') then
player:SendBroadcastMessage("Failed")
else
player:SendBroadcastMessage("Success")
end
end)
end
```
Any thoughts?
Archived author: Foe • Posted: 2024-02-16T03:29:28.017000+00:00
Original source
Yes, it's a callback function, the player doesn't exist in that context
Archived author: DaveY • Posted: 2024-02-16T03:29:49.913000+00:00
Original source
OK, too used to js
Archived author: Foe • Posted: 2024-02-16T03:31:41.658000+00:00
Original source
```Lua
local function OnResponse(status, body, headers)
-- This is a different function, player is not a variable. When you use this inline, you are just lucky if the player object even exists..
player:SendBroadcastMessage("still in scope 2")
print(body)
if(body == nil or body == '') then
player:SendBroadcastMessage("Failed")
else
player:SendBroadcastMessage("Success")
end
end
local function OnChat(event, player)
if msg == "test" then
player:SendBroadcastMessage("still in scope 1")
HttpRequest("POST", "http://localhost:9000/go", jsonString, "application/json", OnResponse)
end
end```
Archived author: Foe • Posted: 2024-02-16T03:32:40.305000+00:00
Original source
That is inherently how this works. If the response function is triggered in a later update, ie. not this tick and synchronous, the player object doesn't exist because the parent function context doesn't exist anymore
Archived author: Foe • Posted: 2024-02-16T03:33:00.387000+00:00
Original source
Calling a userdata object inside an inline callback function is inherently dangerous and unintended and unstable behavior
Archived author: DaveY • Posted: 2024-02-16T03:34:46.906000+00:00
Original source
Makes sense
Archived author: DaveY • Posted: 2024-02-16T03:38:36.134000+00:00
Original source
`playerGuid = player:GetGUID()` followed up later by `newplayer = GetPlayerByGUID( playerGuid )` does the trick safely
Archived author: Foe • Posted: 2024-02-16T03:39:44.968000+00:00
Original source
Yep, that's the way to do it
Archived author: DaveY • Posted: 2024-02-16T03:40:10.376000+00:00
Original source
Thanks for responding Foe These initial threshings and support go a long way hehe