Skip to content

Commit

Permalink
Merge pull request #125 from playroomkit/upstream-update
Browse files Browse the repository at this point in the history
fix: JSON parsing in browser mode
  • Loading branch information
momintlh authored Dec 13, 2024
2 parents 61caacf + 85d300a commit 7f4c90a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
54 changes: 33 additions & 21 deletions Assets/PlayroomKit/Examples/2d-platformer/scripts/GameManager2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@

public class GameManager2d : MonoBehaviour
{

[SerializeField] private GameObject playerPrefab;
[SerializeField]
private GameObject playerPrefab;

/// <summary>
/// player scores and UI to display score of the game.
/// </summary>
[Header("Score and UI")]
[SerializeField] private int score = 0;
[SerializeField] private TextMeshProUGUI scoreTextPlayer1;
[SerializeField] private TextMeshProUGUI scoreTextPlayer2;
[SerializeField]
private int score = 0;
[SerializeField]
private TextMeshProUGUI scoreTextPlayer1;
[SerializeField]
private TextMeshProUGUI scoreTextPlayer2;

private TextMeshProUGUI selectedScoreText;

Expand Down Expand Up @@ -49,10 +52,10 @@ private void Initialize()
_playroomKit.InsertCoin(new InitOptions()
{
maxPlayersPerRoom = 2,
defaultPlayerStates = new() {
{"score", 0},
},

defaultPlayerStates = new()
{
{ "score", 0 },
},
}, () =>
{
_playroomKit.OnPlayerJoin(AddPlayer);
Expand Down Expand Up @@ -92,7 +95,6 @@ void HandleScoreUpdate(string data, string caller)
{
Debug.LogError($"No GameObject found for caller: {caller}");
}

}

/// <summary>
Expand All @@ -110,25 +112,36 @@ private void Update()

players[index].SetState("pos", playerGameObjects[index].transform.position);

players[index].SetState("a", "HELLO WORLD");
players[index].SetState("d", 11);
players[index].SetState("b", 9.81f);
players[index].SetState("c", false);

ShootBullet(index);

for (var i = 0; i < players.Count; i++)
{
if (players[i] != null && PlayerDict.TryGetValue(players[i].id, out GameObject playerObj))
{
Debug.Log("Getting state of: " + players[i].id);
// Debug.Log("Getting state of: " + players[i].id);
var pos = players[i].GetState<Vector3>("pos");

Debug.Log(pos);


string a = players[i].GetState<string>("a");
float b = players[i].GetState<float>("b");
bool c = players[i].GetState<bool>("c");
int d = players[i].GetState<int>("d");

Debug.Log($"Player {i} state: a: {a}, b: {b}, c: {c}, d: {d}");
Debug.Log($"a type: {a.GetType()}, b type: {b.GetType()}, c type: {c.GetType()}, d type: {d.GetType()}");


var color = players[i].GetState<Color>("color");
if (playerGameObjects != null)
{
playerGameObjects[i].GetComponent<Transform>().position = pos;

playerGameObjects[i].GetComponent<SpriteRenderer>().color = color;
}

}
}
}
Expand All @@ -144,12 +157,11 @@ private void ShootBullet(int playerIndex)
{
Vector3 playerPosition = playerGameObjects[playerIndex].transform.position;

score = playerGameObjects[playerIndex].GetComponent<PlayerController2d>().ShootBullet(playerPosition, 50f, score);
score = playerGameObjects[playerIndex].GetComponent<PlayerController2d>()
.ShootBullet(playerPosition, 50f, score);

_playroomKit.RpcCall("ShootBullet", score, PlayroomKit.RpcMode.ALL, () =>
{
Debug.Log("Shooting bullet");
});
_playroomKit.RpcCall("ShootBullet", score, PlayroomKit.RpcMode.ALL,
() => { Debug.Log("Shooting bullet"); });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UBB;
using UnityEngine;
using SimpleJSON;

namespace Playroom
{
Expand Down Expand Up @@ -58,31 +59,41 @@ public void SetState(string key, object value, bool reliable = false)

public T GetState<T>(string key)
{
string stringValue = _ubb.CallJs<string>("GetPlayerStateByPlayerId", null, null, false, _id, key);
string rawValue = _ubb.CallJs<string>("GetPlayerStateByPlayerId", null, null, false, _id, key);

if (string.IsNullOrEmpty(stringValue))
if (string.IsNullOrEmpty(rawValue))
{
Debug.LogWarning($"State for key '{key}' is null or empty.");
return default;
}

try
{
if (typeof(T) == typeof(int) || typeof(T) == typeof(float) || typeof(T) == typeof(bool) ||
typeof(T) == typeof(string))
var jsonNode = JSON.Parse(rawValue);

if (typeof(T) == typeof(string))
{
var wrapper = JsonUtility.FromJson<PrimitiveWrapper<T>>(stringValue);
return wrapper.value;
return (T)(object)jsonNode.Value;
}
else
if (typeof(T) == typeof(int))
{
return JsonUtility.FromJson<T>(stringValue);
return (T)(object)jsonNode.AsInt;
}
if (typeof(T) == typeof(float))
{
return (T)(object)jsonNode.AsFloat;
}
if (typeof(T) == typeof(bool))
{
return (T)(object)jsonNode.AsBool;
}

return JsonUtility.FromJson<T>(rawValue);
}
catch (Exception e)
{
Debug.LogError($"Failed to parse JSON for key '{key}': {e.Message}");
return default; // Return default if parsing fails.
Debug.LogError($"Failed to parse state for key '{key}': {e.Message}\nReceived value: {rawValue}");
return default;
}
}

Expand Down
4 changes: 2 additions & 2 deletions ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ EditorBuildSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Scenes:
- enabled: 0
- enabled: 1
path: Assets/PlayroomKit/Examples/2d-platformer/2d-platformer.unity
guid: cf82eec4c654cf44b89cec2a91afebd5
- enabled: 1
- enabled: 0
path: Assets/PlayroomKit/Examples/discord-activity/discord-activity.unity
guid: bb7ef8e4a67f3fb46b78e5d95a6858a7
- enabled: 0
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ PlayerSettings:
webGLDebugSymbols: 0
webGLEmscriptenArgs:
webGLModulesDirectory:
webGLTemplate: APPLICATION:Default
webGLTemplate: PROJECT:DiscordTemplate
webGLAnalyzeBuildSize: 0
webGLUseEmbeddedResources: 0
webGLCompressionFormat: 2
Expand Down

0 comments on commit 7f4c90a

Please sign in to comment.