Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Tom

Visual Basic 6 - ByRef not working

This topic is 5634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s an odd scenario. I''m making a four-player puzzle game for Windows using VB6 and DX8. The engine is all but finished -- I''m working on mechanics at the moment, and that incidentally is where all my problems arose. I''ve got two crucial procedures that rely on altering data in variables passed ByRef. One of them animates my sprites; the other processes joypad input. Both of them are working -- at least, they work as long as I''m not dropping blocks onto the board. For some reason, when I call these procedures within the procedure that handles the focus block (i.e., that which is presently under the player''s control), ByRef stops working. Here''s the procedure:
  
Private Sub ProcessFocusPair(ByVal FieldNum As Long)

''   This is where we control the focus block -- the pair of blocks that the player
''   is presently controlling.

    Dim fmX As Long, fmY As Long, fsX As Long, fsY As Long
    
    With Field(FieldNum).FocusBlock

''   Process gravity:

        .yOffset = .yOffset + MasterGravity
        If .yOffset > 31 Then .yOffset = 0
        
''   Calculate block positions (relative to field grid):
    
        fmX = (.Master.Verts(0).X - (FieldNum * 256) - xOffset - 32) \ 32
        fmY = (.Master.Verts(0).Y - 255) \ 32
        fsX = (.Slave.Verts(0).X - (FieldNum * 256) - xOffset - 32) \ 32
        fsY = (.Slave.Verts(0).Y - 255) \ 32
    
    Debug.Print "fmX: " & Str$(fmX) & ", fmY: " & Str$(fmY) & ", fsX: " & Str$(fsX) & ", fsY: " & Str$(fsY)
    Debug.Print "yOffset: " & Str$(Field(FieldNum).FocusBlock.yOffset)
    
''   The next thing we do is apply the player''s input (if any). We''ll go through and
''   check each button state to see if it''s greater than zero. If so, then the player
''   is pressing the button, so we need to act on that.
''
''   Here''s how it works:  the precise time at which input is received is stored in
''   the DevState array. When we process this event, we add 0.25 seconds to the
''   button state so it isn''t re-processed too quickly. So if a player holds down
''   a button, it will be re-processed every quarter of a second. When he lets go of
''   the button, the timer is reset to zero.

''   Process joystick input (if any):

1:      If devState(FieldNum, BTN_RotL) > 0 Then Call RotateLeft(FieldNum, fmX, fmY, fsX, fsY): DoEvents
        ''If devState(FieldNum, btn_rotr) > 0 Then GoSub RotateRight
        ''If devState(FieldNum, btn_swvic) > 0 Then GoSub SwitchVictim
        ''If devState(FieldNum, btn_movel) > 0 Then GoSub MoveLeft
        ''If devState(FieldNum, btn_mover) > 0 Then GoSub MoveRight
        ''If devState(FieldNum, btn_moveu) > 0 Then GoSub MoveUp
        ''If devState(FieldNum, btn_moved) > 0 Then GoSub MoveDown
        
''   Field collision detection against the focus pair is tested here.

        If (fmY = 13) Or (fsY = 13) Then            ''<- If master or slave is in 13th row
        
            Field(FieldNum).Blocks(fmX, fmY) = CreateBlock(FieldNum, fmX, fmY, .Master.BColor, .Master.BType, BS_Falling)
            Field(FieldNum).Blocks(fsX, fsY) = CreateBlock(FieldNum, fsX, fsY, .Slave.BColor, .Slave.BType, BS_Falling)
        
            Call GK_Mechanics.ProcessField(FieldNum)        ''<- Call field processing
            Field(FieldNum).NewMaster = False
                
        ElseIf Field(FieldNum).Blocks(fmX, fmY + 1).BState > BS_NotExist Then ''<- If space below master is filled
            
            Field(FieldNum).Blocks(fmX, fmY) = CreateBlock(FieldNum, fmX, fmY, .Master.BColor, .Master.BType, BS_Falling)
            Field(FieldNum).Blocks(fsX, fsY) = CreateBlock(FieldNum, fsX, fsY, .Slave.BColor, .Slave.BType, BS_Falling)
        
            Call GK_Mechanics.ProcessField(FieldNum)        ''<- Call field processing
            Field(FieldNum).NewMaster = False
            
        ElseIf Field(FieldNum).Blocks(fsX, fsY + 1).BState > BS_NotExist Then  ''<- If space below slave is filled
        
            Field(FieldNum).Blocks(fmX, fmY) = .Master      ''<- Drop master into field
            Field(FieldNum).Blocks(fsX, fsY) = .Slave       ''<- Drop slave into field
            
            Call GK_Mechanics.ProcessField(FieldNum)        ''<- Call field processing
            Field(FieldNum).NewMaster = False
            
''   If there was no collision, we keep moving the pair down.

        Else
            .Master = CreateBlock(FieldNum, fmX, fmY, .Master.BColor, .Master.BType, .Master.BState, .yOffset)
            .Slave = CreateBlock(FieldNum, fsX, fsY, .Slave.BColor, .Slave.BType, .Slave.BState, .yOffset)
            .Outline = CreateBlock(FieldNum, fmX, fmY, .Master.BColor, BT_Outline, .Master.BState, .yOffset)
        End If
        
2:      Call GK_Mechanics.ProcessAnimation(.Master): DoEvents
3:      Call GK_Mechanics.ProcessAnimation(.Slave): DoEvents
        
    End With
    
End Sub
  
I''ve numbered the lines that aren''t being executed properly. Basically, the parameters in these calls are passed ByRef, but values are not being returned. There is one exception: if I set breakpoints in the procedures, they are executed properly. I''m hoping someone has at least some idea of what''s gonig on here, because I certainly don''t. Thanks very much for your time. I appreciate any feedback that might help resolve the problem.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
this might be a dumb q, but....

have you inserted byref in the function ?

public sub ProcessAnimation(Byref x as myclass)?

Share this post


Link to post
Share on other sites
Is it possible that DoEvents is causing your application to perform an operation that hoses those procedures? It could be another function that calls the same procedures, or even a problem with ProcessFocusPair being called while another instance of it is still running. That could explain why it appears to work when you put a breakpoint in it: the breakpoint would keep DoEvents from causing something else to scribble on your values.

Share this post


Link to post
Share on other sites
Why the devil are you using DoEvents so much?

DoEvents should be called once per "frame", preferably immediately after you transfer the current field to the screen (whether by backbuffer blit or swap, or by updates of controls)

Placing DoEvents anywhere else is a very poor way to ''hack'' something into proper timing, and even then it will depend on processor speed. As you should know, DoEvents essentially tells Windows to your program''s process, send it to the back of the list, and let everything else run for a few moments. Without DoEvents, the VB runtimes actually have exclusive control over the processor until VB returns to an "idle" (no events running, no code moving, just waiting for something to happen) state.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!