Files
BlitzNext/_release/help/commands/2d_commands/SendNetMsg.htm
T
2014-12-08 12:43:20 +13:00

131 lines
4.6 KiB
HTML

<html>
<head>
<title>Blitz3D Docs</title>
<link rel=stylesheet href=../css/commands.css type=text/css>
</head>
<body>
<h1>SendNetMsg type,data$,from,to,reliable</h1>
<h1>Parameters</h1>
<table>
<tr>
<td>
type = value 1-99
<br />
data$ = string containing message to send
<br />
from = player ID of the sender
<br />
to = player ID of the recipient (0=broadcast)
<br />
reliable = flag for sending message reliably
</td>
</tr>
</table>
<h1>Description</h1>
<table>
<tr>
<td>
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 />
<br />
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 />
The TYPE parameter is a number from 1 to 99. These values are denoted as 'user messages'.
<br />
<br />
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 />
<br />
FROM is the player's ID that is sending the message. This is the value returned from the CreateNetPlayer() command.
<br />
<br />
TO is the player's ID you wish to send the message to. A default value of 0 will broadcast to ALL players.
<br />
<br />
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 />
<br />
The example requires that you run it on the local machine while the remote computer runs the example in the RecvNetMsg() command.
</td>
</tr>
</table>
<h1><a href=../2d_examples/SendNetMsg.bb>Example</a></h1>
<table>
<tr>
<td>
; SendNetMsg example
<br />
; ------------------
<br />
; Run this example on the local computer
<br />
; run the example for RecvNetMsg() on a remote computer
<br />
<br />
; Graphics mode with double buffering
<br />
Graphics 640,480,16
<br />
SetBuffer BackBuffer()
<br />
<br />
; Create a network game with NO requester
<br />
joinStatus=HostNetGame("ShaneGame")
<br />
<br />
; A type to hold all the player's information
<br />
Type multi
<br />
Field x
<br />
Field y
<br />
Field id
<br />
Field name$
<br />
Field xspeed
<br />
Field boxColor
<br />
End Type
<br />
<br />
; make sure the game started ok...
<br />
If joinStatus=2 Then
<br />
Print "Hosted game started... "
<br />
Else
<br />
Print "Hosted game could not be started!"
<br />
End
<br />
End If
<br />
<br />
; Create 5 local players using TYPEs
<br />
For t = 1 To 5
<br />
; New type instance
<br />
player.multi = New Multi
<br />
; Assign the ID field with the created player ID and name him
<br />
playerID=CreateNetPlayer("Player" + t)
<br />
<br />
; if the player was created ok ... assign some random parameters
<br />
If playerID <> 0 Then
<br />
player
<br />
ame$="Player" + t
<br />
playerx = Rand(640)
<br />
playery = Rand(480)
<br />
playeroxColor = Rand(255)
<br />
playerxspeed = Rand(1,5)
<br />
; Print some text results
<br />
Print "Player " + t + " has joined the game with ID=" + playerID
<br />
Else
<br />
Print "The player couldn't join! Aborting!"
<br />
End If
<br />
Next
<br />
<br />
; We've got them all! Wait for a key
<br />
Print "All local players are joined! Press a key ..."
<br />
WaitKey()
<br />
<br />
; Loop this routine
<br />
While Not KeyHit(1)
<br />
Cls
<br />
; for each of the players, update their locations on the screen
<br />
For player = Each multi
<br />
Color playeroxColor,playeroxColor,playeroxColor
<br />
Rect playerx,playery,10,10,1
<br />
Text playerx-10,playery-15,player
<br />
ame$
<br />
playerx = playerx + playerxspeed
<br />
If playerx > 640 Or playerx < 0 Then
<br />
playerxspeed=-playerxspeed
<br />
message$="Player ID #" + playerID + " hit a wall!"
<br />
; Send a broadcast message if a player rebounds off the wall
<br />
; this message will show up on the remote machine
<br />
SendNetMsg Rand(1,99),message$,playerid,0
<br />
End If
<br />
Next
<br />
Flip
<br />
Wend
<br />
End
<br />
</td>
</tr>
</table>
<br>
<a target=_top href=../index.htm>Index</a><br>
<br>
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>
</html>