0 votes

When I make a Resource var I'm using local to the scene of a saved scene that I instance a lot, it makes the getter in a setget var in my Resource's code break.

Code that breaks when I switch my custom Resrouce to local to scene:

extends Resource

class_name Stats

export(Dictionary) var stats = {
    "dps": 0.0,
    "cdr": 0.0,
    "armour": 0.0,
    "movespeed": 0.0,
    "tenacity": 0.0,
    "h_regen": 0.0,
    "e_regen": 0.0
}

func get_stats():
    var res = {}
    for key in stats:
        res[Enums.stat[key]] = stats[key]
    return res



func set_stat(stat_type, new_value):
    stats[Enums.stat.keys()[stat_type]] = new_value

Short explanation of the stats var:
This dictionary gets set up with string values for keys, but when I get my stats dictionary from another script, I convert it's keys to their corresponding enum values. (When Resource is local to scene, the getter breaks, because when it refers to stats it gets a dictionary with enum values as keys, not strings.

Godot version v.3.2.2 Stable
in Engine by (23 points)

1 Answer

0 votes
Best answer

I found out what the problem is!

When you have a Resource that contains a variable: var a: Dictionary setget , _get_a; and you use .duplicate(true) on it, it creates a new Resource and starts filling it up with copies of the other Resources values. So, when it tries to copy the variable a from the original Resource, it says new_resource.a = original_resource.a, which calls the getter function that setget for var a: Dictionary setget , _get_a specifies.

This is why my code breaks. Because my code converts my stats dictionary 's keys from strings to their corresponding enum's int values in the getter function for the variable. This make the new duplicate of the original Resource have int keys instead of string keys, and thus breaks the getter function's functionality.

by (23 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.