Page 1 of 1

mob.lua - Entity Manager inspired by jQuery

Posted: Mon May 07, 2018 10:10 pm
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.

Re: mob.lua - Entity Manager inspired by jQuery

Posted: Wed May 09, 2018 9:14 pm
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)

Re: mob.lua - Entity Manager inspired by jQuery

Posted: Thu May 10, 2018 12:47 am
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.

Re: mob.lua - Entity Manager inspired by jQuery

Posted: Thu May 10, 2018 5:40 am
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.

Re: mob.lua - Entity Manager inspired by jQuery

Posted: Thu May 10, 2018 9:58 am
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.

Re: mob.lua - Entity Manager inspired by jQuery

Posted: Thu May 10, 2018 4:54 pm
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.

Re: mob.lua - Entity Manager inspired by jQuery

Posted: Sat May 19, 2018 7:43 pm
by D0NM
The idea of managing entities and filtering is cool.
This may be used for various unit tests.
Plz, keep it up.