Page 1 of 1

structs with unions

Posted: Wed Sep 07, 2016 7:27 pm
by matteus
Does the SGDK support unions? I can't get this code to work!!

Code: Select all

struct Script {
    char type;
    union {
        struct {
            char charName[24];
            char charDialogue[480];
        } CharacterDialogue;
        struct {
            char menuItem1[24];
            char menuItem2[24];
            char menuItem3[24];
            char menuItem4[24];
            char menuItem5[24];
            char menuItem6[24];
        } Menu;
    };
};
enum {
    TypeCharacterDialogue,
    TypeMenu
};

struct Script array[10];
array[0].type = TypeCharacterDialogue;
array[0].CharacterDialogue.charName = 'Dave';
array[0].CharacterDialogue.charDialogue = 'Why will this not work?!?#';
array[1].type = TypeMenu;
array[1].Menu.menuItem1 = = 'Its rubbish';
array[1].Menu.menuItem2 = 'Its crap',
array[1].Menu.menuItem3 = 'Its a pain',
array[1].Menu.menuItem4 = 'Its something',
array[1].Menu.menuItem5 = 'Its meh',
array[1].Menu.menuItem6 = 'Its pants';

Re: structs with unions

Posted: Wed Sep 07, 2016 8:23 pm
by matteus
answered my own problem as per usual :D (okay not as usual) lol

Re: structs with unions

Posted: Wed Sep 07, 2016 10:23 pm
by Stef
SGDK uses GCC so yeah of course it supports union as it's part of C language :)

Re: structs with unions

Posted: Thu Sep 08, 2016 10:20 pm
by Miquel
You even can use unnamed unions if you don't like that much verbose. Is my opinion that unions are the best memory manager there is for genny.

Re: structs with unions

Posted: Mon Sep 12, 2016 5:42 pm
by matteus
Thanks! I might give that a go!