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

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Eroica
Prole
Posts: 9
Joined: Sat Feb 28, 2015 12:38 pm

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

Post by Eroica » Sat Sep 12, 2015 3:48 pm

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?

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

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

Post by bakpakin » Sat Sep 12, 2015 4:00 pm

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. :crazy:
((_((_CRAYOLA_((_((_> GitHub <_((_((_CRAYOLA_((_(()

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

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

Post by Eroica » Sat Sep 12, 2015 4:11 pm

Oh hey, pretty quick reply! :awesome:

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

Post by meowman9000 » Fri Mar 11, 2016 9:51 pm

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

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

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

Post by bakpakin » Sat Mar 12, 2016 1:55 am

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

Post by meowman9000 » Sat Mar 12, 2016 6:15 am

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

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

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

Post by bakpakin » Mon Apr 11, 2016 12:35 pm

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

Post by meowman9000 » Thu Apr 21, 2016 2:04 am

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?

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

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

Post by bakpakin » Thu Apr 21, 2016 12:39 pm

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

Post by fedkanaut » Fri Jun 03, 2016 11:54 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest