discord / discord-api-docs

Official Discord API Documentation
https://discord.com/developers/docs/intro
Other
5.91k stars 1.25k forks source link

Invalid C code in game sdk starter guide #5986

Open bababooey1234 opened 1 year ago

bababooey1234 commented 1 year ago

Description

Following the game sdk getting started tutorial at https://discord.com/developers/docs/game-sdk/sdk-starter-guide#code-primer-unreal-engine-c raises the compilation error error: storage size of 'events' isn't known

The game sdk provides the following snippets for utilising the game sdk library in C:

#pragma pack(push, 8)
#include "discord_game_sdk.h"
#pragma pack(pop)
struct Application {
    struct IDiscordCore* core;
    struct IDiscordUsers* users;
};

struct Application app;
// Don't forget to memset or otherwise initialize your classes!
memset(&app, 0, sizeof(app));

struct IDiscordCoreEvents events;
memset(&events, 0, sizeof(events));

struct DiscordCreateParams params;
params.client_id = CLIENT_ID;
params.flags = DiscordCreateFlags_Default;
params.events = &events;
params.event_data = &app;

DiscordCreate(DISCORD_VERSION, &params, &app.core);

However, the line struct IDiscordCoreEvents events; raises the following compilation error in the TDM64-GCC compiler:

error: storage size of 'events' isn't known

This seems to be due to the fact that struct IDiscordCoreEvents is defined with typedef void* IDiscordCoreEvents; in discord_game_sdk.h, although I'm not sure why that would cause an error, as replacing the line in the code with void *events; seems to work just fine.

Steps to Reproduce

main.c:

#include <stdio.h>
#pragma pack(push, 8)
#include "c/discord_game_sdk.h"
#pragma pack(pop)

struct Application {
    struct IDiscordCore *core;
    struct IDiscordUsers *users;
};

int main(void) {
    struct Application app;
    memset(&app, 0, sizeof(app));

    struct IDiscordCoreEvents events;
    memset(&events, 0, sizeof(events));

    struct DiscordCreateParams params;
    DiscordCreateParamsSetDefault(&params);

    params.client_id = clientid;
    params.flags = DiscordCreateFlags_Default;
    params.events = &events;
    params.event_data = &app;

    enum EDiscordResult result = DiscordCreate(DISCORD_VERSION, &params, &app.core);

    if(result)exit(result);

    puts("Application Created");
}

Compile with: gcc -Wall -o main.exe -L -l discord_game_sdk.dll main.c (with discord_game_sdk.dll and discord_game_sdl.dll.lib in same directory as main.c)

Expected Behavior

Setup completing without error

Current Behavior

error: storage size of 'events' isn't known

Screenshots/Videos

No response

Client and System Information

Compiler: TDM64-GCC version 10.3.0

OS: Microsoft Windows 11 Home Version 10.0.22621 Build 22621

bababooey1234 commented 1 year ago

Found the issue: the documentation says "struct IDiscordCoreEvents events;", when it should just be "IDiscordCoreEvents events;"

Pirulax commented 1 year ago

Really weird design choice to not just do the usual:

typedef struct {
    // stuff
} Application;

[This way struct doesn't have to be explicitly stated for variables]