PDA

View Full Version : Issue with getTokensWithinDistance



bmos
April 27th, 2021, 21:57
When I run Token.getDistanceBetween on a client tied to a onMove function in a token, I get a different number then when I run it as host.
It seems on player computers the onMove function is occurring based on the original location rather than the new location.

EDIT: I should also add that I am using onMove to send OOB to run the command on the host so it could be a timing issue.
EDIT2: the title of this thread is wrong, it's about getDistanceBetween not getTokensWithinDistance

4.0.10
Windows 10

Moon Wizard
April 27th, 2021, 22:04
Can you spit out the info locally to see if it's because of the OOB?

JPG

bmos
April 28th, 2021, 00:56
Can you spit out the info locally to see if it's because of the OOB?

JPGIt's not the OOB situation.
This code still has the issue:

local onMove = nil;
local function auraOnMove(tokenMap)
--Debug.chat("in auraOnMove");
if onMove then
onMove(tokenMap);
end
checkRange(DB.findNode('combattracker.list.id-00003'), DB.findNode('combattracker.list.id-00001'));

--Debug.chat("finishing aura on move");
end

local updateAttributesFromToken = nil;
function auraUpdateAttributesFromToken(tokenMap)
--Debug.chat("in auraUpdateAttributesFromToken");
if updateAttributesFromToken then
updateAttributesFromToken(tokenMap);
end

onMove = tokenMap.onMove;
tokenMap.onMove = auraOnMove;
end

local function checkRange(nodeSource, nodeTarget)
local sourceToken = CombatManager.getTokenFromCT(nodeSource);
local targetToken = CombatManager.getTokenFromCT(nodeTarget);
if not sourceToken or not targetToken then
return false;
end;

Debug.chat(Token.getDistanceBetween(sourceToken, targetToken))
end

function onInit()
updateAttributesFromToken = TokenManager.updateAttributesFromToken;
TokenManager.updateAttributesFromToken = auraUpdateAttributesFromToken;
end

I have trimmed my extension down to a test extension and attached it and my campaign here so you can reproduce easily:

Moon Wizard
May 3rd, 2021, 18:16
I'm testing this on v4.0.10 and v4.1; and I'm seeing the same information being printed on both GM and player clients. I'm loading up the GM and player client; connecting as "george" character; then using arrow keys to move george token up and down one square on both GM and player.

I'm getting the same information on both player and client. I also added a Debug.chat with the token position to see if that was different, but they were the same too.

Maybe I'm following different steps than you are?

Thanks,
JPG

bmos
May 3rd, 2021, 22:44
I'm testing this on v4.0.10 and v4.1; and I'm seeing the same information being printed on both GM and player clients. I'm loading up the GM and player client; connecting as "george" character; then using arrow keys to move george token up and down one square on both GM and player.

I'm getting the same information on both player and client. I also added a Debug.chat with the token position to see if that was different, but they were the same too.

Maybe I'm following different steps than you are?

Thanks,
JPGThat's so strange... it's now working for me as well...
Sorry to waste your time. I'll post here again if it happens again and I can reproduce it consistently.

Moon Wizard
May 3rd, 2021, 23:59
That would be great. I wouldn't be surprised with some variability of event timing/positioning with networking; so let me know.

Regards,
JPG

bmos
May 11th, 2021, 12:54
It looks like I was wrong about the OOB issue.
It is because of that. I will recreate the test ext to demonstrate this soon.

occurs in at least 5e, 4e, pfrpg

bmos
May 11th, 2021, 15:52
Here is a test campaign and ext to demonstrate the issue I'm running into with OOB timing.
If it's working as intended, perhaps someone has a suggestion to get the same value for host and client?

Steps to reproduce:
1. open campaign with ext loaded
2. open second instance connected to localhost
3. claim a character and move that character's token using the client instance (token locking disabled)
4. observe that chat messages show distance based on original square (before movement) which doesn't match the targeting distance
5. move same player on host instance
6. observe that chat messages show distance based on destination square (after each half step) matching targeting distance

EDIT: occurs on both live and test branch (with latest updates as of 9:50pm EST May 11)

Moon Wizard
May 12th, 2021, 22:48
Script events are generally called before the network updates go out, so we don't double dip on network updates when data is changed. So, the problem that you're running into is that your OOB message is being sent prior to the token movement network update is sent. Also, the network messages are processed in order, which means that your OOB message is processed before the network update.

I did notice that the "onMove" event in FGC fires on network token movement as well as UI token movement; while FGU only fires on UI token movement. I'm looking into whether that is something we can enable without breaking anything else; but I'm not sure at this point.

Regards,
JPG

Moon Wizard
May 12th, 2021, 23:03
Also, I noticed that you are assigning a new handler every time that a token attribute is updated. You should only be adding handlers directly to a token in a Token.onAdd event; or just capturing Token.onMove directly. I'm not sure if your extension is adding potentially tens to hundreds of handler instances or not.

JPG

Moon Wizard
May 12th, 2021, 23:34
Here's a version I ended up testing with:



function onInit()
Token.onMove = onTokenMove;
end

function onTokenMove(tokenMap)
local token1 = CombatManager.getTokenFromCT(DB.findNode('combattr acker.list.id-00003'));
local token2 = CombatManager.getTokenFromCT(DB.findNode('combattr acker.list.id-00001'));

Debug.chat("ON MOVE 1", token1)
Debug.chat("ON MOVE 2", token2)
Debug.chat("ON MOVE 3", Token.getDistanceBetween(token1, token2))
end

Moon Wizard
May 13th, 2021, 06:09
I was able to slip this into the beta build (v4.1). When a token is moved by a remote client (either GM or player), the Token.onMove and tokeninstance.onMove events will fire (as in FGC).

If you base your stuff on something similar to my sample code in the post above along with this change; it should simplify your code.

Regards,
JPG

bmos
May 13th, 2021, 12:37
Thanks JPG!

SoxMax
May 13th, 2021, 14:11
This is awesome! Also the reason for assigning a new handler on token attribute updates is because we weren't aware that Token.onMove existed. Its not actually documented on Jira (https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644662/Token) or the old docs (https://www.fantasygrounds.com/refdoc/Token.xcp).

SilentRuin
May 13th, 2021, 15:05
Now just need something to tell us if its visible in LOS ;)