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?
Eellam4/6/2023
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
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()


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
EEthaks4/6/2023
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)


it would be
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);


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
Eellam4/6/2023
got it working thankyou!
445359924/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 ).