Search Unity

Best practice for large text

Discussion in 'Scripting' started by khanstruct, Jan 28, 2019.

  1. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    My game will be randomly selecting names for multiple characters based on their randomly generated race and sex. In addition, it will also select a few full sentences to generate basic descriptions.

    Now, I can think of a dozen different ways to do this (static classes with Lists, external text files/parsing, etc.), I'm just wondering what the best method would be.

    I'll likely avoid the external file idea, because I don't want players rummaging around in them. But how would you suggest I approach this? Should I just have a static class with a pile of strings I can sort through?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,538
    The class full of strings sort of has a memory implication.

    All of those strings are going to be loaded into memory and stay there. Now it probably won't be much... only a few megs probably. But it's still unnecessary memory usage, especially if you are targeting lower end devices (mobile?).

    But I mean, in this day an age... it's not that big of a deal.

    ...

    Note a TextAsset gets compiled up as an asset when you build. So you wouldn't really have a text file just sitting out for someone to easily rumage through (not without decompiling your game with the various decompilers out there).

    Though personally I wouldn't be concerned about that. If someone is brave enough to go into the install directory and start mucking around with files, they shouldn't expect the game to work necessarily afterward.

    And if they use it as a way to have custom names added... eh, that's a feature in my book. That's the bonus to PC games the fact you can hack up the files and make custom little changes to it.

    The only real time this 'hacking' is a concern is for online play. In which case... anything that should be secure should be moved up to the server and there is a "GeneratePlayerName" function on the server (or something to that effect).

    ...

    Note I will say one big bonus to a raw text file is that you can update it without having to recompile.

    Heck you could even insert extra data into the text file... such as statistics on how often a name was picked. And you can have your algorithm sort out odds wise those names and avoid them in the future.
     
    SparrowGS and khanstruct like this.
  3. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    Thanks. Yeah, these files will have other uses, such as adding stat and skill bonuses based on the random descriptions chosen.

    I'm developing it for PC, and it'll be a lightweight game, so I'm not terribly concerned about a few extra MBs. And you're right, it's a singleplayer game, so it won't be a huge deal if the players start digging around in things. I'm mainly concerned about the "proper" and most efficient way to do it.

    I haven't done text parsing in years. Back when I did, I used a script called TinyXMLReader. Is there anything new in that regard, or should I just go dig that back up?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,538
    .net/mono has XML support built in if you wanted to use XML.

    Personally I use json these days. Unity has a built in json parser (though it's limited). And there is 3rd party fully featured ones like Json.Net that are really nice. I personally use my own json parser:
    https://github.com/lordofduct/space...ree/master/SPSerialization/Serialization/Json

    And heck plain old csv or ini files are super simple to parse just using the System.IO.StreamReader class, just read the file line by line. OR if you want to chew on that memory just load the entire file into memory as a string and parse it as one giant string with System.IO.File.ReadAllText.

    ...

    I would argue that stats and what not probably have nothing to do with a name list and could easily go into their own file/data container.
     
    khanstruct likes this.
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Why must you deny me of fun?:p
    Like @lordofduct said, its one of the key features in PC imo, I personally keep my games(all singleplayer) as "hackable" as I can.
     
    khanstruct likes this.
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,538
    I think of video games the same way I do board games.

    Sure there are rules in the box... but who plays Monopoly by the rule book?

    Once someone has purchased my game they know how to play it by my rules by just playing it... but if they want to go off track, have at it!

    ...

    So currently we're working on a series of survival horror games (see my signature). We're working on episode 3 in the trilogy right now. As each game passes the reality of the game actually being a video game becomes more and more entangled in the story.

    For example in ep2 you learn over the game that the zombies you've been fighting are actually your previous dead self. The world is just full of dead Hanks (your character's name). And later you get a 'gimpboy' that you can play games inside the game to unlock parts of the game.

    Anyways... in ep3 we're taking it to the next level by giving you a 'gameshark'/'action-replay' to use in game. In ep3 you'll be able to swap characters via a 'waystation' (fake playstation) found in safe rooms. And if you hook up the gameshark to it you can insert hacks like infinite lives, wall clipping, etc. And via this there'll be secrets/puzzles to unlock by exploiting the gameshark.

    Like a "unlock all doors" code that will unlock a door that otherwise can't be unlocked.

    Anyways... my point... cheating in games can be FUN! And that's all I want for my players. For them to have fun.
     
    SparrowGS and khanstruct like this.
  7. khanstruct

    khanstruct

    Joined:
    Feb 11, 2011
    Posts:
    2,869
    Sounds like a cool series.

    On second thought, my "descriptions" section is responsible for a fair amount of data, such as the description itself, the name of the description, one or two stat modifiers each. I'm gonna go with a regular class that stores everything. I'll probably even use an array of structs just to make it easier for me.

    This way, I can just attach the class to an object that generates the characters and load it into a scene as needed.