Moving objects with UDP

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1646 Views - Last Post: 13 November 2016 - 09:17 AM Rate Topic: -----

#1 CodeofSteele  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-November 16

Moving objects with UDP

Posted 11 November 2016 - 09:35 AM

Sub sendkey
        Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
      	Dim broadcast As IPAddress = IPAddress.Parse(frmmpconfig.ipvar)
      	Dim sendbuf As Byte() = Encoding.ASCII.GetBytes(key)
      	Dim ep As New IPEndPoint(broadcast, listenport)
      	s.SendTo(sendbuf, ep)
      	Console.WriteLine("Message sent to the broadcast address")
    End Sub
    
    Sub receivekey

    	Dim done As Boolean = False
      	Dim listener As New UdpClient(11001)
      	Dim groupEP As New IPEndPoint(IPAddress.Any, listenport)
      	Try
         	While Not done
            	Dim bytes As Byte() = listener.Receive(groupEP)
            	If (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Up" Then
            		paddleComputer.Top -= 50
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Down" Then
            		paddleComputer.Top += 50
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Easy?" Then ' Changing speed to easy 
            		Dim message As String = groupEP.ToString() & " has requested easy speed. Do you accept?" ' Text in the message box
            		Dim result As DialogResult = MessageBox.Show(message, caption, ynbutton) ' Gets the result that the user checks
            		If result = DialogResult.Yes Then ' Changes speed and starts game "anew"
            			speed = 3
            			speedval.Text = "Speed: " & speed
            			Call calculate()
            			gametimer.Enabled = True
            			key = "Easy"
            			Call sendkey()
            		Else	' Says no and just starts game from where it's at 
            			speed = speed			
            			gametimer.Enabled = True
            			key = "No"
            			Call sendkey()
            		End If
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Normal?" Then ' Changing speed to normal 
            		Dim message As String = groupEP.ToString() & " has requested normal speed. Do you accept?" ' Text in the message box
            		Dim result As DialogResult = MessageBox.Show(message, caption, ynbutton) ' Gets the result that the user checks
            		If result = DialogResult.Yes Then ' Changes speed and starts game "anew"
            			speed = 10
            			speedval.Text = "Speed: " & speed
            			Call calculate()
            			gametimer.Enabled = True
            			key = "Normal"
            			Call sendkey()
            		Elseif result = DialogResult.No Then	' Says no and just starts game from where it's at 
            			speed = speed
            			gametimer.Enabled = True
            			key = "No"
            			Call sendkey()
            		End If
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Hard?" Then ' Changing speed to normal 
            		Dim message As String = groupEP.ToString() & " has requested hard speed. Do you accept?" ' Text in the message box
            		Dim result As DialogResult = MessageBox.Show(message, caption, ynbutton) ' Gets the result that the user checks
            		If result = DialogResult.Yes Then ' Changes speed and starts game "anew"
            			speed = 15
            			speedval.Text = "Speed: " & speed
            			Call calculate()
            			gametimer.Enabled = True
            			key = "Hard"
            			Call sendkey()
            		Elseif result = DialogResult.No Then	' Says no and just starts game from where it's at 
            			speed = speed
            			gametimer.Enabled = True
            			key = "No"
            			call sendkey()
            		End If
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Reset defaults?"
            		Dim message As String = groupEP.ToString() & " has requested hard speed. Do you accept?" ' Text in the message box
            		Dim result As DialogResult = MessageBox.Show(message, caption, ynbutton) ' Gets the result that the user checks
            		If result = DialogResult.Yes Then ' Gets the result
            			speed = 10
            			speedval.Text = "Speed: " & speed
            			Call calculate()
            			gametimer.Enabled = True
            			key = "Reset"
            			Call sendkey()
            		Elseif result = DialogResult.No Then ' Says no and starts the game from where it's at
            			speed = speed
            			gametimer.Enabled = True
            			key = "No"
            			Call sendkey()
            		End If
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Easy" Then
            		speed = 3
            		speedval.Text = "Speed: " & speed
            			Call calculate()
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Normal" Then
            		speed = 10
            		speedval.Text = "Speed: " & speed
            			Call calculate()
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Hard" Then
            		speed = 15
            		speedval.Text = "Speed: " & speed
            			Call calculate()
            	ElseIf (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "Reset" Then
            		speed = 10
            		speedval.Text = "Speed: " & speed
            			Call calculate()
         		End If
         	End While
      	Catch e As Exception

         	Console.WriteLine(e.ToString())
      	Finally
         	listener.Close()
         	Call receivekey()
      	End Try

      	'Receives the key, and depending on what is recieved (what keypress was sent and which paddle it needs to go to) moves the other paddle.

    End Sub
    #End Region


Above, what I am trying to is receieve a message, "Up" or "Down" (these are the ones that don't work), and then move a picture box, aka the computerPaddle, when it receives the message. However, when it receives the message, it won't move the picture box. I've tried testing with message boxes and they always show up. I'm not sure of what could be stopping the paddle from moving. I've dived deep into MSDN and their social site, I've tried StackOverflow, but none have helped at all. Thanks for any help that comes my way!

Is This A Good Question/Topic? 1
  • +

Replies To: Moving objects with UDP

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13661
  • View blog
  • Posts: 54,531
  • Joined: 12-June 08

Re: Moving objects with UDP

Posted 11 November 2016 - 09:42 AM

Have you put a breakpoint there to verify the picture box is refreshing?

Additionally - do you have a refresh to redraw your picture box after the move?
Was This Post Helpful? 1
  • +
  • -

#3 CodeofSteele  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-November 16

Re: Moving objects with UDP

Posted 11 November 2016 - 09:45 AM

View Postmodi123_1, on 11 November 2016 - 09:42 AM, said:

Have you put a breakpoint there to verify the picture box is refreshing?

Additionally - do you have a refresh to redraw your picture box after the move?

Ni I am not, how would I go about doing that? I've never used breakpoints in my life :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13661
  • View blog
  • Posts: 54,531
  • Joined: 12-June 08

Re: Moving objects with UDP

Posted 11 November 2016 - 09:58 AM

There's a whole tutorial on the topic.

http://www.dreaminco...4249-debugging/
http://www.dreaminco...ugging-express/
Was This Post Helpful? 2
  • +
  • -

#5 CodeofSteele  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-November 16

Re: Moving objects with UDP

Posted 11 November 2016 - 11:00 AM

View Postmodi123_1, on 11 November 2016 - 09:58 AM, said:


I took a look at the articles and I don't really understand what is going on. Also, I am not using Visual Studio, I am using Sharp Develop. Could you possibly help me understand what is going on?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13661
  • View blog
  • Posts: 54,531
  • Joined: 12-June 08

Re: Moving objects with UDP

Posted 11 November 2016 - 12:36 PM

I don't know 'sharp develop'. Visual Studios is typically the way to go. Though searching 'sharp develop breakpoints' pulls up quite a bit of stuff.

http://www.informit....691504&seqNum=4

You would want to add a break point about where you get a command back so the code stops and you can step through the lines to see where your logic is going (if it is going anywhere).. If the picture boxes are being updated in their position, but not showing graphically, then you need to think where to put a refresh on the form.
Was This Post Helpful? 2
  • +
  • -

#7 Gaveroid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-November 16

Re: Moving objects with UDP

Posted 11 November 2016 - 05:41 PM

View Postmodi123_1, on 11 November 2016 - 12:36 PM, said:

I don't know 'sharp develop'. Visual Studios is typically the way to go. Though searching 'sharp develop breakpoints' pulls up quite a bit of stuff.

http://www.informit....691504&seqNum=4

You would want to add a break point about where you get a command back so the code stops and you can step through the lines to see where your logic is going (if it is going anywhere).. If the picture boxes are being updated in their position, but not showing graphically, then you need to think where to put a refresh on the form.


I'm working on this with him (butting for a minute in with his blessing :wink: ) and tried your suggestion of breakpoints. I put a breakpoint on what is like 19 in his code snippet. It gets there and breaks the application like it should, but it just doesn't do anything! The exact same code is in a keypress sub elsewhere in our application and works flawlessly every time. :/

SharpDevelop is an open source Visual Studio clone / alternative. Its latest version supports only C#, but earlier versions support loads of stuff - C#, VB .NET, C++, etc. It works 99% the same as Visual Studio does - we were looking for a version to install on our personal machines and Visual Studio's free offerings make our hard drives want to take themselves "out to pasture" in terms of space in comparison to the, what, 30 or 60 megs that SharpDevelop takes up for 99% the same functionality for our purposes. It also has one killer feature that as far as I know Visual Studio lacks (not that we're using it at the moment but still) - conversion of code between C# and VB .NET. Pretty much any Visual Studio tutorial will translate right over to it, its' differences are very minor in our experiences.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13661
  • View blog
  • Posts: 54,531
  • Joined: 12-June 08

Re: Moving objects with UDP

Posted 11 November 2016 - 05:47 PM

Okey dokey. If you are seeing the object's x/y change, but not seeing it in the GUI then you need to have the GUI refresh.
Was This Post Helpful? 1
  • +
  • -

#9 Gaveroid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-November 16

Re: Moving objects with UDP

Posted 11 November 2016 - 05:50 PM

View Postmodi123_1, on 11 November 2016 - 05:47 PM, said:

Okey dokey. If you are seeing the object's x/y change, but not seeing it in the GUI then you need to have the GUI refresh.


I just tried using a timer to refresh a status bar label every millisecond with the latest value of the top property, and even though the breakpoint still goes if it's enabled on that line (so it's getting to that line at least definitely), the top property's value isn't changing... so it's getting to that line but it doesn't seem to be running at all. It's not like the application hangs or becomes unresponsive (for me at least) either, it just doesn't do it and moves on with its day. :(
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13661
  • View blog
  • Posts: 54,531
  • Joined: 12-June 08

Re: Moving objects with UDP

Posted 11 November 2016 - 05:59 PM

I am not following the need for a timer - is that your simulated "game engine" tick where you do an update and then draw everything? IF so a general refresh should be in there.

Example:
http://www.dreaminco...-basics-part-1/
Was This Post Helpful? 1
  • +
  • -

#11 CodeofSteele  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-November 16

Re: Moving objects with UDP

Posted 11 November 2016 - 06:06 PM

View Postmodi123_1, on 11 November 2016 - 05:59 PM, said:

I am not following the need for a timer - is that your simulated "game engine" tick where you do an update and then draw everything? IF so a general refresh should be in there.

Example:
http://www.dreaminco...-basics-part-1/

What Gaveroid was saying is that we are using a timer to refresh a status bar label with the current top position of the picture box. The timer doesn't refresh the object at all. However, that line of code isn't executing at all. It breaks on a breakpoint, but it does not change the top property of the picture box.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13661
  • View blog
  • Posts: 54,531
  • Joined: 12-June 08

Re: Moving objects with UDP

Posted 11 November 2016 - 06:11 PM

Can you watch the values of this picture box as you debug? In a 'watch' window or what ever is equivalent in this IDE? Step through and see if the actual values change?
Was This Post Helpful? 1
  • +
  • -

#13 Gaveroid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-November 16

Re: Moving objects with UDP

Posted 11 November 2016 - 06:42 PM

View Postmodi123_1, on 11 November 2016 - 06:11 PM, said:

Can you watch the values of this picture box as you debug? In a 'watch' window or what ever is equivalent in this IDE? Step through and see if the actual values change?


SharpDevelop is actually missing a watch menu but I can confirm that it does refresh properly. I have manual keys set up to run paddlecomputer -= 50 and paddlecomputer += 50 and they work properly, with the top refreshing in the label box fine. The same lines of code in the UDP if statements, even though that with breakpoints it's getting to that code, do not visually change the position or the top property's value, where the exact same lines of code in a keypress sub with if e.keycode = keys.i for example works flawlessly.

The issue is, in this sub the line of code runs fine when I hit I:
If e.KeyCode = keys.I Then
        	paddlecomputer.Top -= 50
        End If


But in this sub, the same line of code, even though I can tell with breakpoints it is getting to that line, does not work:
If (Encoding.ASCII.GetString(bytes, 0, bytes.Length)) = "up" Then
            		paddlecomputer.Top -= 50
            	End If


I don't know why, but in that specific it statement, that same line of code gets hung up. If I put msgbox("test") before it, it throws up the message box, but afterwards, it doesn't. The same line of code, paddlecomputer -= 50, works fine in one sub but it hangs up that statement in another sub. The program keeps running otherwise but any code in that statement after it doesn't run.
Was This Post Helpful? 0
  • +
  • -

#14 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1464
  • View blog
  • Posts: 3,713
  • Joined: 01-February 13

Re: Moving objects with UDP

Posted 11 November 2016 - 06:55 PM

Did you try as modi123_1 has suggested and called the Refresh method of the PictureBox? You might even need to call the Refresh method of the Form itself. For example, in the receivekey sub, each time you set the Text or Location of a control, force it to update. You are doing all this in a loop which may not be giving the Form itself time to process any messages.
        paddleComputer.Top -= 50
        paddleComputer.Refresh 'force the PictureBox to repaint itself



Or you might need to make the form refresh itself and all of its controls...
        paddleComputer.Top -= 50
        Me.Refresh() 'force the Form to repaint itself and all of its controls



As a side note, if you are running any of this in a separate thread than the Form, you will need to use special methods to access any controls on the Form.
Was This Post Helpful? 1
  • +
  • -

#15 CodeofSteele  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 11-November 16

Re: Moving objects with UDP

Posted 11 November 2016 - 07:02 PM

View PostIronRazer, on 11 November 2016 - 06:55 PM, said:

Did you try as modi123_1 has suggested and called the Refresh method of the PictureBox? You might even need to call the Refresh method of the Form itself. For example, in the receivekey sub, each time you set the Text or Location of a control, force it to update. You are doing all this in a loop which may not be giving the Form itself time to process any messages.
        paddleComputer.Top -= 50
        paddleComputer.Refresh 'force the PictureBox to repaint itself



Or you might need to make the form refresh itself and all of its controls...
        paddleComputer.Top -= 50
        Me.Refresh() 'force the Form to repaint itself and all of its controls



As a side note, if you are running any of this in a separate thread than the Form, you will need to use special methods to access any controls on the Form.

Well I tried the .refresh method already, but I'll try the second one too. However, it is running on a separate thread to the form once MP is enabled.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2