Cannot assign class object as dictionary value

sphynxissphynxis Posts: 1Member

Hi, I am trying to set up a dictionary to hold some simple data about character classes. When create the class objects and try to assign them as the value in a dictionary the object displays as null in debug. See below code. See image for debug of null objects:

`var characterClasses = {}
var characters = {}
enum ClassType { Warrior, Priest, Mage, Druid }

func _init():
var ClassWarrior = CharacterClass.new("Warrior", Color(.5,.3,.3))

characterClasses[ClassType.Warrior] = ClassWarrior
characterClasses[ClassType.Priest] = CharacterClass.new("Priest", Color(1,1,1))
characterClasses[ClassType.Druid] = CharacterClass.new("Druid", Color(.8,.6,.6))
characterClasses[ClassType.Mage] = CharacterClass.new("Mage", Color(0,0,.8))

for i in range(100):
    var c = round(rand_range(0, characterClasses.size()-1))
    #print (characterClasses[c])
    var id = generate_unique_id()
    characters[id] = character.new(id, "Character"+str(i), c)

class CharacterClass:

var name
var color

func _init(_name, _color):
    name = _name
    color = _color`

I'm new to godot so it's probably something obvious that I'm missing.

Thanks for any help in advance.
Matt

Answers

  • _807__807_ Posts: 57Member
    edited August 2017

    Yesterday i did some test to your code and the data is not null, is reference (dictionary are always a reference), so i don´t understand what is the problem. You can access class members. Probably there is some misconcepction about inner class and objects. Object is one kind of class that beggins one kind of inheritance (Ej: Node2d inherit Node that inherit Object- see the inspector) , reference is other type, inner class don´t have an "instance", there are nothing instanced in scenetree, is only data. I´m not in my main computer, if you can wait some hours i put here your modified code with the "prints" and you can see more clear in console the "reference" thing.

  • _807__807_ Posts: 57Member
    extends Node2D
    
    var characterClasses = {}
    var characters = {}
    var day = 0
    enum ClassType { Warrior=0, Priest, Mage, Druid }
    
    func _init():
        var ClassWarrior = CharacterClass.new("Warrior", Color(.5,.3,.3))
        var ClassPriest = CharacterClass.new("Priest", Color(1,1,1))
        var ClassDruid = CharacterClass.new("Druid", Color(.8,.6,.6))
        var ClassMage = CharacterClass.new("Mage", Color(0,0,.8))
        characterClasses[ClassType.Warrior] = ClassWarrior
        characterClasses[ClassType.Priest] = ClassPriest
        characterClasses[ClassType.Druid] = ClassDruid
        characterClasses[ClassType.Mage] = ClassMage
    
        for i in range(100):
            var c = round(rand_range(0, characterClasses.size()-1))
        #   print (characterClasses[int(c)])
            var id = generate_unique_id()
            characters[id] = character.new(id, "Character"+str(i), c)
            var getclass = characters[id]
            print(str(getclass.id)+" / "+getclass.name+":")
            if int(getclass.character_class) == ClassType.Warrior: print("Warrior")
            elif int(getclass.character_class) == ClassType.Priest: print("Priest")
            elif int(getclass.character_class) == ClassType.Mage: print("Mage")
            elif int(getclass.character_class) == ClassType.Druid: print("Druid")
    
        print("\ncharacterClasses Dictionary:\n")
        print(characterClasses)
        print("\ncharacters Dictionary:\n")
        print(characters)
    
    
    func end_day():
        day += 1
        print(str(self.generate_unique_id()))
    
    func generate_unique_id():
        var id
    
        randomize()
        id = randi() % 9999999
    
        return id
    
    class CharacterClass:
        var name
        var color
        func _init(_name, _color):
            name = _name
            color = _color
    
    class character:
        var id
        var character_class
        var name
        func _init(_id, _name, _character_class):
            id = _id
            name = _name
            character_class = _character_class
    
  • _807__807_ Posts: 57Member

    Paste the code, execute and see the console. There is no "null" data.

Sign In or Register to comment.