Forums WoW Modding Support Archives Azerothcore Discord Archives [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)

[DiscordArchive] I am using this code inside a chat handler script. The POST call returns (or I can force it to fail)

Pages (2): 1 2 Next
rektbyfaith
Administrator
0
02-16-2024, 03:27 AM
#1
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?
rektbyfaith
02-16-2024, 03:27 AM #1

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?

rektbyfaith
Administrator
0
02-16-2024, 03:29 AM
#2
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
rektbyfaith
02-16-2024, 03:29 AM #2

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

rektbyfaith
Administrator
0
02-16-2024, 03:29 AM
#3
Archived author: DaveY • Posted: 2024-02-16T03:29:49.913000+00:00
Original source

OK, too used to js
rektbyfaith
02-16-2024, 03:29 AM #3

Archived author: DaveY • Posted: 2024-02-16T03:29:49.913000+00:00
Original source

OK, too used to js

rektbyfaith
Administrator
0
02-16-2024, 03:31 AM
#4
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```
rektbyfaith
02-16-2024, 03:31 AM #4

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```

rektbyfaith
Administrator
0
02-16-2024, 03:32 AM
#5
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
rektbyfaith
02-16-2024, 03:32 AM #5

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

rektbyfaith
Administrator
0
02-16-2024, 03:33 AM
#6
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
rektbyfaith
02-16-2024, 03:33 AM #6

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

rektbyfaith
Administrator
0
02-16-2024, 03:34 AM
#7
Archived author: DaveY • Posted: 2024-02-16T03:34:46.906000+00:00
Original source

Makes sense
rektbyfaith
02-16-2024, 03:34 AM #7

Archived author: DaveY • Posted: 2024-02-16T03:34:46.906000+00:00
Original source

Makes sense

rektbyfaith
Administrator
0
02-16-2024, 03:38 AM
#8
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
rektbyfaith
02-16-2024, 03:38 AM #8

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

rektbyfaith
Administrator
0
02-16-2024, 03:39 AM
#9
Archived author: Foe • Posted: 2024-02-16T03:39:44.968000+00:00
Original source

Yep, that's the way to do it
rektbyfaith
02-16-2024, 03:39 AM #9

Archived author: Foe • Posted: 2024-02-16T03:39:44.968000+00:00
Original source

Yep, that's the way to do it

rektbyfaith
Administrator
0
02-16-2024, 03:40 AM
#10
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
rektbyfaith
02-16-2024, 03:40 AM #10

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

Pages (2): 1 2 Next
Recently Browsing
 1 Guest(s)
Recently Browsing
 1 Guest(s)