Hi! I have been looking for examples of how to deal with my variables in my game but I can't find the right one.

I tried to make nested dictionaries for a "blackboard" system I learned about in a GDC video. But in that video they seem to assume we already know some important concepts. I tried it but this doesn't seem like the right approach to keeping track of the state of all characters and the general game:

  • xyz replied to this.

    Godoten Each dictionary entry requires a key and a value, even if the value is a reference to another dictionary.

    var characters: Dictionary = {
    	"rookie": rookie,
    	"oldGuy": oldGuy
    }

    If you don't want to index by a key, you can put your dictionaries in a plain array:

    var characters: Array = [rookie, oldGuy]

      I'd say the usual way is to create a script that can be used for all characters that should have the same properties and functions.

      Create a new scene and chose the type you need for your character. I chose CharacterBody 2D.
      Attach a script to it:

      class_name Character extends CharacterBody2D
      
      @export var character_name : String
      var talked_with_hero := false
      var invited_to_party := false
      var party_invite_accepted := false
      
      #Other built-in functions for characterbody2d and your functions

      Use @export annotation for the variables you like to set in the inspector.
      class_name Character is optional, but is helpful for code completion.

      Save the scene. Now you can rightclick in the filesystem and select new inherited scene and create more characters and change their properties etc. Or manually create a new scene and attach the script to it to create new characters.

      So each character keeps track on its own.

      If you plan to have a party, you might create another script that handles the party.
      You could use an array and put all characters into it, that have joined the party, or remove the ones that left the party.

      Well, that's what came to my mind from what I've assumed from your text. 🙂

        trizZzle But how could I modify the values for the inherited scenes in this case? I can declare the "var" again, so do I need to modify it in ready() ? And how do I access to those values from the dialogue manager since those scripts are not singleton?

          xyz I see, I was using a Dictionary as if it was an Array. Thank you!

          Godoten I don't know how to explain this. 🙁

          In case you are making a game where you can walk around and talk to other characters you need a reference to the character you are talking to. Once you have a reference to the other character you can access functions and variables of them.

          As your dialogue manager is a singleton you can access it from your player or other characters. You could create a function where you pass in text and other information (for example a reference for the character who talks if needed).
          Basically you would use the DialogueManager just to show dialogues and it doesn't need to know about all your characters.

            trizZzle Sounds good. I will try it this way. I still don't understand how should I give my inherited script of a Character a custom value for a variable. This will give me an error:

            extends Characters
            var name: String = "oldGuy"
            var talked_with_hero: bool = true  #<- This will give an error because it's already declared in the parent class

              Godoten I didn't mean an inherited script. I meant an inherited scene of your character. Think of it as a blueprint for your characters (or other things that have the same functionality but have different values). If you add new nodes to it, all other scenes that inherit from it will have those nodes too.

              This is the character I create inherited scenes from. See the @export annotation in front of the variables? This makes them show up in the inspector when you click the node in the scene. Then you can assign values to them from within the editor (though I guess talked to player will be something that needs to change during runtime?).

              To create inherited scenes right click on the scene file in the filesystem and select new inherited scene.
              You can change the root nodes name, the exported variables values and add other nodes to it if needed.
              Nodes in yellow show that those nodes come from the inherited scene.

              And if you think you don't want this scene to be inherited anymore you still can rightclick the root node and select clear inheritance.

              I hope this helps. I didn't want to throw you off track because this wasn't your initial question. 😟

                trizZzle Oh right, the point would be to change it at runtime! Also, the @export method seems to make it a lot easier. Thank you so much!