Y-samp / Y-Core

Core Y gamemode
9 stars 3 forks source link

warning 229: index tag mismatch #2

Closed Vasily-X closed 6 years ago

Vasily-X commented 6 years ago

Seems this code generates warnings:

#define Player.                     OO_TYPE(PLAYER,Player)

enum E_PLAYER_CORE
{
    E_PLAYER_Core_id,
    E_PLAYER_Core_password[129],
    E_PLAYER_Core_language
}
new PlayerCore[MAX_PLAYERS + 1][E_PLAYER_CORE];

hook OnGameModeInit()
{
    Player.Core[MAX_PLAYERS][@id] = 0;
    Player.Core[MAX_PLAYERS][@password][0] = EOS;
    Player.Core[MAX_PLAYERS][@language] = -1;
    return Y_HOOKS_CONTINUE_RETURN_1;
}

hook OnPlayerConnect(playerid)
{
    memcpy(PlayerCore[playerid], PlayerCore[MAX_PLAYERS], 0, MAX_PLAYERS * 4, MAX_PLAYERS);
    return Y_HOOKS_CONTINUE_RETURN_1;
}

I get two warnings from the memcpy function:

warning 229: index tag mismatch (symbol "PlayerCore")
warning 229: index tag mismatch (symbol "PlayerCore")

If I'm not wrong the macros add tags to the arrays but I don't understand them at all. If I user Player.Core[playerid] instead it generates an error: error 017: undefined symbol "Player@Core"

Any idea about how to avoid this? Thank you.

Misiur commented 6 years ago

Hello!

So, you are declaring an array with one extra empty slot to be a blueprint for each freshly connected user. So, first, to get rid of the index mismatch:

memcpy(PlayerCore[playerid][E_PLAYER_CORE:0], PlayerCore[MAX_PLAYERS][E_PLAYER_CORE:0], 0, MAX_PLAYERS * 4, MAX_PLAYERS);

I'm a litle rusty here, but basically the function expects one dimensional array, and the enumerated array is a bit of a different beast. So that's what I usually do to make it work.

Now, with that dealt with, I think you've made a mistake. You want to copy one "slot" of the PlayerCore. However, you are copying "MAX_PLAYER" bytes - that's not what you want in this case. This will override next slots and will cause mayhem. You need to copy "size of the enum" * 4 bytes. Luckily, the enum identifier provides its size! Just remember to override the tag to "tagless", or there'll be more compiler warnings. So:

memcpy(PlayerCore[playerid][E_PLAYER_CORE:0], PlayerCore[MAX_PLAYERS][E_PLAYER_CORE:0], 0, _:E_PLAYER_CORE * 4, _:E_PLAYER_CORE);
Vasily-X commented 6 years ago

@Misiur you're totally right. I'll go your way now, looks much cleaner.

It compiles fine, thanks for checking this out.