The Godot Community Forums are back up and running! Please read the the announcement post for important information on what happened. Thanks!

Random generation that is consistent between save files.

In this game there is a language you need to decipher and the language is represented through symbols. However I feel the game would have no replay value if the symbols meant the same thing everytime so I want the language to be randomised (as well as certain quests)

I'm a godot newbie and I've coded before but it's been a while. I know I need to use some sort of array but I'm not sure how to go about randomising the language and quests but have it remember and not randomly generate something different whenever the level is loaded or the game is closed.

Am I missing something simple like having the code skip over the generation if its recognised it's already been activated once?

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 3,141
    Accepted Answer

    Welcome to the forums @obsessedthinking!

    I think you can achieve this using Godot's build in random number generator. To randomize the language and quest order, you could try using the shuffle function. To keep certain quests in the same order, the simplest way would probably be to store the quests in several arrays initially, and combine them after randomizing the ones you want. Finally, you can you save files (documentation) to detect if the language and quest arrays have been generated already, and if they have, just load the files instead of generating them.

    Here's some untested code for how it could potentially be accomplished:

    const SAVE_FILE_LOCATION = "user://save_file.txt"
    
    # I don't know what the data you have will look like, so I'm just going to use
    # simple strings.
    var language_array = ["a", "b", "c"]
    var important_quests_1 = ["Tutorial", "Quest_01"]
    var random_quests_1 = ["Quest_02", "Quest_03", "Quest_04"]
    var important_quests_2 = ["Tutorial_02"]
    var random_quests_2 = ["Quest_05", "Quest_06"]
    
    # a dictionary to hold data we want to save/load
    var game_data_dictionary = {}
    
    func _ready():
        # check for a save file
        var save_game_file = File.new()
        if (save_game_file.file_exists(SAVE_FILE_LOCATION)):
            load_game_data(save_game_file)
        else:
            generate_game_data(save_game_file)
            save_game_data(save_game_file)
    
    func load_game_data(game_file):
        game_file.open(SAVE_FILE_LOCATION, File.READ)
        game_data_dictionary = parse_json(game_file.get_line())
        game_file.close()
    
        # Done! Now we can use save_game_dictionary as needed
        print (game_data_dictionary)
        print ("")
        print ("Language array: ", game_data_dictionary["language_array"])
    
    func generate_game_data():
        # Get a new random seed for the random number generator
        randomize()
        # Randomize the language array
        language_array.shuffle()
        # Shuffle the random quests
        random_quests_1.shuffle()
        random_quests_2.shuffle()
        # Add the random language to the save game dictionary
        game_data_dictionary["language_array"] = language_array
        # You can either save all of the arrays individually, or combine them
        # in this example, I'll just combine them so it's a single value in the dictionary.
        game_data_dictionary["quests_array"] = important_quests_1 + random_quests_1 + important_quests_2 + random_quests_2
    
    func save_game_data(game_file):
        # Save to a file
        game_file.open(SAVE_FILE_LOCATION, File.WRITE)
        game_file.store_line(to_json(game_data_dictionary))
        game_file.close()
    

    That's more or less the approach I'd take if I was going at this problem, though because the code isn't tested, I'm not sure if it will work or not.


    This discussion was caught in the moderation queue since you have not confirmed your account yet.

    Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.

    If you need any help, please let us know! You can find ways to contact forum staff on the Contact page. Thanks! :smile:

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 3,141Admin
    Accepted Answer

    Welcome to the forums @obsessedthinking!

    I think you can achieve this using Godot's build in random number generator. To randomize the language and quest order, you could try using the shuffle function. To keep certain quests in the same order, the simplest way would probably be to store the quests in several arrays initially, and combine them after randomizing the ones you want. Finally, you can you save files (documentation) to detect if the language and quest arrays have been generated already, and if they have, just load the files instead of generating them.

    Here's some untested code for how it could potentially be accomplished:

    const SAVE_FILE_LOCATION = "user://save_file.txt"
    
    # I don't know what the data you have will look like, so I'm just going to use
    # simple strings.
    var language_array = ["a", "b", "c"]
    var important_quests_1 = ["Tutorial", "Quest_01"]
    var random_quests_1 = ["Quest_02", "Quest_03", "Quest_04"]
    var important_quests_2 = ["Tutorial_02"]
    var random_quests_2 = ["Quest_05", "Quest_06"]
    
    # a dictionary to hold data we want to save/load
    var game_data_dictionary = {}
    
    func _ready():
        # check for a save file
        var save_game_file = File.new()
        if (save_game_file.file_exists(SAVE_FILE_LOCATION)):
            load_game_data(save_game_file)
        else:
            generate_game_data(save_game_file)
            save_game_data(save_game_file)
    
    func load_game_data(game_file):
        game_file.open(SAVE_FILE_LOCATION, File.READ)
        game_data_dictionary = parse_json(game_file.get_line())
        game_file.close()
    
        # Done! Now we can use save_game_dictionary as needed
        print (game_data_dictionary)
        print ("")
        print ("Language array: ", game_data_dictionary["language_array"])
    
    func generate_game_data():
        # Get a new random seed for the random number generator
        randomize()
        # Randomize the language array
        language_array.shuffle()
        # Shuffle the random quests
        random_quests_1.shuffle()
        random_quests_2.shuffle()
        # Add the random language to the save game dictionary
        game_data_dictionary["language_array"] = language_array
        # You can either save all of the arrays individually, or combine them
        # in this example, I'll just combine them so it's a single value in the dictionary.
        game_data_dictionary["quests_array"] = important_quests_1 + random_quests_1 + important_quests_2 + random_quests_2
    
    func save_game_data(game_file):
        # Save to a file
        game_file.open(SAVE_FILE_LOCATION, File.WRITE)
        game_file.store_line(to_json(game_data_dictionary))
        game_file.close()
    

    That's more or less the approach I'd take if I was going at this problem, though because the code isn't tested, I'm not sure if it will work or not.


    This discussion was caught in the moderation queue since you have not confirmed your account yet.

    Upon creating your account you should have received an account verification email. The confirmation email may have been incorrectly flagged as spam, so please also check your spam filter. Without confirming your account, future posts may also be caught in the moderation queue. You can resend a confirmation email when you log into your account if you cannot find the first verification email.

    If you need any help, please let us know! You can find ways to contact forum staff on the Contact page. Thanks! :smile:

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file