Issue Seven

Page 19

2. GMDEV

18

TUT: MMORPG (Continued) In step event of your player object, the player you are yourself, you can read the messages and take action like this: mreceive = mplay_message_recieve(0); mid = mplay_message_id(); mvalue = mplay_message_value(); if (mreceive && (mid==1)) { instid = instance_create(0,0,obj_player); ds_map_add(global.data,mvalue,instid); } And you can now see how you can easily add more messages to interpret. That is what we will do now, as we create the system which will synchronize all movements. I will not create a movement system or anything like that in this tutorial. I will just show you how to do mplay. But anyway, put this when you walk somewhere: mplay_message_send(0,100+global.myid,x); mplay_message_send(0,200+global.myid,y); And when you change sprite, for an example if you start fighting or if you walk or anything like that which you can not figure out from client side (you must always try to figure things out from your side without any mplay, which we will discuss soon), you do this: mplay_message_send(0,300+global.myid, sprite_index); And then to your code you’ll add an action for each received message. You can make it look like this: mreceive = mplay_message_recieve(0); mid = mplay_message_id(); mvalue = mplay_message_value(); if (mreceive && (mid==1)) { instid = instance_create(0,0,obj_player); ds_map_add(global.data,mvalue,instid); } if (mreceive && (mid>100)) { nid = string_char_at(string(mid),1); player = string_char_at(string(mid), string_length); if (nid==1) { (ds_map_find_value(global.data,player)). x = mvalue; }

18

if (nid==2) { (ds_map_find_value(global.data,player)). y = mvalue; } if (nid==3) { (ds_map_find_value(global.data,player)). sprite_index = mvalue; } } I checked first whether the id was larger than 100, because this means it was not a system message. Then I intialize the variables nid and player, by reading the first number of the id (we know that 101 will send x, 204 will send y etc) and player by reading the last number of mid (since we know that mid originally was created by 100+global.myid, or 200+global.myid etc). Using values from the data structure that we earlier created we can find the instance corresponding to the player that sent the message. We can therefore now walk around and see each other. Now is the time to do some discussion about what really needs to be transferred using mplay and what needs not. We don’t want to send more messages than we actually have to. All messages slow down a little, so the more we can do on our own the better. For an example, if you walk on a gold coin on the ground your player automatically picks it up (and destroys the coin object itself), then you would not need to send that info, instead you could put instance_destroy() in collision with obj_player, which would be the object to represent all the other players. Try to apply things like this as often as you can. Another thing you need to think of is when to use data and when to use messages. Often changing values such as x and y might be better synchronize with messages, but stats such as the individuals attack and defence would better be synchronized with data. Here comes an example of how you could do to retrieve those stats of the player with id 5. We’re using the same system as with messages. First this is how to write info, attack and defence for player five: mplay_data_write(105,my_attack); mplay_data_write(205,my_defence); And to collect the data: Player5attack = mplay_data_read(205); Player5defence = mplay_data_read(205);


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.