Posted: 21st Oct 2021 8:06
Hi everybody.
After a few weeks of tutorials, I finally made the simplest game possible to test multiplayer.
It consists of a red ball (host) and a green ball (client). They connect fine when picked from computer 1 and 2,
but when I move the red one, it only moves on the hosts screen, and vice versa with the green...
How do I tell the host to update on that computer and then send the location to the client?
Here's the code I have so far...
+ Code Snippet
SetDisplayAspect ( 4.0 / 3.0 )


LoadImage ( 1, "red.png" )
LoadImage ( 2, "green.png" )


CreateSprite ( 1, 1 )
SetSpritePosition ( 1, 10, 20 )
SetSpriteSize ( 1, 10, -1 )


CreateSprite ( 2, 2 )
SetSpritePosition ( 2, 50, 20 )
SetSpriteSize ( 2, 10, -1 )


NetworkID = 0
State = 0
type myType
        a as integer
        b as string
endtype


data as myType
data.a = 0


do
    if State = 0
        Print ( "Select Red ball to host a game" )
        Print ( "Select Green ball to join a game" )
        

        if GetPointerPressed ( ) = 1
            hit = GetSpriteHit ( GetPointerX ( ), GetPointerY ( ) )
        

            if ( hit = 1 )
                NetworkID = HostNetwork ( "AGK Test Game", "Player 1", 44441 )
                myType = data.a
            endif
        

            if ( hit = 2 )
                NetworkID = JoinNetwork ( "AGK Test Game", "Player 2" )
                myType = 1
            endif
            State = 1
        endif
    endif


    if State = 1 and IsNetworkActive ( NetworkID ) <> 0
        id = GetNetworkFirstClient ( NetworkID )
        

        while id <> 0
            Print ( GetNetworkClientName ( NetworkID, id ) )
        

            id = GetNetworkNextClient ( NetworkID )
        endwhile
        

        if myType = data.a
            x# = GetSpriteX ( 1 ) + GetDirectionX ( )
            y# = GetSpriteY ( 1 ) + GetDirectionY ( )
        

            SetSpritePosition ( 1, x#, y# )
        

            message = CreateNetworkMessage ( )
            

            AddNetworkMessageFloat ( message, x# )
            AddNetworkMessageFloat ( message, y# )
            

            SendNetworkMessage ( NetworkID, 0, message )
        endif
        

        if myType = 1
			x# = GetSpriteX ( 2 ) + GetDirectionX ( )
            y# = GetSpriteY ( 2 ) + GetDirectionY ( )
            message = GetNetworkMessage ( NetworkID )
            
            SetSpritePosition ( 2, x#, y# )
            

            while message <> 0 
                x# = GetNetworkMessageFloat ( message )
                y# = GetNetworkMessageFloat ( message )
                

                

                DeleteNetworkMessage ( message )
                

                message = GetNetworkMessage ( NetworkID )
            endwhile
        endif
    endif


    Sync ( )
loop


If you want to test it, you need the media-folder in the project, It can be found, zipped, below.

Best Regards
-Jonas
Posted: 21st Oct 2021 9:57
I done away with the media easy to add back in and a removed the type as you was doing different things with it but again should be able to add it back in replace it with two variables.

+ Code Snippet
SetDisplayAspect ( 4.0 / 3.0 )

CreateSprite ( 1, 0)
SetSpritePosition ( 1, 10, 20 )
SetSpriteSize ( 1, 10, -1 )
SetSpriteColor(1, 255, 0, 0, 255)

CreateSprite ( 2, 0)
SetSpritePosition ( 2, 50, 20 )
SetSpriteSize ( 2, 10, -1 )
SetSpriteColor(2, 0, 255, 0, 255)

Global NetworkID As Integer = 0
Global State As Integer = 0

Global iAmHost As Integer = 0
Global iAmClient As Integer = 0

do
    if State = 0
        Print ( "Select Red ball to host a game" )
        Print ( "Select Green ball to join a game" )
        
        if GetPointerPressed ( ) = 1
            hit = GetSpriteHit ( GetPointerX ( ), GetPointerY ( ) )

            if ( hit = 1 )
                NetworkID = HostNetwork ( "AGK Test Game", "Player 1", 44441 )
                iAmHost = 1
            Elseif ( hit = 2 )
                NetworkID = JoinNetwork ( "AGK Test Game", "Player 2" )
                iAmClient = 1
            endif
            State = 1
        endif
    endif

    if State = 1 and IsNetworkActive ( NetworkID ) <> 0
        id = GetNetworkFirstClient ( NetworkID )
        
        while id <> 0
            Print ( GetNetworkClientName ( NetworkID, id ) )
        
            id = GetNetworkNextClient ( NetworkID )
        endwhile
        
        if iAmHost = 1
            x1# = GetSpriteX ( 1 ) + GetDirectionX ( )
            y1# = GetSpriteY ( 1 ) + GetDirectionY ( )
        
            SetSpritePosition ( 1, x1#, y1# )
        
            messageID = CreateNetworkMessage ( )
            
            AddNetworkMessageFloat ( messageID, x1# )
            AddNetworkMessageFloat ( messageID, y1# )
            
            SendNetworkMessage ( NetworkID, 0, messageID )

        Elseif iAmClient = 1
			x2# = GetSpriteX ( 2 ) + GetDirectionX ( )
            y2# = GetSpriteY ( 2 ) + GetDirectionY ( )
            SetSpritePosition ( 2, x2#, y2# )
             
            messageID = GetNetworkMessage ( NetworkID )
   
            while messageID <> 0 
                x1# = GetNetworkMessageFloat ( messageID )
                y1# = GetNetworkMessageFloat ( messageID )
                
				SetSpritePosition ( 1, x1#, y1# )

                DeleteNetworkMessage ( messageID )
                
                messageID = GetNetworkMessage ( NetworkID )
            endwhile

        endif
    endif

    Sync ( )
loop


The main thing you was doing wrong was while sending the data you wasn't using it to position the sprite, you were only recording it in to x# and y# and then deleting it.

I'm also using different variable names for the sprite position, so it is more apparent what each sprite is using for its position.

The green sprite still won't move on the host, but with the info above, you should be able to work it out.