This tutorial will show how RPC works.
Make sure to download the 002 tutorial on Itch.io. It contain some bugfixes.
RPC stands for remote procedure call.
The point with it is that any player can MAKE any player run any script with any arguments.
Ex the client can make the server run scrGiveMeTheMapData
You remeber that each game is independent.
All variables that is on the server can only be accessed by the server.
And all variables on the client can only be accessed by the client.
But sometimes you need variables from the server or from a client.
Ex when the server create a dynamic map.
The client dont know what the map is.
So the client must request the map data somehow.
This is done by RPC.
htme_obj_player
You can see that the player object got key events 1 and 2
You see that this code will only run on the LOCAL
If I dont use the htme_isLocal the key event would work on the REMOTE when P2 press the 1 key.
You see that we create a obj_RPC_To_Server_Dummy instance
This is a handler object for the RPC that will exists until we receive the data from the server.
Remeber that it may take 1-2 seconds before we receive any data. So this just wait until we get a responce.
You see we set some data to the RPC handler
We use that data here:
obj_RPC_To_Server_Dummy>Create event
The TestData is set to a default value but we changed it to "Hi server how are you?"
If we go to the step event
The thing you need to focus on is rpc_script_to_run=scr_RPC_dummy_return;
This will set the script the receiver of the RPC will run
htmerpc_send is the actual send script.
self.rpc_id is a uniqe id for this RPC message
rpc_script_to_run is the variable that hold the script we want to run on the receiver side
obj_server_handler.htme_mp_player is the server object and htme_mp_player is the servers hash key.
The hash key is used as an adress to send info via the engine to a specific player. In this case the player that is the server.
We will talk more about obj_server_handler in the next tutorial.
TestData is the variable we send with the RPC "Hi server how are you?"
This is actually the arguments for the script we want the receiver to run. We can add up to 12 arguments in the send.
Note that you must use reals or strings.
You cant simply send a ds_list with TestData=myDsList;
The TestData would just hold a 1 or a 2 real id that points to the list in the games memory.
But the games are independent. And the server dont share the same memory.
You would need to turn the list to an json (string) or a comma separeted string Ex "value1,value2,value3"
Ok. We now imagine that this is sent over the internet and now is received by the server.
The server read the RPC and call scr_RPC_dummy_return("Hi server how are you?")
scr_RPC_dummy_return
*Andreas make a Jedi mind trick* "You are now on the server computer."
The server do show_debug_message(argument0); and write "Hi server how are you?"
We also return a value "Ok. I got it"
The engine will now send back the return value to the sender.
We now imagine that the client who sent the RPC receive the retrun value
obj_RPC_To_Server_Dummy>Step event
The client has played while we where gone and this handler checked this line each step to see if a response has come.
var returnedValue = ds_map_find_value(obj_htme_rpc.returnedValues,self.rpc_id); for a return value that we now got
It first pass the timeout. If for some reason the network is super slow. We will ignore the response and destroy the handler.
Next is a check to make sure we still want the return value.
if htmerpc_Allow_Run_Script_Current_Room(script_get_name(rpc_script_to_run),rpc_room)=false Check if we still are in the same room as we where when we sent the RPC
Because we may have moved our player to a different room and now the RPC is not needed anymore and may harm the game.
Ex Say P2 enter a room that needed some item info about an item in the room. So P2 send a RPC to the server and request that info.
While the RPC was on its way we decided to go to the next room.
Now P2 is in another room and by now we received the info from the server.
But the info is not needed anymore.
Sometimes you send a RPC and you want the return value whatever room you are in.
To make that happen you need to add the script to an array
Scripts>Gmnet>RPC>htmerpc_SCRIPTS_RUN_IN_ALL_ROOMS_INIT
There are two situations here.
When the client send a RPC to the server but the server is in another room than the sender.
And when the a client or server send a RPC to another client but he is in another room. Or if the client changed room while waiting on the return value.
htmerpc_SCRIPT_WHO_SEND_RETURN_VALUE_INIT is also a script that you use to set which scripts have a return value. Sometimes you don't want to send back a value.
Objects>Gmnet>RPC>obj_RPC_To_Server_Dummy>Step event
if AllowRun
If all is ok. The code in this statement will run.
Right now we just show the return message.
obj_RPC_To_Client_Dummy
Is also a RPC handler
Create event
Here we set client_object to obj_client_handler
All clients got their own obj_client_handler that is synced to the other players.
We can use this REMOTE controlled object and get the player hash from it and send a RPC to a specific player.
Step event
It works like before but this time we make a client run a specific script.
We could loop all obj_client_handler to make every player run a specific script. You can also set it to "". That would sen the message to all connected players.
Ex: We may want all players to go to a specific room in the game.
Ok. Time to test RPC
We click on P1 and press 1
The RPC send from the server to the server so we run the script and send back the return value.
You can see the RPC message in the debug output.
If we go to P2 and press 1
You only see the received RPC to run the script but not the return value
This is because it was sent back to P2.
Lets make P2 go to the other room and press 1.
Now we see that the RPC was rejected because the server player and P2 is not in the same room.
Lets change that.
htmerpc_SCRIPTS_RUN_IN_ALL_ROOMS_INIT
allow_to_run_in_any_room_server[0]="scr_RPC_dummy_return";
Ok. Now we try again.
P2 go to the other room and press 1.
Now we see that the RPC was accepted by the server and run the script and sent back a return value.
This gives you control over the RPC messages.
Ok. Thats it for now.
Không có nhận xét nào:
Đăng nhận xét