Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relationships #79

Closed
Ziboo opened this issue Apr 1, 2023 · 8 comments
Closed

Relationships #79

Ziboo opened this issue Apr 1, 2023 · 8 comments
Labels

Comments

@Ziboo
Copy link

Ziboo commented Apr 1, 2023

Hello,

Do you have any best practices to create relationships between entities ?

For instance I have a Farm component and a Plant Component

  • I can create multiple entites with a Farm
  • Create multiples entities with Plant
  • I would like to link this Plants with a specific Farm

Not sure what would be the best things to do...

A list of Plants or Entity Ids on the Farm class ?

Thanks a lot !

@Ziboo Ziboo added the question label Apr 1, 2023
@benmoran56
Copy link
Owner

Hi @Ziboo,

Generally speaking, entities shouldn't directly relate to each other directly. One specific entity shouldn't be tagged directly to any other specific entity.

If you have a case where all the type A entities need to interact with all the type B entities, you can usually push this logic up to the Processor.

You can also use single Components to "tag" entities.

What is the type of behavior that you're trying to accomplish?

@Ziboo
Copy link
Author

Ziboo commented Apr 3, 2023

Hi

I need to be able to link a Plants to a specific Farm. I can have multiple Farms.

I think the simplest example where relationship are useful is Inventory for instance.
A game can have multiple Inventories and Items should be linked in some way to an Inventory

Right now for my exemple above I'm storing the Farm entity id on the plant component for filtering later but it's not ideal

@dataclass
class Plant:
    farm : int = -1
    growth: float = 0
    max_growth: float = 300
    
@dataclass
class Farm:
    pass

# somewhere in the code, need to filters Plants to a selected Farm

...
for entity, (plant, water) in game.GAME.ecs.get_components(Plant, WaterContainer):
            if plant.farm != self.selected_farm_entity:
                continue
 ....   

@gretkierewicz
Copy link
Contributor

gretkierewicz commented Apr 14, 2023

As I understand it, Farm should be entity here, not the Component. The question is, why do you want it to be Component in the first place.

Regardless, I have similar question, so will just post it here as another example.
I'm creating roguelike as fun-side-project using this package.

Is making Target Component owning entity as attribute fine? This seems to violate a bit what was written here, about making pure Components. @benmoran56

@gretkierewicz
Copy link
Contributor

Probably I've found some answers here: https://ajmmertens.medium.com/building-games-in-ecs-with-entity-relationships-657275ba2c6c

@benmoran56
Copy link
Owner

@gretkierewicz Sorry I missed your messages. I only briefly looked at that blog post, but I do use the concept of a Target component myself sometimes.

@gretkierewicz
Copy link
Contributor

Yeah I'm using that for now too, but I think when my project will grow I'll implement relationship feature anyway. If so, I'll create pull request with it as well

@benmoran56
Copy link
Owner

Closing this for now, as there isn't really a single recomended method. However, using a "Target" style of Component seems reasonable enough.

@Felecarpp
Copy link
Contributor

I made some tests using a str instead of type(component) as a key for get_components in World. I can format str before get_component to have the target in the key.
For example, I use f"OwnedBy{player}" component key, player is an entity, so I can get all items holded by a player with get_components easily. Owever, I lost the typing of get_components.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants