Jump to content

[SOLVED] how can i separate a network function so i can run it each frame?.


photo

Recommended Posts

ull disclosure: i am not a very experienced C++ developer so i think i am asking something basic.

i have this snippet of code that start my client:

{
    printf("\nconnecting client (insecure)\n");

    double time = 100.0;
    const double deltaTime = 0.01f;
    ClientServerConfig config;

    uint64_t clientId = 0;
    random_bytes((uint8_t*)&clientId, 8);
    printf("client id is %.16" PRIx64 "\n", clientId);

    ClientServerConfig config;

    Client client(GetDefaultAllocator(), Address("0.0.0.0"), config, adapter, time);
    Address serverAddress("127.0.0.1", ServerPort);

    if (argc == 2)
    {
        Address commandLineAddress(argv);
        if (commandLineAddress.IsValid())
        {
            if (commandLineAddress.GetPort() == 0)
                commandLineAddress.SetPort(ServerPort);
            serverAddress = commandLineAddress;
        }
    }

    uint8_t privateKey[KeyBytes];
    memset(privateKey, 0, KeyBytes);

    client.InsecureConnect(privateKey, clientId, serverAddress);

    char addressString[256];
    client.GetAddress().ToString(addressString, sizeof(addressString));
    printf("client address is %s\n", addressString);

    const double deltaTime = 0.01f;

    signal(SIGINT, interrupt_handler);

    while (!quit)
    {
        
        client.SendPackets();

        client.ReceivePackets();

        if (client.IsDisconnected())
            break;

        time += deltaTime;

        client.AdvanceTime(time);

        if (client.ConnectionFailed())
            break;

        yojimbo_sleep(deltaTime);
    }

    client.Disconnect();

    return 0;
}

but i need this part to be a separate function:

    while (!quit)
{
    
    client.SendPackets();

    client.ReceivePackets();

    if (client.IsDisconnected())
        break;

    time += deltaTime;

    client.AdvanceTime(time);

    if (client.ConnectionFailed())
        break;

    yojimbo_sleep(deltaTime);
}

because this is the actual function that keeps the client connected to the server.

my question is, how can i separate the main function that creates the actual client, and save it as a global value?

as you can see snippet 2 needs client but i cannot just copy or create client because i get the following errors:

enter image description here enter image description here

and here is how the class is made: enter image description here

the thing is i need the separate the function so i can run it each frame but i don't know how i can save client as a global value.

thanks in advance.

Link to comment

Hello,

If I understand the question correctly, then you can try to place the client's logic in the AppSystemLogic. To do this, you can add to the header file:

class AppSystemLogic : public Unigine::SystemLogic
{
public:
    // ...
    AppSystemLogic(int argc, wchar_t *argv[]);
    //...

private:
    void update_client();

    Client *client{nullptr};
    bool is_valid_connection{false};
    float time{0.0f};
};

and add to AppSystemLogic.cpp:

AppSystemLogic::AppSystemLogic(int argc, wchar_t *argv[])
{
    // ...
    client = new Client(GetDefaultAllocator(), Address("0.0.0.0"), config, adapter, time);
    Address serverAddress("127.0.0.1", ServerPort);

    if (argc == 2)
    {
        Address commandLineAddress(argv);
        if (commandLineAddress.IsValid())
        {
            if (commandLineAddress.GetPort() == 0)
                commandLineAddress.SetPort(ServerPort);
            serverAddress = commandLineAddress;
        }
    }

    uint8_t privateKey[KeyBytes];
    memset(privateKey, 0, KeyBytes);

    client->InsecureConnect(privateKey, clientId, serverAddress);

    signal(SIGINT, interrupt_handler);

    is_valid_connection = true;
    // ...
}

AppSystemLogic::~AppSystemLogic()
{
    if (client)
    {
        client->Disconnect();
        delete client;
    }
}

int AppSystemLogic::update()
{
    update_client();

    return 1;
}

void AppSystemLogic::update_client()
{
    if (!is_valid_connection || !client)
        return;

    client->SendPackets();

    client->ReceivePackets();

    if (client->IsDisconnected())
    {
        is_valid_connection = false;
        return;
    }

    time += App::getIFps();

    client->AdvanceTime(time);

    if (client->ConnectionFailed())
    {
        is_valid_connection = false;
        return;
    }
}

When creating the system logic, use the new constructor:

// ...
AppSystemLogic system_logic(argc, argv);
AppWorldLogic world_logic;
AppEditorLogic editor_logic;

Unigine::EnginePtr engine(UNIGINE_VERSION, argc, argv);
// ...

If additional functions are needed to work with the client, then they can simply be added to the system logic.

  • Like 1
Link to comment

@karpych11 Thank you so much!

 

i did tried doing what you said and i gave me a better idea to work with c++  and because of that i was able to make it work!

here is a screenshot of the code:
image.thumb.png.f561dc58060b6a03fe55131b36910026.png

with what you showed me i am now able to make private values and can just use them in my functions, i am so happy you thought me this! :D
now i am actually capable of connecting to a server while the game is running!, now my next step is to have them communicate positions :D i am so exited! thank you!!!
image.png.6d6a1f3ce5ec9be8b43919cb246b0bef.png

Link to comment
  • silent changed the title to [SOLVED] how can i separate a network function so i can run it each frame?.
×
×
  • Create New...