Struct issue

You must understand the Game Editor concepts, before post here.

Struct issue

Postby CrackedP0t » Mon Mar 10, 2014 9:57 pm

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;
}
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score

Re: Struct issue

Postby DarkParadox » Mon Mar 10, 2014 11:28 pm

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".
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: Struct issue

Postby lcl » Mon Mar 10, 2014 11:29 pm

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;
}
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Struct issue

Postby CrackedP0t » Tue Mar 11, 2014 12:26 am

It works now; I was probably just making some stupid mistake.
Thanks for the suggestion DarkParadox, though.
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score

Re: Struct issue

Postby CrackedP0t » Tue Mar 11, 2014 1:28 am

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
       }
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score

Re: Struct issue

Postby bat78 » Tue Mar 11, 2014 2:27 am

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.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Struct issue

Postby lcl » Tue Mar 11, 2014 4:36 pm

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()
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron