ECS -- is it ok to have a method/function as a component ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
RNavega
Party member
Posts: 456
Joined: Sun Aug 16, 2020 1:28 pm

Re: ECS -- is it ok to have a method/function as a component ?

Post by RNavega »

lrdprdx wrote: Sun Mar 23, 2025 2:13 pm First of all, one more time thank you for your participation and help. It's insane how you're involved !
As a hobbyist I've never really learned about ECS, so this is a good opportunity to look into it a bit more.
P.S. I apologize for any kind of rude words if any --- it is just my reaction in this particular case and context.
There's no rudeness, but I appreciate the consideration. Keep writing like you normally do.
states like "RUN_SAD", "RUN_HAPPY", etc.
OR
a std::map<std::pair<EState, EMood>, std::vector<animation>>.
Another idea is to have a property string inside the animation component which is a general key inside its animation pool table to get the right animation:
(code)
I think all of those are essentially the same in terms of "information entropy". That is, "RUN_SAD" is no different than [run][sad], where you're using an underscore to establish the hierarchy in one or using nested tables in the other. You have to use 'something', so any of those is good.
One thing though is that there's a dependency between those keys: in your design the mood key 'must' come after the action key, and all animations 'must' have a mood, direction and action.
So maybe something like a tag system (not like ECS tag, but like an orderless categorization) to identify animations could be an alternative, because then you don't have to worry about the ordering of the keys or the presence / lack of them.
So searching for the animation under (sad, run) or (run, sad) would yield the same data.

Code: Select all

-- Animation tag order map.
animation_tags = {
    -- Action tags.
    walk  = 1,
    fly   = 1,
    run   = 1,
    jump  = 1,
    -- Mood tags.
    happy = 2,
    sad   = 2,
    -- Direction tags.
    left  = 3,
    right = 3,
    up    = 3,
    down  = 3,
    -- Some other type of tag.
    --bla = 4,
    --blu = 4,
    --(...)
}


local sorted_tags_helper = {}


function make_animation_key(a, b, c, d)
    -- Alternative: use a FOR loop with select() for a cleaner & unlimited number
    -- of parameter tags, but it's not as fast as this unrolled way because of the extra
    -- function calls.
    sorted_tags_helper[1], sorted_tags_helper[2],
                           sorted_tags_helper[3],
                           sorted_tags_helper[4] = '', '', '', ''

    local tag_index = animation_tags[a]
    if tag_index then sorted_tags_helper[tag_index] = a end

    tag_index = animation_tags[b]
    if tag_index then sorted_tags_helper[tag_index] = b end

    tag_index = animation_tags[c]
    if tag_index then sorted_tags_helper[tag_index] = c end

    tag_index = animation_tags[d]
    if tag_index then sorted_tags_helper[tag_index] = d end

    return (sorted_tags_helper[1] .. sorted_tags_helper[2] ..
            sorted_tags_helper[3] .. sorted_tags_helper[4])
end


-- Player
animation {
    -- WALK / SAD / LEFT.
    [make_animation_key('walk', 'sad', 'left')] = {
        asset = 'assets/actors/player/walk_sad_left.png',
        frames = {4, 5, 6, 7},
        --(...)
    }
    -- FLY / LEFT.
    [make_animation_key('fly', 'left')] = {
        asset = 'assets/actors/player/fly_all_left.png',
        frames = {1, 2, 3, 4},
        loop = true,
        --(...)
    }
}

Code: Select all

> print(make_animation_key('sad', 'up', 'walk'))
walksadup
Looking at those discussion threads linked earlier, there's so many different opinions on how to do things. I think this is because ECS is flexible, so everyone comes up with their own variations of how to do them, so if you find something that's working for you then do push on. As long as your game is working like what you want then IMO that's great already, as the players don't know/care about what paradigm you're using under the hood.
If you're stuck somewhere, then like Dan Harmon said (in the context of screenwriting): "prove you're a bad writer".
User avatar
marclurr
Party member
Posts: 164
Joined: Fri Apr 22, 2022 9:25 am

Re: ECS -- is it ok to have a method/function as a component ?

Post by marclurr »

I had a go at making a platformer using an ECS a few years ago. I found there to be very few non-trivial ECS examples at the time so I was mostly making it up as I went, but I it was very fiddly to make a platform game this way for me. I think in the end I had a "script" component that was essentially just OOP but worse, and even at the time I hated the solution. I just couldn't decompose my problem in a way that fit the ECS style well. Highly likely a skill issue.

The code is public in case you want some inspiration but there's definitely better ways to solve the problems https://github.com/marclurr/love2d-platformer
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests