How to go about this...

Satiex

New member
Joined
Mar 18, 2008
Messages
3
Programming Experience
1-3
Hello


I am programming a timer for a game (Halo)
What needs to happen, is you select the level you want from the dropdown/combo box.
This will set the values of 4 timers, 1 for the Sniper Rifle, one for the Rocket Launcher, one for the Active-Camo and one for the Over-Shield.

I coded a Private Sub that Dim's 4 counters to a specified value depending on the SelectedIndex (the Map that is selected) of the group box.

When the Start button is clicked, I've coded the 4 timers to start, but they can't find the starting value set by the I coded in the "Private Sub GetTimes()"

How could I go about fixing this?

Below is the code.
programci3.jpg


VB.NET:
Public Class Form1

    Dim H1SRCount As Integer
    Dim H1RLCount As Integer
    Dim H1OSCount As Integer
    Dim H1ACCount As Integer


#Region " Enumerations "

#End Region

#Region " Structures "


#End Region

#Region " Creation "

    Private Sub frmHaloTimer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadHalo1()
    End Sub

#End Region

#Region " Sub-Routines "

    Private Sub LoadHalo1()
        cbxH1GT.Enabled = False
        btnStart.Enabled = False
        btnReset.Enabled = False
        btnPause.Enabled = False
    End Sub

    Private Sub LoadHalo2()

    End Sub

    Private Sub LoadHalo3()

    End Sub

    Private Sub GetTimes()


        If cbxHalo1Map.SelectedIndex.ToString = "Blood Gluch" Then
            Dim H1ACCount = 180
            Dim H1OSCount = 60
            Dim H1RLCount = 90
            Dim H1SRCount = 120
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Sidewinder" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 120
            Dim H1SRCount = 30
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Battle Creek" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 120
            Dim H1SRCount = 30
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Derilect" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 30
            Dim H1SRCount = 30
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Prisoner" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 120
            Dim H1SRCount = 30
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Chirion TL34" Then
            Dim H1ACCount = 45
            Dim H1OSCount = 45
            Dim H1RLCount = 45
            Dim H1SRCount = 45
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Wizard" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 60
            Dim H1SRCount = 60
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Damnation" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 120
            Dim H1SRCount = 30
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Longest" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 60
            Dim H1SRCount = 60
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Rat Race" Then
            Dim H1ACCount = 90
            Dim H1OSCount = 60
            Dim H1RLCount = 60
            Dim H1SRCount = 60
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Boarding Action" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 180
            Dim H1SRCount = 30
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Hang 'em High" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 180
            Dim H1RLCount = 120
            Dim H1SRCount = 30
        End If

        If cbxHalo1Map.SelectedIndex.ToString = "Chill Out" Then
            Dim H1ACCount = 60
            Dim H1OSCount = 60
            Dim H1RLCount = 120
            Dim H1SRCount = 60
        End If

    End Sub

    Private Sub cbxHalo1Map_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxHalo1Map.SelectedIndexChanged
        'GetTimes()
        btnStart.Enabled = True
        btnReset.Enabled = True
        btnPause.Enabled = True
    End Sub

#End Region

#Region " Function "

    Private Sub timeH1RL_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeH1RL.Tick
        lblH1RLTime.Text = H1RLCount.ToString()
        If H1RLCount = 0 Then
            timeH1RL.Enabled = False
            lblH1RLTime.Text = "Done"
        Else
            H1RLCount -= 1
        End If
    End Sub

    Private Sub timeH1OS_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeH1OS.Tick
        lblH1OSTime.Text = H1OSCount.ToString()
        If H1OSCount = 0 Then
            timeH1OS.Enabled = False
            lblH1OSTime.Text = "Done"
        Else
            H1OSCount -= 1
        End If
    End Sub

    Private Sub timeH1AC_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeH1AC.Tick
        lblH1ACTime.Text = H1ACCount.ToString()
        If H1SRCount = 0 Then
            timeH1AC.Enabled = False
            lblH1ACTime.Text = "Done"
        Else
            H1ACCount -= 1
        End If
    End Sub

    Private Sub timeH1SR_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timeH1SR.Tick
        'GetTimes()
        lblH1SRTime.Text = H1SRCount.ToString()
        If H1SRCount = 0 Then
            timeH1SR.Enabled = False
            lblH1SRTime.Text = "Done"
        Else
            H1SRCount -= 1
        End If

    End Sub

#End Region

#Region " Variables "




#End Region

#Region " Event "

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        GetTimes()
        timeH1AC.Interval = 1000
        timeH1AC.Enabled = True
        timeH1SR.Interval = 1000
        timeH1SR.Enabled = True
        timeH1OS.Interval = 1000
        timeH1OS.Enabled = True
        timeH1RL.Interval = 1000
        timeH1RL.Enabled = True
    End Sub

#End Region

End Class
 
When assigning a value to a variable, you do not need to 'Dim' it.

By Dimming within a function/sub, you create a new variable that only exists within the block.

So try removing the dim statements from your subs / functions, and see how it goes.
 
Also, when looking for one specific value, instead of doing a If / Then for every case, try using Select Case.

VB.NET:
    Private Sub GetTimes()
        Select Case cbxHalo1Map.SelectedItem.ToString

        Case "Blood Gluch"
            H1ACCount = 180
            H1OSCount = 60
            H1RLCount = 90
            H1SRCount = 120
        

        Case "Sidewinder"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 120
            H1SRCount = 30
       

        Case "Battle Creek"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 120
            H1SRCount = 30
        

        Case "Derilect"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 30
            H1SRCount = 30
       

        Case "Prisoner"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 120
            H1SRCount = 30
        

        Case "Chirion TL34"
            H1ACCount = 45
            H1OSCount = 45
            H1RLCount = 45
            H1SRCount = 45
        

        Case "Wizard"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 60
            H1SRCount = 60
       

        Case "Damnation"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 120
            H1SRCount = 30
       

        Case "Longest"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 60
            H1SRCount = 60
        

        Case "Rat Race"
            H1ACCount = 90
            H1OSCount = 60
            H1RLCount = 60
            H1SRCount = 60
       

        Case "Boarding Action"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 180
            H1SRCount = 30
        

        Case "Hang 'em High"
            H1ACCount = 60
            H1OSCount = 180
            H1RLCount = 120
            H1SRCount = 30
        

        Case "Chill Out"
            H1ACCount = 60
            H1OSCount = 60
            H1RLCount = 120
            H1SRCount = 60
        End Select

    End Sub

Please note I have also changed the 'cbxHalo1Map.SelectedIndex.ToString' as that will not give you the item text, it will give you the zero-based index of that item within the combo.
 
Thanks a lot, I've adjusted the code as you've instructed, however it seems the timers still can't find the starting value. The all go straight to "Done".

Should I be calling "GetTimes()" at the SelectedIndexChanged or the btnClick event? Does that matter?
And shall I initially be Dim'ing the counters in the Public Class at the top, or somewhere else?
 
The variables are being dimmed in the correct place.

Put a breakpoint at the end of GetTimes, and see if the variables are being set correctly.

Also, put the following in as a final statement within the select

VB.NET:
Case Else
Msgbox( "Unrecognised value: " & cbxHalo1Map.SelectedItem.ToString)

That way you will see if there is an error that is stopping the variables from being set.
 
Back
Top