mob.lua - Entity Manager inspired by jQuery

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
NicMagnier
Prole
Posts: 8
Joined: Mon May 07, 2018 9:46 pm

mob.lua - Entity Manager inspired by jQuery

Post by NicMagnier »

During the last Ludum Dare I experimented with the idea of managing entities and filtering them using a system similar to jQuery. I found it pretty useful so I expanded on the idea and finished a first version of a library.

Usually I do my set of tools for myself but I thought it was interesting enough to be shared, so I put it on github
https://github.com/NicMagnier/mob.lua

Before, I was always creating tons of objects all over the place and all in different tables to easily loop through them (looping through enemies, through particles, and so on). It was working but it always make some horrible looking codes. So here the idea is to query the entities you want with a simply syntax so it very easy to read the source code and also to quickly query the entities you want.

So for example with mob.lua I can randomly pick a npc in a special state that is close by

Code: Select all

mob.get("type_npc @about_to_explode")
	:filter(function(e)
		local dx = e.x - love.mouse.getX()
		local dy = e.y - love.mouse.getY()
		return (dx*dx+dy*dy)<150*150
	end)
	:random()
Or I could draw my entities all properly sorted

Code: Select all

mob.get("on_ground")
	:sort(function(a,b) return a.y<b.y end)
	:draw()
	
-- draw all the cloud and output how many cloud are drawn
love.graphics.setColor(255,255,255,100)
mob.get("cloud"):draw()
I still didn't use this version on a real project but if anyone want to have a look and give some feedback already that would be super interesting.
NicMagnier
Prole
Posts: 8
Joined: Mon May 07, 2018 9:46 pm

Re: mob.lua - Entity Manager inspired by jQuery

Post by NicMagnier »

I've added a couple of functions to easily access specific entities.
The classic example is that we often want to have find the game object which has the biggest or smallest property (like a score, or closest from a position)

To do it we usually do a for loop compare to the best value we have and change to the current entity if we find a better value.

This is so typical that adding these functions will be quite useful

Code: Select all

local leading_player = mob.get("racer"):get_biggest("score")
local player_challenger = mob.get("racer -@finished"):get_closest("score", mob.get_entity("player").score)
(As I write the code, I realise that get_biggest("score") can be misleading since it does return an entity and not the biggest score. mmmh I will have to think about that)
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: mob.lua - Entity Manager inspired by jQuery

Post by raidho36 »

I have a feeling that, if used in a serious project, it will run poorly - just like jQuery. Mainly due to the amount of strings it needs to generate internally to handle any non-trivial query. It doesn't save that much writing, so not really sure what's the point. Also gamedev is not the same as webdev, making something 10% quicker is not nearly as important as that it runs at 10 fps due to framework choices.

If this happens, you've cornered yourself, and your only way of optimizing anything is by stripping it down. Nobody likes when games that look like they belong in 2005 run like they belong in 2025. People could turn a blind eye on that if the game is otherwise good, but if you cheaped out on the programming, chances are the game is also cheap everywhere else.
NicMagnier
Prole
Posts: 8
Joined: Mon May 07, 2018 9:46 pm

Re: mob.lua - Entity Manager inspired by jQuery

Post by NicMagnier »

The point is code readability and not to have to manually loop through your entities. That is maybe not your priority but when I'm working on a project I try to save all the time I have and having a more concise and readable code make it also way easier during debuging.

There is a overhead of course, but much less that you imply. Internally the library is breaking down a query as object to parse entity efficiently. Entity management is commonly not a massive bottleneck so code clarity should have priority.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: mob.lua - Entity Manager inspired by jQuery

Post by raidho36 »

It's not uncommon for jQuery page to run 10 times slower than just JavaScript. So I wouldn't make claims like that without putting it to the test first. Also, if you're limited by the typing speed, it's because you're doing something trivial - which is a typical scenario for a web page rendering, not so much for video game progrmaming.
NicMagnier
Prole
Posts: 8
Joined: Mon May 07, 2018 9:46 pm

Re: mob.lua - Entity Manager inspired by jQuery

Post by NicMagnier »

I was talking about my library. If you want to talk about jQuery you are welcome to debate about it in another topic.
User avatar
D0NM
Party member
Posts: 250
Joined: Mon Feb 08, 2016 10:35 am
Location: Zabuyaki
Contact:

Re: mob.lua - Entity Manager inspired by jQuery

Post by D0NM »

The idea of managing entities and filtering is cool.
This may be used for various unit tests.
Plz, keep it up.
Our LÖVE Gamedev blog Zabuyaki (an open source retro beat 'em up game). Twitter: @Zabuyaki.
:joker: LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua :joker:
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests