Jump to content

c++ variables


cassius
 Share

Recommended Posts

This is anoob question but I am brushing up on c++ after a long layoff. I am ok with the syntax but the scope of vars and whats visible to what sometimes catches me out. Are the variables declared at top of app.cpp global or local or should globals go in main.cpp above the main function?

EDIT:I have spent some time googling these questions but I would rather ask here where more specific answers are more likely.

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

I think technically they are global, but the compiler does not "know" them out of the .cpp itself. If you really really need global variables (*argh* I find them very bad in OOP design) you should declare and initialize them in the .h file, then where you need to access them you will include that file. But since global variables (and global functions) are definitely not OOP, I think a more elegant way would be to add those variables into a class.

 

EDIT: well, I tried now to declare a variable in a .h out of the class declaration and the compilation failed, so it seems you should add them to a class.. sorry, there's still something I need to recall from the past, didn't use C++ from long time, so probably the other guys could be more specific and clear. Here is: http://www.learncpp.com/cpp-tutorial/42-global-variables/

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

I am trying to learn classes. I understand structs and used one called actor in my le2 game, but that was more c than c++. In that game all my vars were declared inside the main function.

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

playerPos.y += (crouched ? playerCrouchHeight : playerHeight);

 

I found this line in aggrors third person tut.

Anyone know what "?" means?

 

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

I am usually not a friend of these.

There are only rare occasions when it is really better to use them because they can be assigned to variables or parameters directly since they return a value.

 

But tastes are different.

Link to comment
Share on other sites

I am struggling to understand classes too at the moment

Here is what I have

 

class actor

{

public:

Model* mesh;

int animsequence;

 

}

How could I use this with a character model called barbarian?

Lets say I wanted to load in the character.

I would be adding more vars to the class of course in time..

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

I watched aggrors tutorial several times but my eyesight is not brilliant, I got lost halfway through.If I can't get help here I will probanly use a struct instead. But thanks anyway.

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

You can add a constructor to your actor class which accepts an entity as parameter:

 

Actor.h

class Actor {
public:
Actor(Model*);
Model* GetModel(); // you want a getter method because you don't want your entity to be changed outside the class
private:
Model* mesh;
}

 

Actor.cpp

Actor::Actor(Model* model) {
mesh = model;
}

Model* Actor::GetModel() {
return mesh;
}

 

How to use:

Actor* actor = new Actor(Model::Load("mypath/mymodel.mdl")); // this line calls the constructor above
// Now you can access its model
Model* myModel = actor->GetModel();

 

I hope it can clarify a little your doubts. This code is almost similar to what I'm using in my component based project.

 

Now why would you want a getter method like GetModel instead of just adding a public member which requires lesser code? Take this example: for some reason you want your "actor" mesh to be changed dynamically, in this case you want of course at least that the new model has the same position and rotation like the old one, so instead of eventually duplicating the code in more than one part you want to call one only function everywhere that does it for you so you add a setter method to the class:

 

Actor.h

class Actor {
public:
  //...
  void SetModel(Model*);
  //...
}

 

Actor.cpp

// ...
void Actor::SetModel(Model* model) {
  // Get the old entity's rotation/model, or default values if entity was not set
  Vec3 pos = (mesh ? mesh->GetPosition(true) : Vec3(0,0,0));
  Vec3 rot = (mesh ? mesh->GetRotation(true) : Vec3(0,0,0));
  // Release the old entity
  if (mesh)
     mesh->Release();
  // Set the new entity and assign the old position/rotation
  mesh = model;
  mesh->SetPosition(pos, true);
  mesh->SetRotation(rot, true);
}
// ...

 

so to change the current model you'll only write:

actor->SetModel(Model::Load("mypath/myothermodel.mdl"));

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

Thanks ziored Now I understand it. I spent a long time looking it up on google but all that foo stuff was beyond me.

No problem, buddy. I edited my previous post with the reason why you usually want getter/setter method instead of changing directly the variable "mesh", it of course depends on your game/variables (for example you won't need getter/setter for a "speed" variable, so you will add it to "public" declarations of the class and change it like "actor->speed = 10");

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...