Learn KBEngine step by step (4)

Learned to build the official Demo, this is not enough, our purpose is to use KBEngine for our use. So from this time on, we started to learn step by step to build our own project.

1. The asset in the kbe engine folder is the smallest project project, on which we can build our stuff bit by bit.

①First find kbengine_defalut in your kbe, xml (kbe->res->server), open it with vs,

Change the port to 3306 and set your mysql database username and password.

Set this to your database name.

scroll down

Change whether to open registration to true, and set the automatic creation to false when the game database cannot find the game account when the login is legal.

②In the current server resource folder (ie assets), also find kbengine.xml (assets->res->server) and do the same steps as above,

However, the game database cannot be found and the game account is not automatically created when the login is legal only by modifying it.

2. Set the UID of the server (optional)

Some unnecessary steps were skipped when installing KBEngine earlier, and the UID item was missing from the environment variable configuration. Official explanation:

多台硬件服务器共同维护一组服务,
那么每一台机器上的系统uid环境变量都应该保持一致,否则无法形成服务组。

Under windows, you only need to add UID to your environment variable, remember 0<UID<32767

Attach the complete environment variable settings:


The server can also run without setting this environment variable before, because the script for starting the server in the server asset library already has the function of automatically setting the environment variable to start the server correctly.

When you start the server, if you look carefully at the first window that pops up, there will be a UID displayed, but it seems like a flash in the pan, and you won't be able to see it after that. I am obsessed with looking for her figure. One of the advantages under Windows is that the official provides a visualization tool, and I found her in this tool.

Click on guiconsole.bat

Later, I looked back and found that she was in the dim light

This is the machine.exe (first) black window. In addition, the places I marked in red seem to have a sense of deja vu.

3. Start building our client project (unity3d) named MyKBE

① Name a few files first

Plugins store KBE plug-ins. This plug-in can be generated by clicking gensdk.bat in the assets folder of your KBE root directory, and then copying it to the Plugins of MyKBE.

Create two more script folders in Scripts, one for the plugin layer and the other for the client rendering layer.

②Create a new empty object in the Hierarchy panel and name it clientApp, which is the client.

We need to write a script clientApp.cs for it.

See where it is stored. The script content is simple. Add using KBEngine, inherit KBEMain, and delete the rest.

Leave these parameters alone for the time being, just keep the defaults.

Click the play button of Unity, and such information will be displayed under the Console, indicating that the client has been started. After the client is closed, there will be a corresponding display, you can see for yourself.

③ Build a simple login interface

Create a c# script under u3d_scripts, name it ClientLogin, and hang it under Login.

①引用KBEngine和System(using KBEngine;using System;)。

②We first write this code in ClientLogin.cs, go back to u3d, and add a click event to the login Button.

//login request

public void Login()

    {

        Debug.LogFormat("Request login, username:{0},passwd:{1}", username.text, passwd.text);

        //Send the login request code template to the server, except please feel free to change everything else

        KBEngine.Event.fireIn("login", username.text, passwd.text, System.Text.Encoding.UTF8.GetBytes("请随意"));

    }

//registration request

public void Register()
    {

            Debug.LogFormat("Request registration, username:{0}", username.text);

            // Send the registration request code template to the server

      KBEngine.Event.fireIn("createAccount",username.text,passwd.text,System.Text.Encoding.UTF8.GetBytes("请随意"));
    }

②Create a new Account.cs in the kbe_scripts folder (must be this name)

Write this first here, which is the case when the login is successful.

③Add an error prompt when logging in (prompt under Console first)

   //Login failed, error callback
    public void onLoginFailed(UInt16 failedcode)
    {
        Debug.LogFormat("Login failed, error code {0}, failure reason {1}", failedcode, KBEngineApp.app.serverErr(failedcode));

    }

    We need to register the callback function in start to let the server know that there is such a method in this script.

    KBEngine.Event.registerOut("onLoginFailed",this, "onLoginFailed");

④Add a callback function for registration failure

 //Callback function during registration
    public void onCreateAccountResult(UInt16 retcode, byte[] datas)
    {
        if (retcode != 0)
        {
            Debug.LogFormat("Registration failed, error code: {0}, failure reason: {1}" , retcode, KBEngineApp.app.serverErr(retcode));
            return;
        }
        else
        {
            Debug.Log("Account registration succeeded!");
        }
    }

    We also need to register the onCreateAccountResult function.

At this point, our registration and login are finished. After registering an account, you can check whether the account data information is generated in the game database. As for the legitimacy of the account and password, you need to set it yourself, think about how to do it (#^.^#).

Alright, see you next time, Bye~~~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324823072&siteId=291194637