Page 2 of 6
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Sat Sep 12, 2015 3:48 pm
by Eroica
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 = WORLD:addSystem(PauseDrawSystem)
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?
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Sat Sep 12, 2015 4:00 pm
by bakpakin
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.
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Sat Sep 12, 2015 4:11 pm
by Eroica
Oh hey, pretty quick reply!
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!
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Fri Mar 11, 2016 9:51 pm
by meowman9000
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()
function love.load()
tiny.addEntity(world, Joe)
tiny.addSystem(world,TalkingSystem)
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
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Sat Mar 12, 2016 1:55 am
by bakpakin
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.
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Sat Mar 12, 2016 6:15 am
by meowman9000
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()
function love.load()
tiny.addEntity(world, Joe)
tiny.addSystem(world,TalkingSystem)
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
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Mon Apr 11, 2016 12:35 pm
by bakpakin
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.
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Thu Apr 21, 2016 2:04 am
by meowman9000
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?
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Thu Apr 21, 2016 12:39 pm
by bakpakin
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.
Re: [Library] tiny-ecs - Fast Simple Entity Component System
Posted: Fri Jun 03, 2016 11:54 pm
by fedkanaut
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.