Need help with resources and integrating them into my system

Godot Version

4.3

Question

So, a kind soul here has recommended using resources as a way to differentiate card types in my card game from a base, generic card. I also made a generic resource that will have all the base behaviours all cards will have if not said otherwise:


that is, the only thing a generic card can do is attack. It’s damage is based on the opponent’s size and defense stat, along with the card’s own atk stat, so i made the opponent card object a parameter for the doAtk function (idk if this would work, haven’t been able to test it). Im still unsure if i’d be able to grab an object’s variables like i did there (opp.size, opp.def).

My plan is to have scripts that extend the critter_base script, overriding the behavious that deviate from the generic:


The critter’s stats and sprite would also be stored in this subclass specific to each card type, but i’ve been unsucsessful in getting any progress from this setup. I dont know if doing what im trying to do the way im doing it is possible, so if anyone has suggestions i’d love to hear them.

One more thing, im aware this is very much not resources. That’s because, as far as i’ve been able to experiment, resources dont store code, or something.
image
i’ve tried creating specific resources for each creature and porting them into the generic card node, but i’m unable to acsess the behaviours and specific stats that are supposed to be stored in the critter specific code, such as not being able to call for functions in other scenes when i have the generic card scene with a specific creature resource atatched. Am i missing something, or do i need to find another approach?

Here’s a screenie of my EnemyData I’m working on right now.

Bottom left, in green, you can see a list of the enemy resources. After you’ve created a .tres file, when you open it, it will be emtpy. On the right side, you assign it a script that you’ve created that will contain the values (variables) of each unit. The center of the screen shows what it looks like in my example (don’t judge :sweat_smile: :crazy_face:). I export those variables so I can edit them from the base values in the right side of the screenshot.

Now, a defined unit will load that .tres file and assign those values. From there you should figure it all on your own.

A resource is a data container not a node, so it doesn’t exist in the tree, it’s only referenced. So, those ready functions will never fire.
I think the step you are missing is actually creating an instance of your resource. Once you have the script you make a resource in the file tree. Once you have that, you can just drag your texture into the texture variable in the editor.

Accessing the data of your card will happen in the generic scene you created. When I do it, I have a variable in the root of the scene that accepts the resource and propagates the information via a setter. It looks like this:

@export var resource : Resource:
	set (value):
		resource = value
		initial_setup()

func initial_setup():
	Critter_base.texture = resource.sprite
	Critter_base.health = resource.hp

As for the generic attack (and any other functionality) those functions would also live in a scene script, you would just use “resource.atk” rather than “atk”. You can put methods in resources, but I’ve largely found it to be a bit of a trap. That last bit is an opinion, so take it with a grain of salt.

1 Like

this seems incredibly useful, do you mind explaining this part for me? i tried searching about it but it didnt really clear anything up

    set (value):
	    resource = value
	    initial_setup()

That’s called a setter. It and it’s companion (the getter) are super useful for controlling what happens when a variable is accessed either to read(get) or write(set).

Mini tutorial here.

tysm! i think it all worked out

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.