cassius Posted June 14, 2013 Share Posted June 14, 2013 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. Quote 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 More sharing options...
Rick Posted June 14, 2013 Share Posted June 14, 2013 I don't see any variables declared at the top of app.cpp. Not sure what you mean, but window, context, world, & camera are part of the App class. They are not global. Quote Link to comment Share on other sites More sharing options...
ZioRed Posted June 14, 2013 Share Posted June 14, 2013 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/ Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
cassius Posted June 14, 2013 Author Share Posted June 14, 2013 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. Quote 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 More sharing options...
shadmar Posted June 14, 2013 Share Posted June 14, 2013 Aggros tutorial on classes is quite nice : http://www.leadwerks.com/werkspace/page/tutorials/_/classes-in-c-r32 Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
cassius Posted June 14, 2013 Author Share Posted June 14, 2013 playerPos.y += (crouched ? playerCrouchHeight : playerHeight); I found this line in aggrors third person tut. Anyone know what "?" means? Quote 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 More sharing options...
beo6 Posted June 14, 2013 Share Posted June 14, 2013 that is a shorthand syntax for if else. If you write it out it would be this: if ( crouched ) playerPos.y += playerCrouchHeight; else playerPos.y += playerHeight; Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted June 14, 2013 Share Posted June 14, 2013 It is called the tuernary operator. If you have a really simple if statement (like choosing the player height), then it can be useful. Quote Link to comment Share on other sites More sharing options...
beo6 Posted June 14, 2013 Share Posted June 14, 2013 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. Quote Link to comment Share on other sites More sharing options...
cassius Posted June 15, 2013 Author Share Posted June 15, 2013 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.. Quote 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 More sharing options...
Furbolg Posted June 15, 2013 Share Posted June 15, 2013 You should watch aggror's videorial about classes, he explains exactly how to implement a method (that's what you want to know). Quote Link to comment Share on other sites More sharing options...
cassius Posted June 15, 2013 Author Share Posted June 15, 2013 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. Quote 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 More sharing options...
ZioRed Posted June 15, 2013 Share Posted June 15, 2013 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")); Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
cassius Posted June 15, 2013 Author Share Posted June 15, 2013 Thanks ziored Now I understand it. I spent a long time looking it up on google but all that foo stuff was beyond me. Quote 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 More sharing options...
ZioRed Posted June 15, 2013 Share Posted June 15, 2013 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"); Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
cassius Posted June 16, 2013 Author Share Posted June 16, 2013 Ok. thanks again Quote 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 More sharing options...
Recommended Posts
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.