Page 1 of 1

Struct issue

PostPosted: Mon Mar 10, 2014 9:57 pm
by CrackedP0t
I'm trying to use a struct to keep track of all of the player's stats, but the only thing that works is to use an array.
This is fairly clunky and unnecessary, as I only have one.
How can I do this?

Code: Select all
typedef struct playerStructAttributes {
    int energy;
    int health;
              }playerAttributes;

playerAttributes playerstats[1];

//Called from the view when it's created
void setattributes() {
    playerstats[0].energy = 0;
    playerstats[0].health = 100;
}

//Called from an actor at various points
void addenergy () {
    playerstats[0].energy += 5;
}

Re: Struct issue

PostPosted: Mon Mar 10, 2014 11:28 pm
by DarkParadox
I'm not sure why it's breaking for you, but in this case it might be better just to use an "anonymous struct," where you don't name a struct data type, but create a variable using it without any of that.
Code: Select all
struct {
    int energy;
    int health;
} playerstats;

And then you can use the "playerstats" variable right away like "playerstats.health".

Re: Struct issue

PostPosted: Mon Mar 10, 2014 11:29 pm
by lcl
What do you mean when you say that the only thing that works is to use an array?
I modified your code to test it without an array and the code works perfectly fine for me.

Code: Select all
typedef struct playerStructAttributes
{
    int energy;
    int health;
}playerAttributes;

playerAttributes playerstats;

//Called from the view when it's created
void setattributes()
{
    playerstats.energy = 0;
    playerstats.health = 100;
}

//Called from an actor at various points
void addenergy()
{
    playerstats.energy += 5;
}

Re: Struct issue

PostPosted: Tue Mar 11, 2014 12:26 am
by CrackedP0t
It works now; I was probably just making some stupid mistake.
Thanks for the suggestion DarkParadox, though.

Re: Struct issue

PostPosted: Tue Mar 11, 2014 1:28 am
by CrackedP0t
Another problem:
This code throws the error: Error line 62: unexpected declaration sprintf
Code: Select all
void createenemy(int type) {
    Actor * view = getclone("view");
    char tempstring[256];
    sprintf(tempstring, "enemy%i", type);
    Actor * enemy = CreateActor("enemy", tempstring, "(none)", "(none)", view->x - 200, view->y - 200, true); //Line 62
       }

Re: Struct issue

PostPosted: Tue Mar 11, 2014 2:27 am
by bat78
CrackedP0t wrote:Another problem:
This code throws the error: Error line 62: unexpected declaration sprintf
Code: Select all
void createenemy(int type) {
    Actor * view = getclone("view");
    char tempstring[256];
    sprintf(tempstring, "enemy%i", type);
    Actor * enemy = CreateActor("enemy", tempstring, "(none)", "(none)", view->x - 200, view->y - 200, true); //Line 62
       }


Code: Select all
void createenemy(int type) {
    Actor * c_view = getclone("c_view");
    char tempstring[256];
    Actor * enemy = CreateActor("enemy", tempstring, "(none)", "(none)", c_view->x - 200, c_view->y - 200, true);
    sprintf(tempstring, "enemy%i", type);
       }


This line:
Code: Select all
Actor * enemy = CreateActor("enemy", tempstring, "(none)", "(none)", view->x - 200, view->y - 200, true);

is not safe because enemy will point to the created actor. you can allocate it though and free when function ends. Idk were are you about to use the code tho.

Re: Struct issue

PostPosted: Tue Mar 11, 2014 4:36 pm
by lcl
CrackedP0t wrote:Another problem:
This code throws the error: Error line 62: unexpected declaration sprintf
Code: Select all
void createenemy(int type) {
    Actor * view = getclone("view");
    char tempstring[256];
    sprintf(tempstring, "enemy%i", type);
    Actor * enemy = CreateActor("enemy", tempstring, "(none)", "(none)", view->x - 200, view->y - 200, true); //Line 62
       }

The problem is that you can't have that sprintf() call there between the variable declarations.
You have to do it so that you define Actor ' enemy there, and then call sprintf, and after that set enemy equal to CreateActor()