Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Entity system ?

edited August 2013 in Questions Posts: 129

I've been struggeling with entity systems lately, and can't seem to get it to work.

I am watching a dudes tutorial on löve2d and how to make a entity system, maybe I can apply it to Codea?
(Video: from there and forth of the series)
So I try, and fail, and struggle, and get frustrated, and dissapointed, and blah, and blah...

In Löve you actually have to handle with files compared to Codeas tabs, and this youtubers way forces me to use files (kinda).

Actually I'll just say it straight out: how do you make a entity system?
I know the concept of such a system, go trough each entity and update them bit for bit. But I do not know how to make such system.

For those who don't know: Löve 2D is a engine for a Windows, Mac, and Linux that let's you make lightweight games in Lua. (http://love2d.com/)

Tagged:

Comments

  • edited August 2013 Posts: 175

    @akaJag An Entity is a term (or another way of saying) the representation of the state of an object. An entity system is simply a standardised way of working with entity objects. Start off with two classes a "Entity Controller" class and a base "Entity" class.

    The entity controller should only use / care about methods / functions directly defined in the "Entity" class, these functions and attributes should apply to any entity ( there will likely be few) for example an id attribute, position attribute, a initialise function ( which is not the normal init function) and a set position function.

    Then have all your objects inherit from the entity base class, the controller will need a list of potential entities it can create, or upon creation entities register with their controller through their constructor.

    Essentially you abstract away all the common details and handle them in one place and then your specialist entires make use of them, following the rules you set in the entity base class :)

    With abstraction there is no right or wrong way to do it but it does require consistency and some careful thought. All your "entities" need to work within the constraints and standardised ways of doing thing imposed in the base entity class. How you do these things though is completely up to you :)

  • edited August 2013 Posts: 129

    Conclusion: don't make an entity system ;(

  • edited August 2013 Posts: 175

    My recommendation would be the opposite, do make an entity system :D its really very simple and you can tailor it for your own needs! You just make a pattern for yourself to follow which makes all your game objects easier to work with, because you have a standard way of doing things with them :). Then you only have to worry about object specific behaviour as everything else is already handled because you can re-use your code :)

    Here's a very simple one similar to the one in the video :)

    Base entity, holds common properties and functions, here it just holds the position, I've omitted even an id to keep things simple :) having the position at the base means that any entity can have its position set.

    Entity= class()
    
    function Entity:init(pos)
       self.pos = pos or vec2(0,0)
    end
    
    function Entity:setpos(pos)
    self.pos = pos or vec2(0,0)
    end
    

    The controller class in this case is a global class and it allows you to create entities and pass parameters to their constructors. You setup your entity keys in the init function and then use them in create. The value is a function which creates the class represented by your key.

    EntityController = class()
    
    EntityContoller.entities = {}
    
    function EntityController.init()
    EntityContoller.entities["Cube"] = function(...) return Cube(...) end
    end
    
    function EntityController.create(name,...)
        if EntityContoller.entities[name] ~= nil then
    
             return EntityContoller.entities[name](...)
       end
    end
    

    The cube here is an example object, you can set its position because it inherits the behaviour from the base entity class

    Cube = class(Entity)
    
    function Cube:init(pos)
    Entity.init(self,pos)
    
    end
    

    Then in your main to create a cube you'd do something like this. Notice how you can pass pos through to the cube through the create function :)


    function setup() EntityController.init() local cube = EntityController.create("Cube",vec2(1,1)) end

    This is just a simple example and there would be more required for use in a game but i hope this makes it a bit clearer :)

  • Posts: 129

    Oh my god, you're my hero! Thank you very much!! :)>-

  • Posts: 129

    Now for updating, how do I do this properly?

    With the youtubers way you go ents:update() and it went trough a list and updated each one.

    But how do I make it in this system?

  • edited August 2013 Posts: 129

    Poop

  • edited August 2013 Posts: 175

    @akaJag Whoops! i should of just initialised the pos instead of taking shortcuts ;) I have amended now :)

    The update function is where the ids come into it, when you create an entity you want to give it an id in the EntityController.create function and then remember it in another table then you can update entities like this:

    Slightly modified entity base to add in id.

    Entity= class()
    
    function Entity:init(pos)
       self.id = -1
       self.pos = pos or vec2(0,0)
    end
    
    function Entity:setpos(pos)
    self.pos = pos or vec2(0,0)
    end
    
    

    Modified version of the entity controller which assigns ids and tracks instances. You may also want a "dispose" method on here to remove entity instances from the instances list or have a weak table for instances but that requires a new metatable ;)

    EntityController = class()
    
    EntityContoller.entities = {}
    EntityContoller.nextid = 1
    
    EntityController.instances={}
    
    function EntityController.init()
    EntityContoller.entities["Cube"] = function(...) return Cube(...) end
    end
    
    function EntityController.create(name,...)
        if EntityController.entities[name] ~= nil then
    
    -- create entity as before and set id
             local new = EntityController.entities[name](...)
             new.id = EntityController.nextid
    
    -- track entity and increment next id
             EntityController.instances[new.id]=new
             EntityController.nextid = EntityController.nextid+1
    
             return new
       end
    end
    
    function EntityController.update()
         for id,entity in pairs(EntityController.instances) do
    -- update entity 
         end
    end
    
    

    Edit: Note for sanity reasons ;) you should only use things in the entity base class in the update function, that is functions and attributes / properties that are applicable to all entities, nothing specific to particular entities.

  • edited August 2013 Posts: 129

    NEVERMIND ME!

  • Posts: 129

    Awesome! It works! :D

Sign In or Register to comment.