-
Notifications
You must be signed in to change notification settings - Fork 2
Network
Network is an autoloaded module which you can access anywhere in the code.
Network serves two purposes at the moment : centralize necessary information to perform API calls, and managing the WebSocket events.
If you want your node to perform an API call, call
Network.req(calling_object, method_to_trigger, route, method = HTTPClient.METHOD_GET, headers=PoolStringArray(), body="", params=[])-
calling_objectis the traget object where the call back is. -
method_to_triggerthe methode name as a string. -
routeis the relative url to call. -
methodis an int that descrive the HTTP method (see HTTPClient enum). -
headersthe header of the request (as an PoolStringArray). -
bodythe body of the request. -
paramsare the parameters that should be transmitted to the call back.
After the request resolve or fail, method_to_trigger in calling_object is called with arguments
-
err: the error code of the resuqtest (0 is there was no error) -
response_code: the http code of the response (see here) -
headers: header of the response -
body: Body of the response as a PoolByteArray - the arguments passed in
params
Example :
func _request_assignation(quantity):
Network.req(self, "_on_ship_assigned",
"/api/games/" + _game_data.id +
"/systems/" + _game_data.selected_state.selected_system.id +
"/fleets/" + _game_data.selected_state.selected_fleet.id +
"/ship-groups/",
HTTPClient.METHOD_POST,
[ "Content-Type: application/json" ],
JSON.print({"category" : ship_category.category, "quantity" : quantity}),
[quantity, _game_data.selected_state.selected_fleet]
)
func _on_ship_assigned(err, response_code, headers, body, quantity, fleet):
if err:
ErrorHandler.network_response_error(err)
if response_code == HTTPClient.RESPONSE_NO_CONTENT: # change to the wanted response code
# handle response here
else:
var result = JSON.parse(body.get_string_from_utf8()).result
# handle error hereA websocket event is pushed by the server and notifies the client of any operation performed by the server or another player which affects the game state.
The documentation of existing WS event are presented here
You just have to add a new signal in network.gd. This signal must have the same name as the counterpart Rust Action enum.
For example, if the Rust action is "CombatEnded", you shall have the following signal :
signal CombatEnded(data)where data is the JSON content of the WS event. You can name it as you want.
Then, you just have to connect the signal in the nodes which needs to be aware of the event :
func ready():
Network.connect("CombatEnded", self, "_on_combat_ended")
func _on_combat_ended(data):
print(data)