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

Allow to send entities as parameters #16

Open
mrcomac opened this issue Jun 25, 2023 · 3 comments
Open

Allow to send entities as parameters #16

mrcomac opened this issue Jun 25, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@mrcomac
Copy link

mrcomac commented Jun 25, 2023

Hi,

I'm trying to apply an affect as an user to a token player doesn't have control.

in my module file I have the code below

let socket;
Hooks.once("socketlib.ready", () => {
	socket = socketlib.registerModule("swade-animated");
	socket.register("applyDoc", applyDoc);
});

this is the applyDoc function

async function applyDoc(target,effectDoc) {
    console.log("APPLY DOC");
    return;
    let previousEffect = target.token.actor.effects.filter(i => (i.name.toLowerCase().includes(effectDoc.name.toLowerCase().replace(/\s\(.*\)/, "")) ));
    if(previousEffect.length > 0) {
        target.token.actor.deleteEmbeddedDocuments('ActiveEffect', [Array.from(previousEffect)[0].id]);
    }
    target.token.actor.createEmbeddedDocuments('ActiveEffect', [effectDoc]);
}

and I call the function as below
The target is the token entity, and the effectDoc is the effect item.

socket.executeAsGM("applyDoc", target, effectDoc );

When I assume the GM role and call the function without calling socket.executeAsGM it works. When as GM i call the function using the socket.executeAsGM works. However, when i added the socket.executeAsGM, and try to run as player (with another browser open as GM) it doesn't work.

In the GM console i get the error

foundry.js:753 TypeError: Error thrown in hooked function 'onDiceSoNiceRollStart' for hook 'diceSoNiceRollStart'. Cannot read properties of undefined (reading 'system')
[Detected 2 packages: system:swade, dice-so-nice]
    at Object.onDiceSoNiceRollStart [as fn] (SwadeIntegrationHooks.ts:80:50)
    at #call (foundry.js:730:20)
    at Hooks.callAll (foundry.js:687:17)
    at Dice3D.showForRoll (Dice3D.js:645:15)
    at recursShowForRoll (Dice3D.js:597:22)
    at Dice3D.renderRolls (Dice3D.js:606:13)
    at Object.fn (main.js:249:17)
    at #call (foundry.js:730:20)
    at Hooks.callAll (foundry.js:687:17)
    at ClientDatabaseBackend.callback (foundry.js:13538:13)
    at foundry.js:13510:43
    at Array.map (<anonymous>)
    at #handleCreateDocuments (foundry.js:13510:33) 

In the player console I get the error:

Uncaught (in promise) InternalError: too much recursion
[No packages detected]
    gt is-binary.js:20
    mt is-binary.js:36
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45
    mt is-binary.js:45

@OhhLoz
Copy link

OhhLoz commented Jun 25, 2023

I came to write a similar error, Ive found a solution however wondering if there is a more efficient way. I believe its because once referenced objects such as Actors, Tokens, Items etc are passed into socket functions they are passed as values so no longer reference the original object.

I circumvented this by instead of passing the token itself into the socket function I pass the token._id and inside the function run game.actors.get(id) or canvas.tokens.get(id) which will get the original object you wanted to pass by reference. I imagine theres a better way with less function calls, such as global variables but that creates other issues.

@manuelVo
Copy link
Owner

With the current socketlib API, only values can be passed around that can be converted to JSON. Trying to pass a token fails, because tokens have circular references, which lead to a stack overflow when trying to convert them to JSON. If you need to send a token you'll need to go the route @OhhLoz described: Pass the token ID and then load the token on the other side using canvas.tokens.get(id).

I'll keep this issue around as a request to automatically to the conversion to an id (and back) in socketlib, so that entities can be passed around transparently.

@manuelVo manuelVo changed the title Uncaught (in promise) InternalError: too much recursion Allow to send entities as parameters Jun 26, 2023
@manuelVo manuelVo added the enhancement New feature or request label Jun 26, 2023
@mrcomac
Copy link
Author

mrcomac commented Jun 26, 2023

Thank you for the feedbacks. I'll change to pass objects IDs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants