Giter Site home page Giter Site logo

Comments (4)

kingoftheconnors avatar kingoftheconnors commented on June 2, 2024 1

After some experimentation, I figured out that it wasn't the pointer that was the problem, it was the initialization of the array!

GameStateMessage *message = (GameStateMessage*)client.CreateMessage(MessageType::GAME_STATE_MESSAGE);
message->arr = new int[global::MAX_PLAYERS];
for (int i = 0; i < global::MAX_PLAYERS; i++) {
    message->arr[i] = i*2;
}
client.SendMessage(message, UNRELIABLE_UNORDERED_CHANNEL);

This is the client sending the array. Obviously, it declares the array, but when reading the array, it never got initialized!

So this example worked if forcing the array to be size 5:

struct GameStateMessage : public Message
{
    int *arr = new short[5]; // Initalized for both sender and reciever

    GameStateMessage() {}

    template <typename Stream> bool Serialize( Stream & stream )
    {
        for (int i = 0; i < 5; i++) {
            serialize_int(stream, arr[i], -390, 3700);
        }
        return true;
    }

    YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
};

So, pointer array works, right? Well, the main problem is how the reader will know what the length will be. That's the entire point, right?

Taking some inspiration from the serialize_string function, we just need to send the size FIRST! And then use that to dynamically allocate the received array:

struct GameStateMessage : public Message
{
    const int MAX_ARRAY_SIZE = 64;  // This is our theoretical highest array size
    int *arr;
    int length;

    GameStateMessage() {}

    template <typename Stream> bool Serialize( Stream & stream )
    {
        serialize_int( stream, length, 0, MAX_ARRAY_SIZE );
        if (Stream::IsReading)
        {
            arr = new int[length];
        }
        for (int i = 0; i < length; i++) {
            serialize_int(stream, arr[i], -390, 3700);
        }
        return true;
    }

    YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
};

And this works! Tries sending {1, 2, 4, 6, 8}, gets back out {1, 2, 4, 6, 8}

Thanks @Green-Sky for the little extra push to figure this out!

from yojimbo.

Green-Sky avatar Green-Sky commented on June 2, 2024

I recommend running with a debugger.

from yojimbo.

Green-Sky avatar Green-Sky commented on June 2, 2024

Remember to free the memory, or you have a memory leak in your hands.
Personally I would just go with std::vector.

from yojimbo.

kingoftheconnors avatar kingoftheconnors commented on June 2, 2024

Ah, good point! Thanks!

Since this issue is taken care, I'll close it.

from yojimbo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.