Skip to content

[Network] Feature Implementation

Morgan L edited this page Nov 2, 2024 · 9 revisions
logo

Feature Implementation

Summary

Introduction

logo

Building on the packet choices discussed in the previous section, this page explains how to create new features like packets within the Dimension library. By defining custom packets for example, developers can introduce new game features and expand the library’s functionality to suit unique project requirements.

This guide covers the steps to implement and integrate features, maintaining compatibility with Dimension’s modular, game-agnostic structure.

Packet Creation

Note: All the steps explanations take place into RType directory. Feel free to move the Dimension library directory in your personal workspace.

Create Packet File

In the directory engine/libs/dimension/src/dimension/packet/basics, you have to create your .cpp, .hpp packet file (Example: HelloWorld.cpp & HelloWorld.hpp)

Packet file content

Each .hpp packet file contains at least this structure:

#pragma once
#include "APacket.hpp"

namespace dimension
{
class HelloWorld : public dimension::APacket
{
   public:
    HelloWorld(uint8_t type);

    //Construct new object with serialized data by deserializing them.
    HelloWorld(std::vector<char> &buffer);
    ~HelloWorld();

   public:
    // Serialize class data to binary to make it sendable.
    std::vector<char> serializeData() const 
};
}  // namespace dimension

Each .cpp packet file contains at least this structure:

#include "HelloWorld.hpp"

namespace dimension
{
HelloWorld::HellowWorld(uint8_t type) : APacket(type) { this->_packetDataSize = 0; }

HelloWorld::HelloWorld(std::vector<char> &buffer) : APacket(buffer) {}

HelloWorld::~HelloWorld(){};

std::vector<char> HelloWorld::serializeData() const
{
    std::vector<char> buffer;
    return buffer;
}
}  // namespace dimension

Note: If you want to add custom data, take a look at more complex packets like CreateEntity to see how they're implemented.

Register your packet

To complete the creation of your packet, you need to register it to the packet factory.
To do it so, you have to include your packet into engine/libs/dimension/src/dimension/packet/PacketFactory.hpp:

#pragma once

...
#include "HelloWorld.hpp"

namespace dimension
{
class PacketFactory
{
   ...
};
}  // namespace dimension

Then register your packet into engine/libs/dimension/src/dimension/packet/PacketFactory.cpp:

#include "PacketFactory.hpp"

namespace dimension
{
PacketFactory::PacketFactory()
{
    ...
    this->_packetManager.registerPacket<HelloWorld>();
}

PacketFactory::~PacketFactory() {}
}  // namespace dimension

You can now use your packet into your project.


Previous page: Specifications
Next page: [Engine] Holtzman Engine Documentation

Clone this wiki locally