Results 1 to 13 of 13

Thread: Saving Game

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    25

    Saving Game

    Hi,

    Well, I have most of my game done, except I need to learn the following:

    How to transfer variables from form to form
    How to transfer variables to an outside source, like notepad, so it will save the information in the game.

    Also, I would like to learn how to create a database, that will run from a server, so they could create an account, and login. But this is not a neccesity.

    Thanks a ton, I know its pretty broad.

    Shiz

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Saving Game

    Quote Originally Posted by Shizzell
    How to transfer variables from form to form
    One way is to add a Module to your project, and declare the shared variable(s) in it (using Public), the variable(s) are then available from anywhere in your project.

    How to transfer variables to an outside source, like notepad, so it will save the information in the game.
    That's complex.. an easier option is to save a text file - see the Classic VB FAQ link below for examples & explanations.

    Also, I would like to learn how to create a database, that will run from a server, so they could create an account, and login. But this is not a neccesity.
    That's a whole new kettle of fish.. see the ADO Tutorial link below. (note that if you are using a database, you could use that to save the game instead of files).

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    25

    Re: Saving Game

    Could you give me an example of the module with public?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Saving Game

    If you want 2 variables called "Fred" and "Joe" to be available to your forms, you would put this at the top of a module:
    VB Code:
    1. Public Fred as String, Joe as String

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    25

    Re: Saving Game

    I know this is noobish, but i'ved never written a module, how does it work?

  6. #6
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: Saving Game

    Quote Originally Posted by Shizzell
    I know this is noobish, but i'ved never written a module, how does it work?
    Rich click on the project explorer, Add->Module. Then bung Si's code into it. The project explorer is were all your forms are listed.

    As for saving the game vars

    http://www.vbforums.com/showpost.php...75&postcount=7
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    25

    Re: Saving Game

    Dude, nice. That helps alot. Thanks bro.
    But where do i put my variables?

  8. #8
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: Saving Game

    Just write them in at the top and declare them as public.

    Code:
    Public gamevariable1 as integer, HighScore as long
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    25

    Re: Saving Game

    Just write them in at the top and declare them as public.


    Code:


    Public gamevariable1 as integer, HighScore as long
    No, I mean in the saving the game, where in that code do I put my information?

    And I simply don't get the code to save, and get. What must I do completely? And do I put that under a command button?

    ??
    Last edited by Shizzell; Jul 5th, 2006 at 12:19 PM.

  10. #10
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: Saving Game

    Quote Originally Posted by Shizzell
    No, I mean in the saving the game, where in that code do I put my information?

    And I simply don't get the code to save, and get. What must I do completely? And do I put that under a command button?

    ??
    Ah i see

    yes command buttons

    VB Code:
    1. 'the varibales in the following udt should be replaced with you setting variables, for example. Play position, high score.....
    2. Private Type MyType
    3. setting1 as integer
    4. setting2 as string * 10
    5. setting3 as byte
    6.  
    7. End Type
    8.  
    9.  
    10.  
    11. 'command button save
    12.  
    13. Dim settings As MyType
    14. Open "C:\settings.set" For Binary Access Write As #1
    15.   Put #1, , settings
    16. Close #1
    17.  
    18.  
    19. 'command button load
    20.  
    21. Dim settings As MyType
    22. Open "C:\settings.set" For Binary Access Read As #1
    23.   Get #1, , settings
    24. Close #1
    25.  
    26. 'assign the values of the type to the game variables.
    27.  
    28. var1 = settings.setting1
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    25

    Re: Saving Game

    Could you give me an example? I think I'm catching on though.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    May 2006
    Posts
    25

    Re: Saving Game

    Ok, I got it to work kinda. Code:

    VB Code:
    1. Option Explicit
    2. Dim wow2 As String
    3.  
    4. Private Type MyType
    5. setting1 As Integer
    6. setting2 As String * 10
    7. setting3 As Byte
    8.  
    9. End Type
    10.  
    11. Private Sub Command2_Click()
    12.  
    13. Dim settings As MyType
    14. Open "C:\settings.txt" For Binary Access Write As #1
    15.   Put #1, , label1.Caption
    16. Close #1
    17. End Sub
    18.  
    19. Private Sub Command3_Click()
    20. Dim settings As MyType
    21. Open "C:\settings.txt" For Binary Access Read As #1
    22.   Get #1, , settings
    23. Close #1
    24.  
    25. wow2 = settings.setting2
    26.  
    27. Label3.Caption = wow2
    28. End Sub
    29.  
    30. Private Sub Form_Load()
    31. Label1.caption = "Helloooooo"
    32. End Sub


    And when I click the load command button (Command2) It gives me the following in label3: lloooooo


    I need some help =/

    Thanks guys
    Last edited by Shizzell; Jul 5th, 2006 at 01:47 PM.

  13. #13
    Addicted Member
    Join Date
    Feb 2006
    Location
    The Sea of Tranquility
    Posts
    252

    Re: Saving Game

    almost correct

    VB Code:
    1. Option Explicit
    2.    
    3.  
    4.     Private Type MyType
    5.     setting1 As Integer
    6.     setting2 As String * 10
    7.     setting3 As Byte
    8.  
    9.     End Type
    10.  
    11.     Private Sub Command1_Click()
    12.  
    13.     Dim settings As MyType
    14. 'assign your caption to setting2 first.
    15. settings.setting2 = Label1.Caption
    16.    
    17. Open "C:\settings.txt" For Binary Access Write As #1
    18.       Put #1, , settings
    19.     Close #1
    20.     End Sub
    21.  
    22.     Private Sub Command2_Click()
    23.     Dim settings As MyType
    24.     Open "C:\settings.txt" For Binary Access Read As #1
    25.       Get #1, , settings
    26.     Close #1
    27.  
    28.     Label2.Caption = settings.setting2
    29.  
    30.      
    31.     End Sub
    32.  
    33.     Private Sub Form_Load()
    34.     Label1.Caption = "Helloooooo"
    35.     End Sub

    Of course you can have as many variables of different types in the UDT. Just to note ive changed it to command1 and command2 and label 1 and 2.
    Last edited by Rich2189; Jul 5th, 2006 at 04:15 PM.
    Rich

    A)bort, R)etry, I)nfluence with large hammer.
    Please take a moment to rate useful posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width