[Online game client business logic] using LUA to develop business logic under unity

Source: Internet
Author: User
Tags lua

Our game has such a scenario: the role of the client needs to use some guild data, but the server will not be in the player (after the corner) into the game will be pushed to the player, but need the client to the server when needed to request the guild's data, the previous implementation is to request the message when the call to add a callback function, As soon as the message comes back, execute the callback function to continue the subsequent business! But there is a lack of such a finding:

1. Sometimes the use of a good number of parameters, which requires that each time a number of irrelevant parameters to the message requestor, and then callback to the parameters are returned intact, very cumbersome;

2. This kind of callback writing feeling is not in line with the human order of thinking habits, a bit chaotic;

The LUA code for callbacks resembles the following:

--the parameters of the @callbackFunc band may be (self, p1, p2, ..., [and finally Alliancedata, which is added when the data is distributed)]functionAllianceproxy:requestalliancedata (Callbackfunc)--Handle send Request MSG    --here, use a uniquekey to register this callback with a manager.EndfunctionAllianceproxy:responsealliancedata (alliancedata)--handle receive response msg    --here the message is distributed according to the above UniqueKey, and the callback is executed, similar to the invocation: Callbackfunc (Self, p1, p2, ..., alliancedata)End--This is how the Guild data is requested:functionRole:handlealliancedata ()Local function _callbackfunc(Self, p1, p2, alliancedata)--use of business logic to alliancedata processing related    Endallianceproxy:requestalliancedata (Packing (_callbackfunc, self, p1, p2))--the packing returned is still a functionEnd

The parameters above are self, p1, p2, ... And so there is no need to throw it into the requestalliancedata, but just because after receiving the callback need to use, as obsessive-compulsive disorder, I think this writing is very disgusting, so today I think, this can fully use the mechanism provided by LUA to do this thing, the general idea of the code is as follows: (Note: The following code has not been compiled and tested, and the following code will be perfected if the table of its main function is destroyed when the execution of the co-process is executed)

--Create and run a co-process--@param mainfunc main functionfunctionGlobal:createandrunningco (MainFunc) self:assertfmt (type(mainfunc) = ='function','func=%s is not function',ToString(MainFunc))LocalCO =coroutine.create(MainFunc)Localissuccess, errmsg =Coroutine.resume(CO, CO) self:assertfmt (Issuccess,'One error happened in mainfunc:%s', ErrMsg)Endfunctionglobal:checkcoisrunning (CO) self:assertfmt (type(CO) = ='Thread')    LocalCurco =coroutine.running() self:assertfmt (CO= = Curco,'co[%s] is isn't running, current running Coroutine was%s, please sure this co is running', CO, Curco)EndfunctionGlobal:yieldandsaveco (Key, CO) self:assertfmt (key~=Nil) self:checkcoisrunning (CO)ifCot[key] = =Nil  ThenCot[key]= {}    End    Table.insert(Cot[key], CO)return Coroutine.yield()EndfunctionGlobal:resumeandremoveco (Key, ...) ifCot[key] Then         for_, COinch Ipairs(Cot[key]) Do            if Coroutine.status(CO) = ='suspended'  Then                Localissuccess, errmsg =Coroutine.resume(Co, ...) Self:assertfmt (issuccess,'One error happened in mainfunc:%s', ErrMsg)End        EndCot[key]=Nil    EndEnd
LocalGlobal =require('Global') Global:createandrunningco (function(CO)LocalAlliancedata = Alliance:requestalliancedata (CO)--Do somethingEnd)functionAllianceproxy:requestalliancedata (CO)--Send msg:request Alliance Data    returnGlobal:yieldandsaveco ('Alliancedata', CO)EndfunctionAllianceproxy:responsealliancedata (alliancedata)--receive msg:response Alliance DataGlobal:resumeandremoveco ('Alliancedata', Alliancedata)End

Users who need to get Guild data can use the following notation:

Global:createandrunningco (function(CO)    local alliancedata = Alliance:requestalliancedata (co)    --do Somethingend)
And then in the message to do the corresponding processing on the line, this writing with the previous comparison, it is clear that our order to write the corresponding logic on the line, there is no need to like the previous say, such as callback came back and then execute the corresponding logic

[Online game client business logic] using LUA to develop business logic under unity

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.