Sign in to follow this  
EvilCloneVlad

Using Points in Functions

Recommended Posts

EvilCloneVlad    114
I have this struct that i created
struct creature {
       string name; //Players Name
       int level;   //Players Current Level
       int max_hp;  //Players Maximum Health Points
       int cur_hp;  //Players Current Health Points
       int mod_hp;  //Players HP Modifier
       int max_mp;  //Players Maximum Mana Points
       int cur_mp;  //Players Current Mana Points
       int mod_mp;  //Players MP Modifier
       int atk;     //Players Attack Power
       int mod_atk; //Players Attack Power Modifier
       int def;     //Players Defense
       int mod_def; //Players Defense Modifier
       int max_exp; //Players Experience Needed to Grow
       int cur_exp; //Players Current Experience
};
and i create an instance of that struct with this
    creature player;
    creature * pplayer;
    pplayer = &player;
I also have a function that i use to input values for all of the information into the "player" instance, i am wondering how i pass the pointer to my other function so i can do something like pplayer->name = blahblahblah inside my function. the other function i use to setup the values:
void setupClass()
{
pplayer->name = blahblahblah;
pplayer->hp = x;
}
I hope that I have explained my situation clearly enough, otherwise if you need other information just ask I will do my best to clarify.

Share this post


Link to post
Share on other sites
rip-off    10976
Since you are using C++, why not use a constructor?

Also, instead of commenting the short hand member names, why not write out the member name in full. This will make it easier to read code that uses Creature instances.

Share this post


Link to post
Share on other sites
EvilCloneVlad    114
@greg

Will that allow me to change the values of the "player" instance that I have previously created by doing that? If so then yes that is what I wanted. (and i would pass it like so: setupClass(pplayer); then to pass my current pointer to that function right?

@tans

I am trying to change the values that are in the "player" instance of my creature struct from inside a function that normally does not have access to it, so what I need to do is pass the pointer I am using, into that function.

Share this post


Link to post
Share on other sites
nobodynews    3126
It's hard to give advice because there are so many things I would do differently than you, probably a result of knowledge level difference.

You give no reason for needing a pointer. For the code you have it is completely unnecessary. You could use references instead when you want to modify a creature object in a function. Like so:
void foo(creature &entity) {
entity.name = "whatever";
entity.level = 9001; // OVER 9000!
// etc.
}

int main() {
creature player;
foo(player);
}

If for some reason pointers are necessary then GregMichael shows the syntax for it.

player seems to be a global variable. It shouldn't be. Create it in main and pass it to functions that need it, like I show in the above sample.

creature seems like an ideal candidate to get some constuctors and member functions to manipulate its data. Look into this.

Hope this helps.

Share this post


Link to post
Share on other sites
EvilCloneVlad    114
@rip

As I am very new to C++ I did not realize you could use a constructor with structs. I thought that was only for classes. As for not using full names with the variables, i certainly could do away with that.

@nobody

Is there a reason you wouldn't use a pointer? More efficient? Adding unneeded complexity? or something of that nature? And the player instance of the creature struct is created in the main function

Also if I am going to use a constructor, is there a way to have it create it with different values? The reason I ask is that when I create the player instance of the creature struct at that time I don't know what the stats are going to be when they start because they haven't chosen their class yet which determines the starting values of each of the variables? is there a way to incorporate that into the constructor then?

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by EvilCloneVlad
@rip

As I am very new to C++ I did not realize you could use a constructor with structs. I thought that was only for classes. As for not using full names with the variables, i certainly could do away with that.

In C++, structs and classes are practically the same. The only difference is default access levels, a struct defaults to public member access and inheritance where a class defaults to private. And these are just defaults, they are easily changed.
Quote:

@nobody

Is there a reason you wouldn't use a pointer? More efficient? Adding unneeded complexity? or something of that nature? And the player instance of the creature struct is created in the main function

A reference is more idiomatic. A pointer implies that NULL is a valid value to pass to the function. It also might indicate that you plan to pass an array (though modern C++ would discourage such a signature). A reference makes it very clean: you want to pass an existing creature instance. It also removes some of the pointer syntax like dereferencing, the arrow operator and the address-of operator, none of which have much to do with the intent of the function.

Quote:

Also if I am going to use a constructor, is there a way to have it create it with different values? The reason I ask is that when I create the player instance of the creature struct at that time I don't know what the stats are going to be when they start because they haven't chosen their class yet which determines the starting values of each of the variables? is there a way to incorporate that into the constructor then?

You generally want to delay instantiating objects until you can set all their members. In this case, you might pass an aggregate type representing the class or type or stats of the creature to the constructor. So you might have to prompt the player to choose their class and only after that construct the player instance.

It is a bad idea to allow objects to have an "uninitialised" state, this can cause subtle bugs if you forget to initialise the instance and end up using it.

Share this post


Link to post
Share on other sites
nobodynews    3126
Quote:
Original post by EvilCloneVlad
@nobody

Is there a reason you wouldn't use a pointer? More efficient? Adding unneeded complexity? or something of that nature?
References and pointers are about as equally efficient as two things can be. The problem is, a pointer adds unneeded complexity. You need to check for NULL before using a pointer while a reference will always be valid. There are situations where using a pointer would be preferred over a reference or even required, but yours doesn't appear to be one of them.
Quote:
And the player instance of the creature struct is created in the main function
That's good. You were using player in the function setupClass without passing it in which made me think "global".

Quote:
Also if I am going to use a constructor, is there a way to have it create it with different values? The reason I ask is that when I create the player instance of the creature struct at that time I don't know what the stats are going to be when they start because they haven't chosen their class yet which determines the starting values of each of the variables? is there a way to incorporate that into the constructor then?
Yes. In your case, you have a lot of data so it will look a little ugly if you have hard-coded the data in your code file. Basically, you'd do this:
struct creature {
// data
creature(string name, int level, int max_hp, /* ... */, int cur_exp) : name(name), level(level), max_hp(max_hp), /* ... */, cur_exp(cur_exp) { };

int main()
{
creature player("Hero McKnightington", 1, 110, /* ... */, 0);
}
And even this would be changed if you read the data from, say, a file. But, one step at a time.

edit: For the
: name(name), level(level)
syntax look into initializer lists. Also, name(name) syntax is perfectly legal and has well-defined behavior in this context. You don't have to use the same name, but I prefer it to avoid having to think of silly prefixes or suffixes for incredibly temporary variables.

Share this post


Link to post
Share on other sites
Zahlman    1682
Another bit of advice: organize data hierarchically. When you have sets of data with related names, it's a sign that you need some kind of sub-structure. In your case, for example, there are pairs of current/max variables. We can make a smaller 'statistic' structure and then embed some of those. Then we have a common place to put the clipping logic.

So I guess all that's left is the actual question:

Quote:
i am wondering how i pass the pointer to my other function


The same way you pass anything else to any function: as a parameter.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this