#Using an anonymous function or a GDScript file as an export variable?

6 messages · Page 1 of 1 (latest)

still scroll
#

Hi Everyone,

I'm working on a roguelike DeckBuilder Tutorial series for Godot 4.
Since these games are quite data-heavy, I created a custom resource type to store all card-related data.

To keep things decoupled and flexible, I want to export a card's effect as a variable as well (see image)

An effect looks something like this:

func apply_effect(targets: Array[Node2D]) -> void:
var damage_action := DamageAction.new()
damage_action.amount = 6
damage_action.execute(targets)

I came up with two solutions:

  1. export card_effect as its own resource. The problem with that is that I would need to create a .tres file and a .gd file for effects and I feel like having two files to implement one function is an overkill and produces a lot of unnecessary files.
  2. export card_effect as a GDScript resource. The problem with this solution is that I cannot write a pseudo-interface with inheritance, which means you always need to remember to use the exact same function signature as in the example above.

Are there any cleaner solutions to this? Can you give some alternatives? Is it possible to export an anonymous function or something like this?
I keep hitting walls with a clean architecture for this, so any tips are highly appreciated! 😊

torpid anchor
#

I would not be afraid of having a Resource for that even if very simple

rocky crest
#

Is there a reason why you need a .tres file for solution 1? You could just make each CardEffect subclass have its own class_name and then make subresources via the inspector.

#

Also, is there a reason why the CardEffect needs to be separate from the CardData? I assume each card would have its own effect, meaning that it would be easier to just make apply_effect be a virtual method rather than a separate class.

Alternatively, if you're trying to split out data and logic, why does the CardEffect need to be a member of CardData then?

still scroll
rocky crest
#

You're very welcome! 🙂