By the sounds of it, "create_creature" should be a constructor of your Creature struct. If you do want to separate both, the best (?) way is to pass a Creature object to create_creature by reference.
If by "create_creature", you mean a function that calculates some stuff and then constructs a Creature object based on the results, you're going to have scope issues (i.e. your object is destroyed when the create_creature function ends). To ensure persistence, you need a "place" to hold your creature object that exists inside the
using scope. Then, pass that "place" to your create_creature function.
For example, imagine a function fight_random_encounter, that generates a creature and runs the combat algorithm versus your player:
1 2 3 4 5 6 7 8 9 10
|
fight_random_encounter() {
// TRY 1
create_creature();
fight(creature); // What is creature? Where is it? It might have been created in create_creature, but it died when that function ended!
// TRY 2
Creature Rat = create_creature(); // create_creature returns the creature it created, which is assigned to Rat.... but why not do it directly in the constructor?
// TRY 3
Creature Rat;
create_creature(Rat); // create_creature accepts a Creature reference and assigns it values!
}
|
To summarize: fight() needs access to the Creature object, which means it has to exist inside the calling scope (here: fight_random_encounter). This means that any function create_creature needs a way to return this object to the caller.
You could wonder if TRY3 is actually better than doing it through the constructor. In the current use, it's not. However, you might want to fight multiple creatures!
1 2 3 4
|
fight_random_encounters() {
Creature rats[5];
for (int ir = 0; ir < 5; ++ir) create_creature(rats[i]);
}
|
Yes, that can also be done by constructors, but this feels and looks more clear to me.
[edit]
Please never use the name "create_creature". I don't know why, but I mistyped it nearly every time.