SendNetMsg.htm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <html>
  2. <head>
  3. <title>Blitz3D Docs</title>
  4. <link rel=stylesheet href=../css/commands.css type=text/css>
  5. </head>
  6. <body>
  7. <h1>SendNetMsg type,data$,from,to,reliable</h1>
  8. <h1>Parameters</h1>
  9. <table>
  10. <tr>
  11. <td>
  12. type = value 1-99 <br />
  13. data$ = string containing message to send <br />
  14. from = player ID of the sender <br />
  15. to = player ID of the recipient (0=broadcast) <br />
  16. reliable = flag for sending message reliably
  17. </td>
  18. </tr>
  19. </table>
  20. <h1>Description</h1>
  21. <table>
  22. <tr>
  23. <td>
  24. First off, this ONLY works when you have joined a network game via StartNetGame or JoinNetGame and you have created a player via CreateNetPlayer (you must create a player, even if it is just to lurk). <br />
  25. <br />
  26. This is probably the most complicated of the networking commands. This what you use to actually send a message to one or all of the players on the network game. The other players will use RecvNetMsg() to intercept your message. <br />
  27. The TYPE parameter is a number from 1 to 99. These values are denoted as 'user messages'. <br />
  28. <br />
  29. The Data$ parameter is the actual string that contains the message you want to send. Helpful to know that in order to keep traffic low, you will want to combine details of a message into a single message instead of sending multiple messages with a single element. For example, you might want to send X, Y, and FRAME in a single string like "200,100,4" and parse it out at the recipient's end. <br />
  30. <br />
  31. FROM is the player's ID that is sending the message. This is the value returned from the CreateNetPlayer() command. <br />
  32. <br />
  33. TO is the player's ID you wish to send the message to. A default value of 0 will broadcast to ALL players. <br />
  34. <br />
  35. The RELIABLE flag will put a priority on the message and it will ensure there is no packet loss in the delivery. However, it is at least 3 times slower than a regular non-reliable message. <br />
  36. <br />
  37. The example requires that you run it on the local machine while the remote computer runs the example in the RecvNetMsg() command.
  38. </td>
  39. </tr>
  40. </table>
  41. <h1><a href=../2d_examples/SendNetMsg.bb>Example</a></h1>
  42. <table>
  43. <tr>
  44. <td>
  45. ; SendNetMsg example <br />
  46. ; ------------------ <br />
  47. ; Run this example on the local computer <br />
  48. ; run the example for RecvNetMsg() on a remote computer <br />
  49. <br />
  50. ; Graphics mode with double buffering <br />
  51. Graphics 640,480,16 <br />
  52. SetBuffer BackBuffer() <br />
  53. <br />
  54. ; Create a network game with NO requester <br />
  55. joinStatus=HostNetGame("ShaneGame") <br />
  56. <br />
  57. ; A type to hold all the player's information <br />
  58. Type multi <br />
  59. Field x <br />
  60. Field y <br />
  61. Field id <br />
  62. Field name$ <br />
  63. Field xspeed <br />
  64. Field boxColor <br />
  65. End Type <br />
  66. <br />
  67. ; make sure the game started ok... <br />
  68. If joinStatus=2 Then <br />
  69. Print "Hosted game started... " <br />
  70. Else <br />
  71. Print "Hosted game could not be started!" <br />
  72. End <br />
  73. End If <br />
  74. <br />
  75. ; Create 5 local players using TYPEs <br />
  76. For t = 1 To 5 <br />
  77. ; New type instance <br />
  78. player.multi = New Multi <br />
  79. ; Assign the ID field with the created player ID and name him <br />
  80. playerID=CreateNetPlayer("Player" + t) <br />
  81. <br />
  82. ; if the player was created ok ... assign some random parameters <br />
  83. If playerID <> 0 Then <br />
  84. player <br />
  85. ame$="Player" + t <br />
  86. playerx = Rand(640) <br />
  87. playery = Rand(480) <br />
  88. playeroxColor = Rand(255) <br />
  89. playerxspeed = Rand(1,5) <br />
  90. ; Print some text results <br />
  91. Print "Player " + t + " has joined the game with ID=" + playerID <br />
  92. Else <br />
  93. Print "The player couldn't join! Aborting!" <br />
  94. End If <br />
  95. Next <br />
  96. <br />
  97. ; We've got them all! Wait for a key <br />
  98. Print "All local players are joined! Press a key ..." <br />
  99. WaitKey() <br />
  100. <br />
  101. ; Loop this routine <br />
  102. While Not KeyHit(1) <br />
  103. Cls <br />
  104. ; for each of the players, update their locations on the screen <br />
  105. For player = Each multi <br />
  106. Color playeroxColor,playeroxColor,playeroxColor <br />
  107. Rect playerx,playery,10,10,1 <br />
  108. Text playerx-10,playery-15,player <br />
  109. ame$ <br />
  110. playerx = playerx + playerxspeed <br />
  111. If playerx > 640 Or playerx < 0 Then <br />
  112. playerxspeed=-playerxspeed <br />
  113. message$="Player ID #" + playerID + " hit a wall!" <br />
  114. ; Send a broadcast message if a player rebounds off the wall <br />
  115. ; this message will show up on the remote machine <br />
  116. SendNetMsg Rand(1,99),message$,playerid,0 <br />
  117. End If <br />
  118. Next <br />
  119. Flip <br />
  120. Wend <br />
  121. End <br />
  122. </td>
  123. </tr>
  124. </table>
  125. <br>
  126. <a target=_top href=../index.htm>Index</a><br>
  127. <br>
  128. Click <a href=http://www.blitzbasic.co.nz/b3ddocs/command.php?name=SendNetMsg&ref=comments target=_blank>here</a> to view the latest version of this page online</body>
  129. </html>