[Library] tiny-ecs - Fast Simple Entity Component System

Eroica
Prole
Posts: 9
Joined: Sat Feb 28, 2015 12:38 pm

Re: [Library] tiny-ecs - Fast Simple Entity Component System

Hi, I hope it's alright to revive this thread for a question that I have about tiny-ecs:

First of all, very interesting and useful library! I currently use LÖVE for an app prototype that is in need of an ECS, and in the end decided on tiny-ecs.

However, I'm wondering how I can toggle a System so that it is enabled/disabled depending on its current status.

Consider the following: If I press "P", the screen should get dark and "Pause" should appear. When I press "P" again, the screen should become normal again.

I got it working with something like this:

Code: Select all

function love.load()
WORLD = tiny.world()
pauseDrawSys.active = false
end

[...]

function love.keypressed(key)
if key == "p" then
pauseDrawSys.active = not pauseDrawSys.active
end
end
... But I'm not sure whether this uses a system's "active" field like it was intended. I also need two global variables (WORLD, pauseDrawSys) for this to work.

So far, I haven't found anything about toggling in the API documentation. Do you think there is another way to achieve this?

bakpakin
Party member
Posts: 114
Joined: Sun Mar 15, 2015 9:29 am
Location: Boston

Re: [Library] tiny-ecs - Fast Simple Entity Component System

Yes, that is the way system.active is meant to be used. It should be documented in the API somewhere (hopefully).

While tiny-ecs has an API and a demo, it doesn't have much in the way of a tutorial. Maybe I should make one.
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()

Eroica
Prole
Posts: 9
Joined: Sat Feb 28, 2015 12:38 pm

Re: [Library] tiny-ecs - Fast Simple Entity Component System

I think I was a little bit confused about the wording on system.active in the API, but it sounds logical now. Good to know that it was the right way after all! Thanks for taking your time!

meowman9000
Prole
Posts: 15
Joined: Thu Mar 10, 2016 3:47 pm

Re: [Library] tiny-ecs - Fast Simple Entity Component System

I am trying to copy the kibbles guide but its throwing me an error, any idea what I'm doing wrong? Is there perhaps a simpler guide somewhere for newbs. I also cant seem to see where the entities are being declared in the Kibbles example.
Error: src/entities/Joe.lua:3: attempt to call global 'class' (a nil value)
stack traceback:
src/entities/Joe.lua:3: in main chunk
[C]: in function 'require'
main.lua:2: in main chunk
[C]: in function 'require'
[string "boot.lua"]:428: in function <[string "boot.lua"]:274>
[C]: in function 'xpcall'

Code: Select all

tiny = require "lib.tiny"
joe = require "src/entities/Joe"
TalkingSystem = require "src/systems/TalkingSystem"

local world = tiny.world()



Code: Select all

--Entity joe

local Joe = class "Joe"

function Joe:init()
self.name = "Joe"
self.phrase = "I'm a plumber."
self.mass = 150
self.hairColor = "brown"
end

return Joe


Code: Select all

--System TalkingSystem
local TalkingSystem = tiny.processingSystem(class "TalkingSystem")

function TalkingSystem:init(target)
self.target = target
end

talkingSystem.filter = tiny.requireAll("name", "mass", "phrase")

function TalkingSystem()

function talkingSystem:process(e, dt)
if not self.target then
return
end
e.mass = e.mass + dt
love.graphics.print(e.name .. ", who weighs " .. e.mass .. " pounds, says, \"" .. e.phrase .. "\"")
end
end


bakpakin
Party member
Posts: 114
Joined: Sun Mar 15, 2015 9:29 am
Location: Boston

Re: [Library] tiny-ecs - Fast Simple Entity Component System

The problem seems to be in your attempt to create a Talking system. You are trying to use the global 'class' function, which is not part of standard Lua or tiny-ecs. In the demo, I am using 30log for classes. Another popular choice is middleclass. If you want to use classes like in the demo, you must set up these libraries. Its quite easy.

Besides that, I suspect your talking system would not work. Here is some hopefully fixed code (assuming you can install 30log or middleclass.)
Im not sure what the target variable is supposed to do, but I left it in.

Code: Select all

--System TalkingSystem
local TalkingSystem = tiny.processingSystem(class "TalkingSystem")

TalkingSystem.filter = tiny.requireAll("name", "mass", "phrase")

function TalkingSystem:init(target)
self.target = target
end

function TalkingSystem:process(e, dt)
if not self.target then
return
end
e.mass = e.mass + dt
love.graphics.print(e.name .. ", who weighs " .. e.mass .. " pounds, says, \"" .. e.phrase .. "\"")
end
end

return TalkingSystem

Best of luck, post if you have any more problems.
Last edited by bakpakin on Sat Jun 18, 2016 9:29 pm, edited 1 time in total.
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()

meowman9000
Prole
Posts: 15
Joined: Thu Mar 10, 2016 3:47 pm

Re: [Library] tiny-ecs - Fast Simple Entity Component System

Ah thanks for the help, it all makes sense now.
I got it working, heres the code:

Code: Select all

--Entity joe
local Joe = {
name = "Joe",
phrase = "I'm a plumber.",
mass = 150,
hairColor = "brown"
}

return Joe


Code: Select all

local TalkingSystem = tiny.processingSystem()

TalkingSystem.filter = tiny.requireAny("name", "mass", "phrase")

function TalkingSystem:process(e, dt)
e.mass = e.mass + dt
love.graphics.print(e.name .. ", who weighs " .. e.mass .. " pounds, says, \"" .. e.phrase .. "\"")
love.graphics.print("fish")
end

return TalkingSystem


Code: Select all

tiny = require "lib.tiny"
Joe = require "src/entities/Joe"
TalkingSystem = require "src/systems/TalkingSystem"

local world = tiny.world()

end

function update()
if world then
love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 20, 30)

--Runs every entity
world:update(dt)

end

end


bakpakin
Party member
Posts: 114
Joined: Sun Mar 15, 2015 9:29 am
Location: Boston

Re: [Library] tiny-ecs - Fast Simple Entity Component System

It's been a while, but there have been a few updates that haven't really been mentioned.

First, I rewrote most of the filter code to be faster and used generated code rather than closures. Not that interesting, honestly.

More importantly, I added a new function, tiny.filter, that allows one to create filters with regex-like syntax. The filters created are created with generated code. The syntax is very simple, with alphanumeric strings representing table keys separated by boolen operators & (AND), | (OR), and prefixed with ! (NOT). Parentheses can also be used for grouping.

Examples:

Code: Select all

filter1 = tiny.filter("a&b&c")
filter2 = tiny.filter("a|b|c")
filter3 = tiny.filter("(position|sprite)&!isPlumber")

This code should be both on github and in luarocks.
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()

meowman9000
Prole
Posts: 15
Joined: Thu Mar 10, 2016 3:47 pm

Re: [Library] tiny-ecs - Fast Simple Entity Component System

A question about Entity component systems, is it suggested that everything becomes an entity, including things like drawing the backgrounds? Home encompassing should they be?

bakpakin
Party member
Posts: 114
Joined: Sun Mar 15, 2015 9:29 am
Location: Boston

Re: [Library] tiny-ecs - Fast Simple Entity Component System

meowman9000 wrote:A question about Entity component systems, is it suggested that everything becomes an entity, including things like drawing the backgrounds? Home encompassing should they be?
That is totally up to you. I personally like to make a really simple processing system that only filters drawable entities on a certain layer. I can then have multiple layers, like background and foreground, and entities added to the scene with a layer component and a draw component will be drawn at the right depth. This will work for backgrounds.
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()

fedkanaut
Prole
Posts: 6
Joined: Fri May 27, 2016 4:44 pm

Re: [Library] tiny-ecs - Fast Simple Entity Component System

Hi, love the library, thanks for your hard work. I'm trying to filter systems in tiny.update(), I'm not sure exactly what kind of filter to pass in the third argument. I've tried things like tiny.filter("componentThatSystemIWantFiltersFor") or passing the actual system's filter but nothing seems to be working.

Edit: Never mind, I got it to work by passing the same filter as the system. Pretty sure I just confused myself.

Who is online

Users browsing this forum: groverburger, HDPLocust and 6 guests