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

RPC API #43

Merged
merged 14 commits into from
Feb 13, 2024
Merged

RPC API #43

merged 14 commits into from
Feb 13, 2024

Conversation

momintlh
Copy link
Collaborator

@momintlh momintlh commented Feb 2, 2024

Changes:

  • Added RPCRegister
  • Added RPCCall
  • Added enum for RPC Modes in Unity.

Next Tasks:

  • Currently only String data can be passed, maybe giving a helper function to convert data to JSON?
  • Callback in RPCregister from Unity.
  • List Of Callbacks
  • Parsing data and sender
  • Clean-Up

Testing:

Verified the changes by testing RPC registrations and calls with different modes from both C# and JavaScript. Checked the sequence of ENUM values and ensured correct callback invocations.

Test Results:

  • Host registers and gets the following response:
    image

Rock is the data passed by sender / the nonhost player in this case.

  • Nonhost player calls:
    image

here response (Okay!) is being received from the register callback

based on #37


Final demo output: https://mmntlh.itch.io/playroom-demo

## Changes:
* Added RPC.register
* Added RPC.call
* Added enum for RPC Modes.

## Next ToDo:
* Currently Only String data can be passed, need to use JSON.
* Callback in RPCregister from Unity.
@momintlh momintlh requested review from asadm and SaadBazaz February 2, 2024 14:31
Copy link
Collaborator

@SaadBazaz SaadBazaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs clarification.

Assets/PlayroomKit/PlayroomKit.cs Outdated Show resolved Hide resolved
Assets/PlayroomKit/PlayroomKit.cs Outdated Show resolved Hide resolved
@SaadBazaz SaadBazaz mentioned this pull request Feb 4, 2024
Issue: RPC event with same name and different callbacks only calls the latest callback.
@momintlh
Copy link
Collaborator Author

momintlh commented Feb 4, 2024

The issue where callback was getting overridden is fixed, so now each RPC event can call its own callback, but when the same event has different callbacks, only the latest one gets invoked. Current solution I have is to change the Dictionary<string, Action> to Dictionary<string, List<Action>>, so each event can hold multiple callbacks, But I might be Overengineering this part, so any suggestions would be helpful. @SaadBazaz @asadm.

Changes are in this commit: c60dc8d

@SaadBazaz
Copy link
Collaborator

The issue where callback was getting overridden is fixed, so now each RPC event can call its own callback, but when the same event has different callbacks, only the latest one gets invoked. Current solution I have is to change the Dictionary<string, Action> to Dictionary<string, List<Action>>, so each event can hold multiple callbacks, But I might be Overengineering this part, so any suggestions would be helpful. @SaadBazaz @asadm.

Changes are in this commit: c60dc8d

I think your approach is fine. Do you have any other solutions in mind?

@momintlh
Copy link
Collaborator Author

momintlh commented Feb 6, 2024

 RpcRegister: function (name) {
    if (!window.Playroom) {
      console.error("Playroom library is not loaded. Please make sure to call InsertCoin first.");
      return;
    }

    function registerCallback(data, sender) {
      //TODO: callback from unity here?  dynCall('vi', callback, [data, sender])
      console.log(`${sender.id} played their turn!`);
      console.log(`recieving: ${data}`);
      return 'okay!';
    }

    // Register the callback for the RPC
    Playroom.RPC.register(UTF8ToString(name), registerCallback);
  },

I believe the (data, sender) should be accessible from with unity, right? for that I can have a dynCall() within registerCallback and pass the data, sender as arguments, to do something after the rpcCall has invoked its callback.
(will need to serialize both data and sender before passing as well.)

^ is this the correct architecture here? @SaadBazaz

@SaadBazaz
Copy link
Collaborator

 RpcRegister: function (name) {
    if (!window.Playroom) {
      console.error("Playroom library is not loaded. Please make sure to call InsertCoin first.");
      return;
    }

    function registerCallback(data, sender) {
      //TODO: callback from unity here?  dynCall('vi', callback, [data, sender])
      console.log(`${sender.id} played their turn!`);
      console.log(`recieving: ${data}`);
      return 'okay!';
    }

    // Register the callback for the RPC
    Playroom.RPC.register(UTF8ToString(name), registerCallback);
  },

I believe the (data, sender) should be accessible from with unity, right? for that I can have a dynCall() within registerCallback and pass the data, sender as arguments, to do something after the rpcCall has invoked its callback. (will need to serialize both data and sender before passing as well.)

^ is this the correct architecture here? @SaadBazaz

Looks about right, what if we need multiple? 😬 Does the PlayroomKit JS support multiple callbacks?

@asadm
Copy link
Collaborator

asadm commented Feb 6, 2024

multiple callbacks for one rpc? yes.

@momintlh
Copy link
Collaborator Author

momintlh commented Feb 12, 2024

DEMO game: https://mmntlh.itch.io/playroom-demo

Score (UI) is updated using RPC now. I tried using rpc for syncing movement (instead of using get/set state) but it was super laggy.

Copy link
Collaborator

@SaadBazaz SaadBazaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with comments.

Assets/Plugins/PlayroomPlugin.jslib Outdated Show resolved Hide resolved
}
else
{
Debug.Log($"{data} is '{data.GetType()}' which is currently not supported by RPC!");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you drop an explainer why this is necessary? Why can't we support these types?

Assets/PlayroomKit/PlayroomKit.cs Show resolved Hide resolved
Assets/PlayroomKit/PlayroomKit.cs Show resolved Hide resolved
@momintlh momintlh merged commit 3f56b7a into main Feb 13, 2024
2 checks passed
@SaadBazaz SaadBazaz mentioned this pull request Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants