Q's about saving, modules, and multiple forms. (VB6)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jsnova864
    New Member
    • Apr 2007
    • 12

    Q's about saving, modules, and multiple forms. (VB6)

    Hi, I got a few simple q's, I think. I'm making a pretty simple RPG but my program is getting too big for me to handle. I’m trying to figure out how to use modules to organize it and I figured out how to create one, but how would I import it so I could use it? I also would like to know how to setup something to save a game (and load), just to be able to save some variable values or something so I don’t start at level one every time. This also brings me to the process of saving and loading. I figured out how to make a 2nd form but how do I switch between them so I can use one for the actual field which you play on and the other as a menu screen for new game, load game, save game, ect?
    Thanks for any help.
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Originally posted by Jsnova864
    Hi, I got a few simple q's, I think. I'm making a pretty simple RPG but my program is getting too big for me to handle. I’m trying to figure out how to use modules to organize it and I figured out how to create one, but how would I import it so I could use it? Thanks for any help.
    Hello, Jsnova864!

    I can help you figure out how to set up your modules. Please stay tuned for more help on saving for gaming purposes as I am not too savvy in this arena. Also I am pulling this one out of my forehead, bare with me:

    Form1 Code (If then Else Statement)

    Code:
    Private Sub GetReady2Rumble_Click()
    
    ...a bunch of stuff here, your code perhaps
    
    If user1 = 0 Then
    
    WellWellWell = "DHU!"
    
    Go2EvilDude.Deelite
    
    ElseIf user1 = 10 Then
    
    WellWellWell = "Fancy eh!"
    
    Go2AngelDude.Sooweet
    
    End If
    
    En Sub
    Go2EvilDude Module code (If Then Else statement)

    Code:
    Public WellWellWell As String
    
    Public Sub Deelite()
        
    
        If WellWellWell = "DHU!" Then              
            
           Msg Box "Try again monsieur!" 
    
        End If
    
    End Sub

    Go2AngelDude Module code (If Then Else statement)

    Code:
    Public WellWellWell As String
    
    Public Sub Sooweet()
        
    
        If WellWellWell = "Fancy eh!" Then              
             
          Msg Box "Groovy!" 
    
        End If
    
    End Sub
    Note: The message box information is where you'd need to have your app save to certain level. I guess you would need a timer or a way for the app to know which user is restarting the game therefore to load from last saved block...

    Module explanations:

    Names of your modules: Go2EvilDude and Go2AngelDude

    Deelite and Sooweet are simply specific to your Public Sub command, serves to refire an additional If Statement to compute what must happen when user1 reaches 0 or 10

    WellWellWell is just a way of getting something recorded as a result of user1 having a 0 or 10. Helps calculate the level of hardship/reward for specific instances (i.e. Try again or Groovy)
    Last edited by Dököll; May 16 '07, 01:24 AM. Reason: Deleted text

    Comment

    • Jsnova864
      New Member
      • Apr 2007
      • 12

      #3
      Thanks so much. I know its basic stuff but my teacher went over modules on a day I was absent and our current project didn’t require it so I missed out. Just to be sure I got it straight; in order to call a private sub from the module it's the name of the module, a dot, then the name of the private sub. Right?

      Comment

      • Dököll
        Recognized Expert Top Contributor
        • Nov 2006
        • 2379

        #4
        Originally posted by Jsnova864
        Thanks so much. I know its basic stuff but my teacher went over modules on a day I was absent and our current project didn’t require it so I missed out. Just to be sure I got it straight; in order to call a private sub from the module it's the name of the module, a dot, then the name of the private sub. Right?
        Right, name of module, a dot, and name of public sub...

        Go2AgelDude.Soo weet calls the module named Go2AngelDude

        Public Sub Sooweet() fires the code within that module (Go2AngelDude)

        Good luck with project, and you're welcome!

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          I may be mistaken, but I believe it works more like this...
          • Private is private - you can't call it from outside the module.
          • Public can be called from anywhere in the project, without the module name. (Unless the "module" is actually a form, in which case you do need to name it.)

          In other words, where your code says Go2AngelDude.So oweet it should just say Sooweet. (Great names, by the way. :))

          Comment

          • Jsnova864
            New Member
            • Apr 2007
            • 12

            #6
            O...kk. I haven’t seen public sub before so I didn’t know it existed. I tried a test program and I failed. I think the module part is ok because it has a problem with a method inside the module. Here (Error is at line 4, module code):

            FrmField Code:
            Code:
            Dim A As Integer, Field As Long, ColorG As Long
            Dim XCordG As Double, YCordG As Double, XIncG As Double, YIncG As Double, RadG As Double
            Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
                Select Case KeyCode
                    Case vbKeyEscape 'Quick quit for debugging
                        Unload Me
                End Select
            End Sub
            
            Private Sub Form_Load()
            FrmField.WindowState = vbMaximized 'Full Screen
            End Sub
            
            Private Sub Form_Resize()
                Field = FrmField.BackColor 'Black
                ColorG = &H80FF& 'No VbOrange?
                XCordG = FrmField.ScaleWidth / 2
                YCordG = FrmField.ScaleHeight / 2
                RadG = 10
                Call Guy 'Make Guy
            End Sub
            GuyModule Code:
            Code:
            Public Sub Guy() 'call this anytime the guy needs to move or refresh
                FillStyle = 0
                FillColor = Field 'Field = form backcolor
                Circle (XCordG, YCordG), RadG, Field   'undraw the guy....doesn't like it.
                For A = 0 To 4                         'Says, "Compile Error: Method Not Valid Without Suitable Object"
                    Circle (xcord - RadG, ycord + 5 * A), RadG / 4, Field 'Would that object be the form?
                    Circle (xcord + RadG, ycord + 5 * A), RadG / 4, Field
                Next A
                
                XCordG = XCordG + XIncG 'move the guy
                YCordG = YCordG + YIncG
                
                FillColor = ColorG
                Circle (XCordG, YCordG), RadG, Field 'Draw Guy
                For A = 0 To 4 'Arms
                    Circle (xcord - RadG, ycord + 5 * A), RadG / 4, Field
                    Circle (xcord + RadG, ycord + 5 * A), RadG / 4, Field
                Next A
            End Sub
            I think it is because it doesn't know where to make the circle. I thought because it was called in FrmField then it would draw it there. Is that the problem?

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by Jsnova864
              I think it is because it doesn't know where to make the circle.
              Correct.
              Originally posted by Jsnova864
              I thought because it was called in FrmField then it would draw it there.
              Incorrect.
              Originally posted by Jsnova864
              Is that the problem?
              Yes.

              How's that for clear, concise answers?

              If you'd like the useful ones, they cost extra. :)

              Seriously, where the sub was called from is irrelevant. It is separate, stand-alone code.You could be calling it from another Sub in the same code modeul - where would it draw then?

              You need to either specify Form1 in the code which draws the circle, or accept the drawing target as a parameter. For example, in Sub Guy() you can do this...
              Form1.Circle (XCordG, YCordG), RadG, Field 'undraw the guy....doesn't like it.

              or in Form1, do this...
              Guy Me ' Make Guy
              (note the CALL is optional.)
              and in Sub Guy() do this...
              Public Sub Guy(frm As Form) 'call this anytime the guy needs to move or refresh
              frm.FillStyle = 0
              .
              .
              .


              A couple of things to keep in mind...
              • The same applies to all the properties and methods, such as FillStyle and Circle.
              • You will save yourself a lot of work if you get used to using the With block statement. It effectively lets you say "this whole block of code applies to this object". For example...
                With frm
                .FillStyle = ...
                .FillColor = ...
                End With
              • Unfortunately, there are some methods which don't allow this sort of abbreviation. So for example, if Circle is one of them (I don't remember) then you might still have to include the object name at the front. (This is a pain.)

              Comment

              • Jsnova864
                New Member
                • Apr 2007
                • 12

                #8
                Ok, good news and bad news. The good news is that I’m not getting anymore error statements. The bad news is that it still doesn't work. I have the ScaleMode for Frmfield on pixels. Not sure that is the problem though 'cause I changed XCordG = FrmField.ScaleW idth/2 to XCordG = FrmField.Width/2. Likewise with YCordG and it still didn't work. Nothing happens. On the other hand when I cut the code out of the module and paste it in the form it works.
                Form Code(FrmField):
                Code:
                Dim A As Integer, Field As Long, ColorG As Long, XCordG As Double, YCordG As Double
                
                Dim XIncG As Double, YIncG As Double, RadG As Double
                
                Private Sub Form_Click()
                    ModuleGuy.Guy Me
                End Sub
                
                Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
                    Select Case KeyCode
                        Case vbKeyEscape 'Quick quit for debugging
                            Unload Me
                    End Select
                End Sub
                
                Private Sub Form_Load()
                FrmField.WindowState = vbMaximized 'Full Screen
                End Sub
                
                Private Sub Form_Resize()
                    Field = FrmField.BackColor
                    ColorG = &H80FF& 'No VbOrange?
                    XCordG = FrmField.ScaleWidth / 2 'Scalemode = 3-pixel
                    YCordG = FrmField.ScaleHeight / 2
                    RadG = 20
                    ModuleGuy.Guy Me 'Make Guy
                End Sub
                Module Code(ModuleGuy) :
                Code:
                Public Sub Guy(FrmField As Form) 'call this anytime the guy needs to move or refresh
                    FrmField.FillStyle = 0 'Undraw Guy
                    FrmField.FillColor = Field
                    FrmField.Circle (XCordG, YCordG), RadG, Field
                    For A = 0 To 4
                        FrmField.Circle (XCordG - RadG, YCordG + 5 * A), RadG / 4, Field
                        FrmField.Circle (XCordG + RadG, YCordG + 5 * A), RadG / 4, Field
                    Next A
                    
                    XCordG = XCordG + XIncG 'move the guy
                    YCordG = YCordG + YIncG
                    
                    FrmField.FillStyle = 0
                    FrmField.FillColor = ColorG
                    FrmField.Circle (XCordG, YCordG), RadG, Field 'Draw Guy
                    For A = 0 To 4 'Arms
                        FrmField.Circle (XCordG - RadG, YCordG + 5 * -A), RadG / 4, Field
                        FrmField.Circle (XCordG + RadG, YCordG + 5 * -A), RadG / 4, Field
                    Next A
                End Sub
                Note: I tried frm instead of frmfield and got the same results
                P.S. Thanks for helping me. I've learned more about Vb from you guys than than I have in the past 2 weeks from my teacher :D

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  I tihnk the most important correction you can make is to add
                  Option Explicit
                  at the top of all modules (code and form). You're using variables that don't exist, which means that VB just creates one for you, but it has no meaningful value.

                  Example: XCordG, which only exists within the form. You are trying to use it in your code module. Variable "scope" is a very important concept that you'll need to understand.

                  Comment

                  • Jsnova864
                    New Member
                    • Apr 2007
                    • 12

                    #10
                    Oh oops, stupid mistake. I usually use option explicit, but I forgot to use it because of it was just a test program. Thanks. I had to screw with the dims a bit to get it to work. I still can’t define XCordG and YCordG in the form and call it in the module but I think the "ByRef" method I just learned will let me get around that. I’ll screw around with it later. Thanks a lot for the help. I think I may come back later for saving because I don’t know where to start with it. Thanks again

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by Jsnova864
                      Oh oops, stupid mistake. I usually use option explicit, but I forgot to use it because of it was just a test program. Thanks. I had to screw with the dims a bit to get it to work. I still can’t define XCordG and YCordG in the form and call it in the module but I think the "ByRef" method I just learned will let me get around that. I’ll screw around with it later. Thanks a lot for the help. I think I may come back later for saving because I don’t know where to start with it. Thanks again
                      You should turn on the "Require explicit variable declaration" option, somewhere under the Tools menu. That way it's always there by default. This is one of those things which is better to have always switched on, even for a "quick and dirty" program, as it prevents so many little glitches like this.

                      Passing by reference is the default. Parameters are only passed by value if you say so. Although this is not very precise, the simplest way to think of it is that ByVal means "read only". In other words, just pass a copy of the value, don't let them touch my variable.

                      As for defining variables in the form and using them in the code module, you can, but only if you pass them in the call.

                      Let us know how it all goes. Good luck. :)

                      Comment

                      • Jsnova864
                        New Member
                        • Apr 2007
                        • 12

                        #12
                        Originally posted by Killer42
                        Let us know how it all goes. Good luck. :)
                        Will do! Now I'Il have to learn to work with multiple forms,save somehow, and play music, but I'll try to get some of it myself before I ask fer help. Does anyone know some good sites for VB6 methods (references, tutorails, ect.). Everything I find seems to be for Vb.Net or VB2005. Thanks For all the help. I'll most likely be back :D.

                        Comment

                        • Dököll
                          Recognized Expert Top Contributor
                          • Nov 2006
                          • 2379

                          #13
                          Originally posted by Jsnova864
                          Will do! Now I'Il have to learn to work with multiple forms,save somehow, and play music, but I'll try to get some of it myself before I ask fer help. Does anyone know some good sites for VB6 methods (references, tutorails, ect.). Everything I find seems to be for Vb.Net or VB2005. Thanks For all the help. I'll most likely be back :D.
                          Get a load of these results:



                          Happy hunting!

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by Jsnova864
                            Will do! Now I'Il have to learn to work with multiple forms,save somehow, and play music, but I'll try to get some of it myself before I ask fer help. Does anyone know some good sites for VB6 methods (references, tutorails, ect.). Everything I find seems to be for Vb.Net or VB2005. Thanks For all the help. I'll most likely be back :D.
                            Yes, it is getting harder to find stuff for VB6. However, though I hate to admit it, there is good reason. Let's face it, VB6 is something like 9 years old. That's a long time in IT.

                            Althouhg I like VB6, I really need to find the time to start learning a later version. In which case, I guess it would make sense to go for the latest. Perhaps you should, too. VB2005 is, after all, available as a free download from MS. (I assume it's a limited version that you get for free).

                            Comment

                            Working...