I'm not sure if it's really a bug or it's just supposed to work like that, but I decide to post it here.
(Please review my code at the bottom)
In my test code, when a player click the mouse, it will add 1 to the shared object named shared.count
, and send out a "showChange"
event to other clients simultaneously using partyEmit("showChange")
. "showChange"
event will trigger a showChange()
function in every client, and showChange()
function will print the value of shared.count
in the console.
Expected behavior
showChange()
function is expected to print the changed value of shared.count
. That is to say, if the initial value of shared.count
is 1, when I click the mouse, I'm supposed to have a 2 in the console.
An expected process would be:
- initially,
shared.count
is 1;
- I click the mouse;
shared.count
changes from 1 to 2;
- a
"showChange"
event is sent to the party;
- every client receives
"showChange"
event;
- every client has
shared.count
(should be 2 now) printed in their consoles.
Observed behavior
On the sender client(the client that sent "showChange"
event), the above process happened expectedly. I got 2 in the console finally.
However, on the receiver client(other clients that received "showChange"
event), there was 1 in the console instead of 2 in step 6. That means the receiver client didn't read the changed value of shared.count
but read the previous value.
If the receiver client read shared.count
after the execution of showChange()
, it can have the right changed value that is 2. So the asynchrony only happens for an instant after partyEmit()
.
p5.js and p5.party versions
I think it's the latest version as the source is https://cdn.jsdelivr.net/npm/p5.party@latest/dist/p5.party.js
Error Message
No, there isn't an error. The program can still run.
Your code
Here is my test code in p5 editor.
var shared;
function preload() {
partyConnect(
"wss://deepstream-server-1.herokuapp.com",
"async_example",
"room_0"
);
shared = partyLoadShared("shared");
}
function setup() {
createCanvas(300, 300);
background(0);
if(partyIsHost()) shared.count = 1; // initial value is 1
partySubscribe("showChange", showChange);
}
function showChange() {
console.log("init: " + shared.count); // sender gets the changed value, while receiver gets the previous value
let countdown = setInterval(function() {
console.log(shared.count);
}, 10); // if trying to get shared.count an instant later, both sender and receiver can have the changed value
setTimeout(function() {
clearInterval(countdown);
}, 100);
}
// when clicking the mouse, add 1 to shared.count and tell party to run showChange() to see the changed number in console
function mouseClicked() {
let x = mouseX, y = mouseY;
if(x < width && y < height) {
// change of shared.count and sending of 'showChange' event happen together here
shared.count ++;
partyEmit("showChange");
// This is my current solution - delay the execution of partyEmit(), and it works expectedly; but it looks weird
// setTimeout(function() {
// partyEmit("showChange");
// }, 50);
}
}