none
RPG project - Problems - saving arrays. RRS feed

  • Question

  • In my rpg project, i have an array for all stats (level, strength, agility, health and inteligence) and i'm trying to save each stat in a line.

    the problem is: i want that if i add another stat in the array (e.g.: stat["mana"]) i would need to edit that code that is in middle of the script. (also requesting a find&replace tool =] ) i'm using array.getitemcount(stat) to get the number of stats, but the problem is that the method i'm using saves the index name to the file, and not the faule, someone please help!!!

    i cant publish, so i'll post the code.

     

     

    'TEXT RPG 0.1 BY KROLTAN '------------------------------------------------------------------------------------------------------------
    'Global Variables ----------------
    global_version =
    "0.1"
    color_programtext =
    "green"
    str_check_newgame =
    "new game"
    str_check_loadgame = "load game"
    str_check_exit = "exit"
    'Strings ----------------------------
    str_title =
    "TextRPG v" + global_version
    str_start =
    "Welcome to Text RPG v" + global_version
    str_newloadexit =
    "What you want to do? New Game, Load Game or Exit? (write)"
    str_newprofilepromt =
    "Profile name"
    'Starting stats --------------------
    var_hp =
    100
    stat["level" ] =
    1
    stat["maxhp" ] =
    100
    stat["strength" ] =
    1
    stat["agility" ] =
    1
    stat["intelligence" ] =
    1
    var_location =
    "tutorial"
    var_combat =
    "null"
    'Subroutines -----------------------
    Sub save
      indextosave = Array .GetAllIndices ( stat)
      For savestat = 1 To Array . GetItemCount ( stat)
        File .WriteLine ( saveloc, savestat, stat[indextosave[savestat]])
      EndFor
    EndSub
    Sub newgame_sub
      saveloc = Program . Directory + "\profiles\" + Dialogs .AskForTextLine ( str_newprofilepromt,str_title)
      If saveloc <> "" Then
        save()
      EndIf
    EndSub
    Sub loadgame_sub
      loadfile = Dialogs . AskForFile ( "TextRPG Profiles (*.ktrpgp | *.ktrpgp" )
      indextoload = Array . GetAllIndices ( stat)
      For loadstat = 1 To Array .GetItemCount ( stat)
        File .ReadLine ( loadfile, loadstat)
      EndFor
    EndSub
    'Code -------------------------------
    TextWindow . Title = str_title
    TextWindow . ForegroundColor = color_programtext
    TextWindow . WriteLine ( str_start)
    TextWindow . WriteLine ( str_newloadexit)
    var_saveloadexit = TextWindow . Read ()
    var_saveloadexit_low = Text .ConvertToLowerCase ( var_saveloadexit)
    If var_saveloadexit_low = str_check_newgame Then
      newgame_sub()
    ElseIf var_saveloadexit_low = str_check_loadgame Then
      loadgame_sub()
    ElseIf var_saveloadexit_low = str_check_exit Then
      Program.End()
    EndIf

    |"| /"/|"""\ /"""\|"| |__ __| /\ |"""\ |"| | |/ / | " / | | || | | | / \ | |\ \ | | | |\ \ | | \ | | || |__ | | / \ | | \ \| | |_| \_\|_|\_\\___/|____| |_| /_/""\_\|_| \___|
    • Edited by Kroltan Monday, July 12, 2010 8:22 PM code
    Monday, July 12, 2010 8:04 PM

Answers

  • Rather than write each of the different stats on a line, you could write the whole array to the file. All it would take would be:

    File.WriteLine(saveloc, 1, stat)

    That one statement would save the whole array to the file, and if you ever decide to add new stats, you wouldn't need to write any new code to load them. It would be as simple as:

    stat = File.ReadLine(loadfile, 1)

    • Proposed as answer by WriterCoder Monday, July 12, 2010 9:17 PM
    • Marked as answer by Kroltan Monday, July 12, 2010 11:27 PM
    Monday, July 12, 2010 9:17 PM

All replies

  • Rather than write each of the different stats on a line, you could write the whole array to the file. All it would take would be:

    File.WriteLine(saveloc, 1, stat)

    That one statement would save the whole array to the file, and if you ever decide to add new stats, you wouldn't need to write any new code to load them. It would be as simple as:

    stat = File.ReadLine(loadfile, 1)

    • Proposed as answer by WriterCoder Monday, July 12, 2010 9:17 PM
    • Marked as answer by Kroltan Monday, July 12, 2010 11:27 PM
    Monday, July 12, 2010 9:17 PM
  • Thank you!
    |"| /"/|"""\ /"""\|"| |__ __| /\ |"""\ |"| | |/ / | " / | | || | | | / \ | |\ \ | | | |\ \ | | \ | | || |__ | | / \ | | \ \| | |_| \_\|_|\_\\___/|____| |_| /_/""\_\|_| \___|
    Monday, July 12, 2010 11:27 PM