How do i not make it in "" instead a str

Godot Version

4.5.1 stable

Question

i need it to do if it equals the pack number it sets the pack cost to that specific packs cost but it just prints it as a quote instead of getting the number

var Pack_selection = {
pack = 1,
pack_cost = 0,}
← trying to set this number to pack cost

var Card_pack1 = {
pack_number = 1,
this_pack_owned = 0,
pack_cost = 0,}
← thist is the pack cost of selected pack

func _process(delta: float) → void:
Pack_selection.pack_cost = “Card_pack” + str(Pack_selection.pack) + “.pack_cost”
← the idea **
print(str(Pack_selection.pack_cost))** ← this just prints “Card_pack1.pack_cost” instead of the cost 0**
pass**

This should work:

func _process(delta: float) -> void:
	Pack_selection.pack_cost = get("Card_pack" + str(Pack_selection.pack)).get("pack_cost")
	print(str(Pack_selection.pack_cost)) 
1 Like

Worked like a charm thanks I felt like it could have been something simple but I didn’t understand the get function when looking it now I can sorta understand it more :+1:

1 Like

would happen too know how to figure this out i thought it would be similar to this but i was wrong

var Pack_selection = {
pack = 1, ← getting this pack
pack_cost = 0,}

var Card_pack1 = { ← this pack is the one selected
pack_number = 1,
this_pack_owned = 0, ← then add increase this number
pack_cost = 0,}

func _on_purchase_button_pressed() → void:
if str(stats.money) == str(Pack_selection.pack_cost):
get(“Card_pack” + str(Pack_selection.pack)).get(“this_pack_owned”) += 1 ← i thought this would it does not
pass

if you do know or do not wish to respond it is perfectly fine i will make another post on help soon

You’re telling the computer to get something, not set / assign something.

Its been a while since I used a dictionary but I think you can just pick the dictionary key and add one to it.

my_dictionary[“key”] += 1

Or you have to assign the key to a new value

my_dictionary[“key”] = int(times_purchased_pack)

You retrieve the value by value, not by reference.
This means that you’re not adding anything to your original Dictionary anymore.

This would work, it retrieves the value first, adds 1 to it and then assigns it back to the Dictionary:

get("Card_pack" + str(Pack_selection.pack)).set("this_pack_owned", get("Card_pack" + str(Pack_selection.pack)).get("this_pack_owned") + 1)

But as you can see, it gets pretty ugly real quick. You’re trying to use Dictionaries as objects, which I would advise against it.
Make a CardPack its own class of type Resource and assign properties to that class that you can adjust. Otherwise your codebase will quickly become an unreadable spaghetti.

BTW, please format your code snippets as preformatted text with triple backticks ``` at the beginning and end of the snippet.

1 Like