5Mixer / mphx

A little library to let you make multiplayer games easily with Haxe. No longer maintained and better options are available.
MIT License
125 stars 16 forks source link

[suggestion] Add other update function + add dTime #29

Closed yannsucc closed 8 years ago

yannsucc commented 8 years ago

Hi there !

I had to add some 'update function' on the main loop on server side. So i made this :

package serv;

import monitoring.L;
import mphx.serialization.ISerializer;
import mphx.server.impl.FlashServer;

/**
 * Some modification of mphx.FlashServer to add update method BEFORE the classic update of mphx server
 * @author ...
 */
class ImprovedFlashServer extends FlashServer
{
    private var m_updateCallBacks : Array<Float->Void>;

    public function new(hostname:String, port:Int, _serializer:ISerializer=null, buffer:Int=8) 
    {
        super(hostname, port, _serializer, buffer);
        m_updateCallBacks = [];
    }

    override public function start(maxPendingConnection:Int = 1, blocking:Bool = true) 
    {
        //super.start(maxPendingConnection, blocking);
        m_policyServerRef.start();
        trace("Server active on "+host+":"+port+". Code after server.start() will not run. ");
        listen(maxPendingConnection, blocking);
        var m_oldtime : Float = Sys.time();
        while (true) 
        {
            var now = Sys.time();
            update(now - m_oldtime);
            m_oldtime = now;
            Sys.sleep(0.01); // wait for 1 ms
        }       
    }

    override public function update(timeout:Float = 0):Void 
    {
        for (cb in m_updateCallBacks)
            cb(timeout);

              super.update(timeout);
    }

    public function add(updateCb : Float->Void) : Void
    {
        if(!Lambda.has(m_updateCallBacks,updateCb))
            m_updateCallBacks.push(updateCb);
        else
            L.error("Can't add an update function twice", "server");
    }

    public function remove(updateCb : Float->Void) : Void
    {
        m_updateCallBacks.remove(updateCb);
    }
}

Only a simple list of callback Float->Void call before the Main update of mphx. We can add these CB before server.start() with server.add(CB);

I put an operation for the dtime for update(dtime) parameters too.

(don't know why "super.update(timeout);" have an ugly indent here)

5Mixer commented 8 years ago

Hmm, this change doesn't look bad, but I have a few questions. First, you don't go into to much detail about why this change would be important, but I gather it's to add function calls prior to server updates. I've always done this a different way, without start(). Instead I would have my own function code and call server.update() from there, along side any other code. That's because once a servers start method is called, the while loop stops the code from continuing. If you call update by yourself, without using start you can call update frequently along with your other code. You can kind of see what I mean here. I never call start I make my own while loop, in which I call update as well as my own update

I can see having to write your own update a little confusing to beginners (you have to call server.listen as well), so perhaps this change would be worthwhile.

Secondly, is there a reason this is only shown with flash, and not all servers implementations? I have very little experience with flash, so there could be a genuine reason that I'm not picking up on. If this change was to go through, I think applying it to all server implementations is the most consistent approach.

Thanks!

yannsucc commented 8 years ago

It's just a suggest. Your exemple is more logical. :).

I don't know if this feature is important, but sometimes (in my project) i need to check some data before the server update (if the server is on maintenance by checking on a database for exemple) That's why i don't go into to much detail because i think it's specific to the project. I only try to set it generic :)

I use Flash on my own project so i put this code, it's not flash specific, I try to don't change mphx core if i can. This exemple can be extended to all servers implementations.

5Mixer commented 8 years ago

Going to close this because I don't think the library needs this, at least not yet. I don't want it to become cluttered.

As of now, the best way of doing this is to create your own update method instead of using start(), which calls methods before/after server.update().