Querying for nodes matching multiple terms

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jaguar1983

I’m working on a Card game and I need to be able to find cards based on various features. I also need to be able to get notifications for when a card is modified to fit the query. Ideally, I’d like to be able to use something built into the engine, or an existing

For example, I may have a card whose effect targets green, vegetation cards with less than 3000 power. If the card is a one-shot effect, it can just query for the cards that fit the description. On the other hand, if the card stays on the field and affects or is affected by cards of that type, it needs to be able to change itself or the target based on changes to the features.

Also, effects need to be able to add and remove effects at will, but not disturb effects from other cards including inherent effects. For example, there are two effects on the field that grant Blocker to a card. If one of them is removed, it needs to remove it’s own instance of Blocker without disturbing the other instance.

So far, I have the features as nodes and each node has a group for it’s type and I’m using get_nodes_in_group to find the first term then filtering the results with is_in_group over the rest of the terms. This is my clunky way of handling the one-shot method, but I’m a little stuck with the change subscription system.

Is there a way in-engine to do this? Or is there an existing add-on that will handle this kind of thing for me?

:bust_in_silhouette: Reply From: godot_dev_

There may be tools the engine offers to make your life easier, but looking at your problem, it seems like this is a programming challenge and will require you to sit down and design your logic. I recommend design it (on paper with writing), carefully consider all situations, and if you are satisfied with your design, then dig into Godot for tools that will help you. E.g., you could use signals to notify whenever a card’s parameters are changed.

You could probably achieve most of what you are trying to do via scripting, and use the Godot engine for visuals. E.g., store cards on the field in an array (or dictionary for quick lookup) and iterate the array when removing cards, while a scene tree node’s children could be used to visually place the cards on the screen.

I was kind of hoping that there was an existing library or feature allow for scene queries to save time and effort.

With using signals, I was looking at add_user_signal to create a signal in the query manager for each unique subscribed query, but I worry that the number of queries might blow out and reduce performance.

I’m very new to Godot, but it seems that one of Godot’s principles is that sub-nodes often modify/augment their parents. My thought was to have the modifiers as sub-nodes so that they could be searched. This would also theoretically allow them to add visual indicators and detail text. Following this principle, I’m thinking that Card might have a Modifier subclass and when a Modifier node is added or removed from a Card node, the Card updates a bitset of features, making queries a simple set of AND operations. I think this would fit with Godot’s paradigms.

jaguar1983 | 2022-11-08 01:51

I believe that changes to the scene tree will take more time than just updating node values, so if you can get away with avoiding the removal, adding, and re-parenting of nodes, that would make it more efficient.

For example, instead of constantly searching the scene tree by iterating over the children, make an array that holds a reference to each child, so searching only inolves iterating over the array not the scene tree. Of course if you modify the scene tree, you will need to update your array.

godot_dev_ | 2022-11-08 15:44