0 votes

how should i set my var, if i have many var but need to choose one?

for example:
when i use item(exmple potion with stats str and con),
i want to increase B_STR["BUFF"] and B_CON["BUFF"]

there any other way to do it? the simple way .

for now i just do like this:
it become so long
inv script

var stats = preload(stats script resource path)
var itemData : Dictionary ={ all data of item}
func _on_invSlot_gui_input(event:InputEvent,invSlot :slotClass):

    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed:
                    stats.update_stats(itemData[statsName], itemData[statsType],itemData[[amount)

stats script resource

var B_STR :Dictionary = {"BASE":0,"EQ":0,"BUFF":0,"DEBUFF":0}
var B_CON :Dictionary = {"BASE":0,"EQ":0,"BUFF":0,"DEBUFF":0}
var B_DEX :Dictionary = {"BASE":0,"EQ":0,"BUFF":0,"DEBUFF":0}
var B_INT :Dictionary = {"BASE":0,"EQ":0,"BUFF":0,"DEBUFF":0}
var B_WIS :Dictionary = {"BASE":0,"EQ":0,"BUFF":0,"DEBUFF":0}
var B_LUK :Dictionary = {"BASE":0,"EQ":0,"BUFF":0,"DEBUFF":0}

func update_stats(statsName,statType,amount):
    match statsName:
        "STR":
                        if statType == "BUFF":
                          B_STR["BUFF"] += amount
                        if statType == "EQ":
                          B_STR["EQ"]+= amount
                        if statType == "BASE":
                          B_STR["BASE"] += amount                
                        if statType == "DEBUFF":
                         B_STR["DEBUFF"] += amount         
        "CON":
                        if statType == "BUFF":
                          B_CON["BUFF"] += amount
                        if statType == "EQ":
                          B_CON["EQ"]+= amount
                        if statType == "BASE":
                           B_CON["BASE"] += amount                
                        if statType == "DEBUFF":
                           B_CON["DEBUFF"] += amount     
        "DEX":
                        if statType == "BUFF":
                           B_DEX["BUFF"] += amount
                        if statType == "EQ":
                           B_DEX["EQ"]+= amount
                        if statType == "BASE":
                          B_DEX["BASE"] += amount                
                        if statType == "DEBUFF":
                         B_DEX["DEBUFF"] += amount 
                         and so on>>>>
in Engine by (380 points)

1 Answer

0 votes

If I did not misunderstand, do you want to reduce the code?
Dictionaries within dictionaries can be used:

var stats:Dictionary ={
    "B_STR" : {"BASE" : 5, "EQ" : 5, "BUFF" : 5, "DEBUFF" : 5},
    "B_CON" : {"BASE" : 0, "EQ" : 0, "BUFF" : 0, "DEBUFF" : 0},
    "B_DEX" : {"BASE" : 0 ,"EQ" : 0 ,"BUFF" : 0, "DEBUFF" : 0}
}   

func update_stats(stats_name, stat_type, amount):
    stats["B_"+stats_name][stat_type] += amount
by (2,052 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 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.