These are the medias.
These are the better snippet. Click the left sprite to host, or right sprite to be a client.
Control with virtual joystick.
To keep the code simple, it's LAN only.
Please try it with 1 host and 2 or more client (beside the host).
Note that if this part below is deleted, the host would not sent the player position datas to all client, so each client can only see himself and the host moving, and not able to see other players moving.
`Send this player position to all players
Dmessage = CreateNetworkMessage ( )
AddNetworkMessageFloat ( Dmessage, messageFrom )
AddNetworkMessageFloat ( Dmessage, otherX#)
AddNetworkMessageFloat ( Dmessage, otherZ# )
AddNetworkMessageFloat ( Dmessage, NewObjAngle#)
SendNetworkMessage ( NetworkID, 0, Dmessage )
Full code:
// Project: Test World
// Created: 2015-02-15
// set window properties
SetWindowTitle( "Test World" )
SetWindowSize( 1920, 1080, 0 )
SetDisplayAspect( -1 )
// set display properties
deviceHeight# = GetDeviceHeight()
deviceWidth# = GetDeviceWidth()
`SetVirtualResolution( 1024, 768 )
`SetVirtualResolution( 1920, 1080 )
SetVirtualResolution( deviceWidth#, deviceHeight# )
SetOrientationAllowed( 1, 1, 1, 1 )
SetClearColor(255,255,255)
SetPrintColor(1,1,1, 100)
`SetVirtualJoystickAlpha( 0, 9, 9 )
//Setup camera
SetCameraPosition(1, 0, 2, -30)
SetCameraRotation(1, 0, 0, 0)
//Load images
loadImage(1,"Wooden Figures/bayotex003.png")
LoadImage(2,"lamp/t006.bmp")
LoadImage(1000,"tile17.png")
LoadImage(3,"tree.jpg")
//Make player objects
FOR t = 1 to 10
LoadObject(t,"Wooden Figures/bayo.obj")
SetObjectImage(t,1,0)
SetObjectLightMode(t,1)
SetObjectScale(t, 0.5, 0.5, 0.5)
`SetObjectPosition(t, 100,0,0)
NEXT t
//Players position array
DIM PlayerVar[10,10]
PlayerVar[1,1] = 0
`CreatePlane
loadObject(1000, "ground.obj")
SetObjectScale(1000, 4,4,4)
`RotateObjectLocalX(2,90)
setImageWrapU(1000,1) : setImageWrapV(1000,1)
SetObjectImage( 1000, 1000, 0 )
//Make tree
LoadObject(1001, "leaves tree/tree1.obj")
SetObjectScale(1001,7,7,7)
SetObjectLightMode(1001,1)
SetObjectImage(1001,1,0)
SetObjectPosition(1001, 6, 0, -20)
//Make background plane
CreateObjectPlane(1002, 60, 60)
SetObjectImage(1002,3,0)
SetObjectPosition(1002,0, 7.5, 0)
// set network variables
State = 0
iType = 0
NetworkID = 0
GLOBAL FPSCounter = 0
// create sprite 11 (the host)
CreateSprite ( 11, 1 )
SetSpritePosition ( 11, 100, 100 )
SetSpriteSize ( 11, 100, -1 )
// create sprite 12 (the joiner)
CreateSprite ( 12, 2 )
SetSpritePosition ( 12, 500, 100 )
SetSpriteSize ( 12, 100, -1 )
`#############################################################################################################################################################################################
`#############################################################################################################################################################################################
DO
IF FPSCounter > 60 THEN FPSCounter = 1
FPSCounter = FPSCounter + 1
IF (IsNetworkActive ( NetworkID ) = 1)
MyID = GetNetworkMyClientID(networkID)
NewObjectX# = GetObjectX(MyID) + getJoystickX()/10 `get new object position
NewObjectY# = GetObjectY(MyID)
NewObjectZ# = GetObjectZ(MyID) - getJoystickY()/10
DistX# = NewObjectX# - GetObjectX(MyID) `Calculate object rotation
DistZ# = NewObjectZ# - GetObjectZ(MyID)
NewObjAngle# = ATANFULL(DistX#, DistZ#)
SetObjectPosition(MyID, NewObjectX#, NewObjectY#, NewObjectZ#)
IF (getJoystickX() <> 0) or (getJoystickY() <> 0) `rotate player of joystick pressed
SetObjectRotation(MyID, 0, -NewObjAngle#, 0)
ENDIF
ENDIF
IF iType = 0
Print( ScreenFPS() )
Print(getJoystickX())
Print(getJoystickY())
print("networkID : " +STR(networkId))
print ("My IP :" + GetDeviceIP())
print ("My ID :" + STR(GetNetworkMyClientID(networkID) ))
print ("FPSCounter: " + STR(FPSCounter))
ENDIF
`----------------------------------------------------------------------------------------------------------------------------------------
// initial state of game - host or join game
if State = 0
// display instructions
Print ( "Select green square to host a game" )
Print ( "Select red square to join a game" )
// check input
if GetPointerPressed ( ) = 1
// find out which sprite was hit
hit = GetSpriteHit ( GetPointerX ( ), GetPointerY ( ) )
// green sprite - host game
if ( hit = 11 )
NetworkID = HostNetwork ( "AGK Test Game", "Player 1", 1025 )
iType = 0
State = 1
`SetObjectPosition(GetNetworkMyClientID(networkID), 0,0,0)
//Hide sprites
`SetVirtualJoystickAlpha( 1, 1, 1 )
SetSpriteVisible(11,0)
SetSpriteVisible(12,0)
SetSpriteActive(11,0)
SetSpriteActive(12,0)
endif
// red sprite - join game
if ( hit = 12 )
`randomplayername$="player "+str(random(1,10000))
randomplayername$="player "+str(random(2,100))
NetworkID = JoinNetwork ( "AGK Test Game", randomplayername$ )
iType = 1
State = 1
`SetObjectPosition(GetNetworkMyClientID(NetworkID), 0,0,0)
//Hide sprites
`SetVirtualJoystickAlpha( 1, 1, 1 )
SetSpriteVisible(11,0)
SetSpriteVisible(12,0)
SetSpriteActive(11,0)
SetSpriteActive(12,0)
endif
endif
endif
`---------------------------------------------------------------------------------------------------
// in game
if State = 1 and NetworkID = 0 then print ("Network connection failed")
IF State = 1 and NetworkID > 0
`network status
printc ("Network active:")
printc (IsNetworkActive ( NetworkID ))
for activity=1 to activitycount
printc(".")
next activity
print("")
inc activitycount
if activitycount>10 then activitycount=0
rem if active
if IsNetworkActive ( NetworkID ) <> 0
// get ID of first player
REMSTART
id = GetNetworkFirstClient ( NetworkID )
// cycle through all players and display their names on screen
while id <> 0
// show current player
Print ( GetNetworkClientName ( NetworkID, id ) + " ID: " + STR(id) )
// find next player
id = GetNetworkNextClient ( NetworkID )
endwhile
REMEND
`---------------------------------------------------------------------------------------------------
IF (getJoystickX() <> 0) or (getJoystickY() <> 0) `Send position data if joystick pressed
IF FPSCounter = 10 OR FPSCounter = 20 OR FPSCounter = 30 OR FPSCounter = 40 OR FPSCounter = 50 OR FPSCounter = 60
`Send position data
cmessage = CreateNetworkMessage ( )
AddNetworkMessageFloat ( cmessage, GetNetworkMyClientID(networkID) )
AddNetworkMessageFloat ( cmessage, NewObjectX# )
AddNetworkMessageFloat ( cmessage, NewObjectZ# )
AddNetworkMessageFloat ( cmessage, NewObjAngle#)
SendNetworkMessage ( NetworkID, 0, cmessage )
ENDIF
ENDIF
`---------------------------------------------------------------------------------------------------
// find message from server
cmessage = GetNetworkMessage ( NetworkID ) `Get message ID
IF cmessage <> 0
If iType = 0 `If host, receive locations of individual players, and send to all players
`OtherNum = GetNetworkMessageFromClient( cmessage ) `Get player number that sent this message
`Print ( GetNetworkClientName ( NetworkID, OtherNum ) + " ID: " + STR(OtherNum) )
// cycle through all messages, Position players
// get position
messageFrom = GetNetworkMessageFloat ( cmessage )
otherX# = GetNetworkMessageFloat ( cmessage )
otherZ# = GetNetworkMessageFloat ( cmessage )
OtherNewObjAngle# = GetNetworkMessageFloat ( cmessage )
`Register player positions
`PlayerVar[messageFrom,1] = messageFrom
`PlayerVar[messageFrom,2] = otherX#
`PlayerVar[messageFrom,3] = otherZ#
`Send this player position to all players
Dmessage = CreateNetworkMessage ( )
AddNetworkMessageFloat ( Dmessage, messageFrom )
AddNetworkMessageFloat ( Dmessage, otherX#)
AddNetworkMessageFloat ( Dmessage, otherZ# )
AddNetworkMessageFloat ( Dmessage, NewObjAngle#)
SendNetworkMessage ( NetworkID, 0, Dmessage )
// update players position
IF messageFrom = MyID
ELSE
SetObjectPosition ( messageFrom, otherX#, GetObjectY(messageFrom), otherZ# )
SetObjectRotation(messageFrom, 0, -OtherNewObjAngle#, 0)
ENDIF
// delete the current message
DeleteNetworkMessage ( cmessage )
// find the next message
`cmessage = GetNetworkMessage ( NetworkID )
ENDIF `If iType = 0
IF iType = 1 `If client, receive locations of all players from host
`For z = 1 to 10
// get position
messageFrom = GetNetworkMessageFloat ( cmessage )
otherX# = GetNetworkMessageFloat ( cmessage )
otherZ# = GetNetworkMessageFloat ( cmessage )
OtherNewObjAngle# = GetNetworkMessageFloat ( cmessage )
`Next z
// update players position
IF messageFrom = MyID
ELSE
SetObjectPosition ( messageFrom, otherX#, GetObjectY(messageFrom), otherZ# )
SetObjectRotation(messageFrom, 0, -OtherNewObjAngle#, 0)
ENDIF
// delete the current message
DeleteNetworkMessage ( cmessage )
// find the next message
`cmessage = GetNetworkMessage ( NetworkID )
ENDIF `IF iType = 1
ENDIF `IF cmessage <> 0
ENDIF `if IsNetworkActive ( NetworkID ) <> 0
endif `IF State = 1 and NetworkID > 0
`---------------------------------------------------------------------------------------------------
SYNC()
LOOP
`#############################################################################################################################################################################################
`#############################################################################################################################################################################################