VB.Net Event Handling - Mini Tutorial

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Aire
Posts: 9
Joined: Tue Mar 15, 2005 3:44 pm

VB.Net Event Handling - Mini Tutorial

Post by Aire »

Since there seems to be a lack of .Net tutorials I thought I would contribute this just for those people out ther who wish to use it as a point of reference. Primarily those not extremely well versed in Visual Basic .Net but wish to use Irrlicht's .Net features.

Create a new module project in Visual Basic .Net and then paste this code into your empty source file. Make sure to reference irrlicht.Net.dll

This program should execute and show a white box at an angle, by pressing any key a VB message box will pop up with the id of the key.

Code: Select all

Imports Irrlicht
Imports Irrlicht.Video
Imports Irrlicht.Core
Imports Irrlicht.Scene

Module Module1
    Sub main()
        Dim device As New IrrlichtDevice(Irrlicht.Video.DriverType.OPENGL) ' Start the Engine

        ''' Setup Event Receiver '''
        device.EventReceiver = New MyEventReceiver

        ''' Window Settings '''
        device.ResizeAble = True
        device.WindowCaption = "Event Test"

        ''' Setup Main Camera '''
        Dim cam As ICameraSceneNode = device.SceneManager.AddCameraSceneNode(Nothing, New Vector3D(0, 10, 0), New Vector3D(5, 0, 0), -1)

        ''' Create Simple Block Node '''
        Dim node As ISceneNode = device.SceneManager.AddTestSceneNode(15, Nothing, -1, New Vector3D(30, -15, 0))
        node.Scale = New Vector3D(0.5, 0.5, 0.5)

        ''' FPS '''
        Dim fps As Integer = 0

        While device.Run() = True
            If device.WindowActive Then

                device.VideoDriver.BeginScene(True, True, New Color(0, 100, 100, 100))

                device.SceneManager.DrawAll()

                device.VideoDriver.EndScene()

                ''' Update the FPS Counter in the Window Bar '''
                If (fps <> device.VideoDriver.FPS) Then
                    fps = device.VideoDriver.FPS
                    device.WindowCaption = "Event Test [" + device.VideoDriver.Name + "] fps:" + fps.ToString
                End If

            End If
        End While

    End Sub

End Module

Public Class MyEventReceiver
    Implements IEventReceiver
    Public Function OnEvent(ByVal e As [Event]) As Boolean Implements IEventReceiver.OnEvent
        If e.Type = EventType.KeyInput Then
            MsgBox(e.Key())
            Return True
        End If
        Return False
    End Function
End Class
Engan

Testing some more with events in vb.net

Post by Engan »

Thanks Aire the example was great for getting started.
I did this little tweak to make inte easy to handle the events in the main loop. I don't know if it is a good solution but I didn't want to make all my
objects public now only global object is the collection for the events.

Code: Select all

Public events As New Collection()
Sub Main()
      'Standard code for initializing
      device.EventReceiver = New MyEventReceiver()

      While device.Run() = True
            If device.WindowActive Then
                If events.Count >= 1 Then
                    Dim e As [Event]
                    e = events(1)
                    events.Remove(1)
                    If e.Type = EventType.KeyInput Then MsgBox(e.Key()) 
                End If
                'Standard code for rendering
            End If
      Wend
End Sub

Public Class MyEventReceiver
        Implements IEventReceiver
        Public Function OnEvent(ByVal e As [Event]) As Boolean Implements IEventReceiver.OnEvent
            If e.Type = EventType.KeyInput Or e.Type = EventType.MouseInput Then
                events.Add(e)
                Return True
            End If
            Return False
        End Function
    End Class
WalterWzK
Posts: 72
Joined: Wed Jan 12, 2005 2:57 pm
Location: Netherlands
Contact:

Post by WalterWzK »

Nice work both, altough i already had implemented the first post.
This works and is great, had to wait 3 months for it...

Now waiting for text and gui in VB.Net lol
Current Project: Don Salvatore's Mafia
Genre: 3D Shooter \ RTS
Programming Language: VB .NET
Engine: Irrlicht 11 [.NET Wrapper]
Aire
Posts: 9
Joined: Tue Mar 15, 2005 3:44 pm

Post by Aire »

nice work, I will be posting a bit of a tutorial/sample code on how to best handle multiple event sources in an actual game. Just working on tidying it up a bit first.
Post Reply