How to run code from a specific user

EEllam4/6/2023
Hi, I have the function foo(bar) and when you run this function it opens a window with some details on is there a way I can make foundry think that the function was run from a specific userId, so the window appears on their screen instead of mine? or some other way to make the window appear on their screen doesn't necessarily have to be some kind of psudo command
RRe4XN4/6/2023
Client code executes for everyone, I believe. So what you can do is use a gate that checks for that specific user, e.g.
if (!user.isGM()) {
// do nothing
}

// run code for this user
if (!user.isGM()) {
// do nothing
}

// run code for this user
EEllam4/6/2023
so the code will be run on a click event, I want the GM to click the button, and the USER to see the window
RRe4XN4/6/2023
Right, I don't think that changes my suggestion. Imagine you have the following snippet in your code:
const printHello = () => {
console.log('Hello!')
}

printHello()
const printHello = () => {
console.log('Hello!')
}

printHello()
If you have 3 logged in users and this function is called, you will see 3 outputs in the console if you open it. The way you would refactor this to only print for a specific user is to add the gate pattern I mentioned above.
EEllam4/6/2023
but the function i'm running renders a handlebars template, and only runs it for the client which runs the code
EEthaks4/6/2023
If you want an action that is not tied to some document lifetime cycle executed on every client to trigger something for another client, you'll need sockets: https://foundryvtt.wiki/en/development/api/sockets With those the GM click can send a message to other clients, and the correct client (as determined by a userId check or via socketlib) can render something on their end.
EEllam4/6/2023
so if i wanted to use a socket to maker user: game.users.get(userId) run the below code:
await game.ptu.utils.dex.render(species, type)
await game.ptu.utils.dex.render(species, type)
it would be
socket.emit('ptu.renderDex', {
userId: userID,
species: species,
type: type
})
socket.emit('ptu.renderDex', {
userId: userID,
species: species,
type: type
})
then elsewhere i'd have
async function handleRenderDex({userId, species, type}) {
if(!!userId && game.userId !== userId) return

await game.ptu.utils.dex.render(species, type)
}

socket.on('ptu.renderDex', handleRenderDex);
async function handleRenderDex({userId, species, type}) {
if(!!userId && game.userId !== userId) return

await game.ptu.utils.dex.render(species, type)
}

socket.on('ptu.renderDex', handleRenderDex);
that right?
EEthaks4/6/2023
Mind the event name, it has to match specific requirements.
EEllam4/6/2023
so the system i'm editing is called ptu so does the event name have to be "system.ptu"?
EEthaks4/6/2023
Looks correct
EEllam4/6/2023
what if there is already an event that runs that?
EEthaks4/6/2023
The event name is essentially a Foundry determined value. If you want to have different events, you have to move the kind of event into the payload and use a single listener determining which function to run based on that.
EEllam4/6/2023
ok cool got it working thankyou!
45359924/15/2023
or just use https://github.com/mclemente/fvtt-advanced-macros you can speciy for what user run a macro on the macro sheet (it use always sockets ).

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Answer OverflowHey there! I have some great news: __**Introducing @Answer Overflow indexing to The League!**__ TlAbout: #package-development**This thread is not for discussions about Foundry package development, but for discussing the purpoGood callIt's more reliable to enter the directory.Follow-up questionsFor Foundry v10+ here is the essential info for module integration as I gather you might want to loaIntegration of Vino Behaviour on TheatreSome time ago I had updated the module VINO (https://github.com/p4535992/foundryvtt-vino), the modul[CLOSED] UI panel for help to Transform/Polymorph tokenI have officially dropped the "Automated Polymorpher" module https://github.com/p4535992/foundryvtt-advancementOrigin flag@arbron I spotted a flag `advancementOrigin` on items granted by advancements but don't see it used comparing fomulaAnyone know of a foundry utility to compare two strings that might be dice formulae to tell which hasheet modules in 2.1.xstarting my run for my sheet modules now, will holler with what info I findCantrip Charges 🧵Cantrip Charges 🧵Item piles gridRough draft of a grid-based inventory system thing - it can't swap positions of entries yetMathJSas a javascript dependencyGet most matched Classes`const intersection = yourArray.filter(item1 => yourOtherArray.some(item2 => item2 === item1))`Application and EventsDoes anyone know if Foundry's `Application` (or the browser itself) does anything magical to clean uPopOut!Just starting a thread here for some discussion of "PopOut!" module in relation to my UI library / TPackage jam judge signup threadPackage jam judge signup thread (free league points inside)git filter troubleshootingI'm troubleshooting a new setup that my code contributors can benefit from. I'm using a .gitconfig fmultiroll damage roll messagesMessing with multiroll chat message in v10❓About Commissions**Do not post commissions in this thread.** This thread is for discussing the commissions process, gIssue Submission ToolI'm looking for suggestions for free ticket submission without signing into GitHub that go beyond Bu