Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gdevelop5:all-features:network [2018/08/01 00:47]
4ian
gdevelop5:all-features:network [2019/05/20 00:02] (current)
piyushpalawat99
Line 1: Line 1:
 # Network # Network
  
-GDevelop provides ​with network features that enables ​games to communicate with remote servers, API or website.+GDevelop provides network features that enable ​games to communicate with remote servers, API or websites.
  
 # Send a request to a server/​API/​web page # Send a request to a server/​API/​web page
  
-Everything on the internet ​is working using HTTP requests. When you visit a webpage, your browser ​send a request to the web page address, and the website server ​send a response with the content of the webpage+Everything on the internet ​uses HTTP requests. When you visit a webpage, your browser ​sends a request to the web page address, and the website server ​sends a response with the content of the webpage.
-Games and applications work similarly to send or get data to a server: they send a request to a specific address (also called an endpoint), optionally with parameters, and the server will send back a response. The set of all requests that is handled by a server is sometimes called an API.+
  
-HTTP Requests can have "​verb"​ associated ​to theirin addition the address and the parameters. Requests to get data or fetch a webpage are usually "​GET"​ requests. Requests to post data are usually "​POST"​ requests.+Games and applications work similarly to send or get data to server:  
 + 
 +  * they send a request ​to a specific address (also called an endpoint). Optionallythe request can include parameters. 
 +  * the server sends back a response. The set of all requests that are handled by a server is sometimes called an API. 
 + 
 +In addition ​to the address and the parameters, HTTP requests can have a "​verb"​ associated as well Requests to get data or fetch a webpage are usually "​GET"​ requests. Requests to post data are usually "​POST"​ requests.
  
 GDevelop provides the action called "Send a request to a web page". You can specify the host and the path to the API/web page to be called (for example, if your "​endpoint"​ is `https://​mygame.com/​api/​store-score`,​ the host is `https://​mygame.com` and the path is `/​api/​store-score` (don't forget the slash /)). You can also specify the content of the request (the parameter that will be received by the server). GDevelop provides the action called "Send a request to a web page". You can specify the host and the path to the API/web page to be called (for example, if your "​endpoint"​ is `https://​mygame.com/​api/​store-score`,​ the host is `https://​mygame.com` and the path is `/​api/​store-score` (don't forget the slash /)). You can also specify the content of the request (the parameter that will be received by the server).
  
-When the server ​send the response, it is saved in a variable so that you can read what was send,+When the server ​sends the response, it is saved in a variable so that you can read what was sent.
  
 ## How to format the content ## How to format the content
  
-* For GET requests, parameters have to be send in the content in the format of a "query string":​+* For GET requests, parameters have to be sent in the content in the format of a "query string":​
 `parameter1=value1&​parameter2=value2&​parameter3=value3`... `parameter1=value1&​parameter2=value2&​parameter3=value3`...
  
Line 22: Line 26:
 `"​score="​ + VariableString(Score) + "&​playerName="​ + VariableString(Name)` `"​score="​ + VariableString(Score) + "&​playerName="​ + VariableString(Name)`
  
-* For POST requests, it depends on what is expected by the server, but most of the time the server ​expect ​JSON formatted text. +* For POST requests, it depends on what is expected by the server, but most of the time the server ​expects ​JSON formatted text. 
  
 You can either construct it yourself: You can either construct it yourself:
Line 31: Line 35:
 ### Variable to JSON ### Variable to JSON
  
-JSON is a text format that can be used to describe structure of data, containing number, strings, objects and arrays.+JSON is a text format that can be used to describe ​the structure of data, containing number, strings, objects and arrays.
 For example, an object containing the score and the name of a player would be: `{ "​name": ​ John, "​score":​ 45 }`. For example, an object containing the score and the name of a player would be: `{ "​name": ​ John, "​score":​ 45 }`.
  
Line 38: Line 42:
 > Children can be manipulated using the usual actions related to variables, by entering their name after a dot after the name of the parent variable (for example, if the variable containing the children is called Progress: `Progress.Level`,​ `Progress.Score`...). ​ > Children can be manipulated using the usual actions related to variables, by entering their name after a dot after the name of the parent variable (for example, if the variable containing the children is called Progress: `Progress.Level`,​ `Progress.Score`...). ​
  
-<​note>​Learn more about structure ​on the page about [[gdevelop5:​all-features:​advanced-variables-structure|advanced ​variables ​usage]].</​note>​+<​note>​Learn more about Structures ​on the page about [[gdevelop5:​all-features:​variables|variables]].</​note>​
  
-Structure ​are useful as they can store all the data concerning something, that can then be send to a server. To do this, you can convert the structure variable to JSON, using the expression called `ToJSON`. For example:+Structures ​are useful as they can store all the data concerning something, that can then be sent to a server. To do this, you can convert the structure variable to JSON, using the expression called `ToJSON`. For example:
  
-`ToJSON(Progress)`+``` 
 +ToJSON(Progress) 
 +```
  
-If Progress variable has a children "​Level"​ with value 4 and "​Score"​ with value 1500, the resulting JSON will be: `{"​Level":​ 4, "​Score":​ 1500}`.+If the Progress variable has a children "​Level"​ with value 4 and "​Score"​ with value 1500, the resulting JSON will be: `{"​Level":​ 4, "​Score":​ 1500}`.
  
-You can use it as the content of a request ​send to the server.+You can use it as the content of a request ​sent to the server.
  
 ### JSON to variable ### JSON to variable
Line 52: Line 58:
 You can go the other way, and convert JSON back to a variable. This is useful when you've received the answer from the server, which is also usually formatted in JSON. You can go the other way, and convert JSON back to a variable. This is useful when you've received the answer from the server, which is also usually formatted in JSON.
  
-Use the action "​**Convert JSON to a variable**"​ (or "​Convert JSON to global variable",​ or "​Convert JSON to object variable"​). Specify the JSON to be converted (which will be itself stored in a variable) and the variable where to store the resulting structure.+Use the action "​**Convert JSON to a variable**"​ (or "​Convert JSON to global variable",​ or "​Convert JSON to object variable"​). Specify the JSON to be converted (which will be itself stored in a variable) and the variablewhere you need to store the resulting structure.