Server returns string as 'EncodedObjectAsID'.
Asked Answered
U

9

0

Hello! I am trying to send map to client from server.
Server:

func _network_peer_connected(peer_id:int):
	var player = load("res://Nodes/Character.tscn").instance()

	player.set_name(String(peer_id))

	player.set_network_master(peer_id)

	$"/root/Game/Players".add_child(player)
	players.append(peer_id)

	for playerModel in $"/root/Game/Players".get_children():
		if playerModel != player:
			rpc_id(peer_id, "new_player", int(playerModel.name))

	rpc_id(peer_id, "load_new_user_map", getMap())
	rpc("new_player", peer_id)
	print("A new plr connected!")

getMap:
func getMap():
	var GetMapRequest = HTTPRequest.new()
	add_child(GetMapRequest)
	GetMapRequest.request("url" + gameId)
	var map_request_completed_info = yield(GetMapRequest, "request_completed")
	var map_response_code: int = map_request_completed_info[1]
	var map_body: PoolByteArray = map_request_completed_info[3]
	var map_data = str(map_body.get_string_from_utf8())
	
	GetMapRequest.queue_free()
	
	
	if map_response_code != 200:
		return "ORPHAN"
	return map_data

Client:

remotesync func load_new_user_map(map_str):
	if get_tree().get_rpc_sender_id() == 1:
		var map = JSON.parse(map_str)
		if "CONTENT" in map:
			# The "CONTENT" key holds the actual game content
			var game_content = JSON.parse(map["CONTENT"])
			
			# Access the content elements, for example:
			if "Level" in game_content:
				var levels = game_content["Level"]
				print("Levels:", levels)
			
			if "Scripts" in game_content:
				var scripts = game_content["Scripts"]
				print("Scripts:", scripts)
			
			if "LocalGUI" in game_content:
				var local_gui = game_content["LocalGUI"]
				print("Local GUI:", local_gui)
			
			if "GameSettings" in game_content:
				var game_settings = game_content["GameSettings"]
				print("Game Settings:", game_settings)
		else:
			print("Map is orphan")

This is what the getMap() returns:
"{"CONTENT":"{\"Level\":[{\"name\":\"Objects\",\"position\":[0,0,0],\"rotation\":[0,0,0],\"size\":[1,1,1],\"children\":[],\"type\":\"ObjectsNode\",\"color\":\"#NULL\"}],\"Scripts\":[],\"LocalGUI\":[{\"name\":\"Button\",\"type\":\"Button\",\"buttonText\":\"Button\"},{\"name\":\"Text\",\"type\":\"Text\",\"Text\":\"Text\"}],\"GameSettings\":[{\"Time\":\"Day\",\"IsPublic\":true,\"FogEnabled\":true,\"FogColor\":\"#FFFFFF\"}]}"}"

Unexpressed answered 31/7, 2023 at 6:6 Comment(0)
U
0

Hopefully bumps are allowed

Unexpressed answered 31/7, 2023 at 15:1 Comment(0)
G
0

What exactly is your question or problem?

Goshen answered 31/7, 2023 at 16:11 Comment(0)
U
0

Goshen I want to send a JSON as string from a API, but it returns EncodedObjectAsId

Unexpressed answered 31/7, 2023 at 16:17 Comment(0)
G
0

I'm not sure what "it" is. Do you mean that the function getMap() does not return a String?

Goshen answered 31/7, 2023 at 16:39 Comment(0)
U
0

Goshen The function returns a string and everything on server is correct however the getMap()'s value on client is EncodedObjectAsId

Unexpressed answered 31/7, 2023 at 16:48 Comment(0)
G
0

I've haven't actually done any networking in Godot other than using HTTPRequest to communicate with a web server, so I might not be qualified to help with this.

Does the function load_new_user_map(map_str) fail because its parameter is not a String? I.e., does this line produce an error?
var map = JSON.parse(map_str)

Goshen answered 31/7, 2023 at 17:1 Comment(0)
U
0

Goshen it says unexpected EncodedObjectAsId

Unexpressed answered 31/7, 2023 at 18:40 Comment(0)
G
0

What is the full error message?

Goshen answered 31/7, 2023 at 18:52 Comment(0)
U
0

Occurred because the rpc_id was called even before getMap could return a value.

Unexpressed answered 1/8, 2023 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.