I know i'm using a direct ogre example, but i'm pretty confident the problem is my understanding of the usage of class, constructors, pointers, and "new" all together. here's the deal, i got a very basic ogre program, loading a scene though dotsceneloader "correctly" (aint the point, but its enough so its not the problem) spawning lights, camera, handling inputs, everything work, but of course considering i'm building a multiplayer game, i want the characters to be dynamicaly created, hence why i figured i needed to place them in their own class, and create them with new, for each time i need one.
Now, if (in my createscene function) i use Player * player = new Player; i get a seg fault, realy, i though that was the right way to do it, i tried a few more variation, but they vary to compilation error, to no character appearing but everything else work, to moar segfault at runtime.
I also tried to leave the constructor empty, and make a function that do the trick, but then, the function doesnt have access to the class heritage of my application framework (witch is actualy BaseApplication from the tutorials, the actual issue is with mSceneMgr) So i'm pretty much lost there.
If anyone want to have a look at my code, here is my full project (only sources arts (sample stuff + our own current scene, witch btw doesnt load the ground texture correctly, its suposed to be green, if anyone know about it, let me know) and config, no project or other platform dependant stuff)
I'll apreciate any help, i'm realy getting stuck right now, trying to work the damn 3rd person camera, and this part hold me back (kinda need the player class so i can orientate the camera based on it)
http://108.174.50.33/pcuploadz/pcgame.zip
Class that spawn a model
-
- Goblin
- Posts: 210
- Joined: Mon Dec 12, 2011 12:52 pm
- Location: Germany
- x 34
Re: Class that spawn a model
what you're looking for is a factory-method to generate new objects in runtime. have a look at http://www.ogre3d.org/forums/viewtopic.php?f=2&t=69331 where I pointed out the usage and benefits of this method and also gave some code for a simple implementation of a class that'll do this.
Your Player constructor has some errors in it:
The first parameter "Ninja" is a string that names the entity and is suppost to be unique. so you should either use a createEntity() function that doesn't take a specific name and lets the sceneManager auto-generate a name, or use a count-variable added to the name.
Also the mSceneMgr variable is a class-variable of the baseApplication and you inherit your Player class from your pcApplication (which is inherited from the baseApp). so mSceneMgr here is an own variable in your Player class, not the one you're using in the pcAapplication you're starting in the main() function. that's the seg-fault, because this pointer isn't initialised (with public inheritance your Player becomes a pcApplication with a new version of all class-variables, it doesn't have access to your old one, unless you mark everything as static but this isn't what you want). So what you want to do is make the SceneManager you're using available in the Player constructor. This can be done either by giving the SceneManager as a parameter in the Player-Constructor, or by making your pcApplication a singleton-class that can be accessed in the constructor.
p.s. you should remove the media-folder from the download. it only makes the file a lot larger and has no use if people are only looking at your code.
Your Player constructor has some errors in it:
Code: Select all
Ogre::Entity* entNinja = mSceneMgr->createEntity("Ninja", "ninja.mesh");
Also the mSceneMgr variable is a class-variable of the baseApplication and you inherit your Player class from your pcApplication (which is inherited from the baseApp). so mSceneMgr here is an own variable in your Player class, not the one you're using in the pcAapplication you're starting in the main() function. that's the seg-fault, because this pointer isn't initialised (with public inheritance your Player becomes a pcApplication with a new version of all class-variables, it doesn't have access to your old one, unless you mark everything as static but this isn't what you want). So what you want to do is make the SceneManager you're using available in the Player constructor. This can be done either by giving the SceneManager as a parameter in the Player-Constructor, or by making your pcApplication a singleton-class that can be accessed in the constructor.
p.s. you should remove the media-folder from the download. it only makes the file a lot larger and has no use if people are only looking at your code.
-
- Halfling
- Posts: 42
- Joined: Tue Jun 28, 2011 3:56 am
Re: Class that spawn a model
Thanks alot, thats a pretty great help you're giving me there! First, the ninja parameter, well that is obiviously a copy/paste from the tutorials for a quick try, as for now i were not trying to spawn multiple objects though this class so i dint realy work out the modification needed to do so, yet, thanks for pointing it out.
About the scene manager, would it actualy be better to pass it as parameter? or use the singleton solution? i'll give a litle more explanation over my project, it is a PvP match based game, you could see it as Heroes of Newerth, but different gameplay of course, so a lobby, people queue up for a match, then once they get matched against opponents, they load the 3d scene, quick fight, go back in lobby. i could technicaly hard-code all players individualy considering the initial project will never have more than 10 players per game, and most of the time only 4, but i though that it would be better pratice to just make a generic class to spawn more players, and the server take care of how many, i kinda try to build my game just like it would be an mmo, without actualy beign an mmo, partly cause i might do this as a seccond project later, but also beacause it is probably better pratice, and more convenient for modification (i eventualy plan to make some kind of different matches that would involve more players, and a less fixed number, so that would be very usefull then).
And if i use the parameter solution, something like player(mSceneMgr){ /* code */ } would work? i mean, its passing the adress of the other class, so i dont need to return it back, right? (gotta say, i havent used much pointer and heritage before, only did some simpler coding)
Also, for what i wana do, i might want to use multiple scene manager, or wipe/recreate, aint sure, anyway, to switch from the GUI based lobby to the 3d match, so... would singleton would involve any limitation? aint sure but i were wondering.
and, yeah i though of not putting the media, but since i were kinda wondering if problems would come of my scene, or something during the loading, so i placed it just in case someone want to actualy test the result.
And for the details, i think the post you linked to me will help alot! i briefly read it since i'm just on my lunch break, but tonight i will look into all of this more attentively. and realy, i do apreciate all of the help, thanks!
About the scene manager, would it actualy be better to pass it as parameter? or use the singleton solution? i'll give a litle more explanation over my project, it is a PvP match based game, you could see it as Heroes of Newerth, but different gameplay of course, so a lobby, people queue up for a match, then once they get matched against opponents, they load the 3d scene, quick fight, go back in lobby. i could technicaly hard-code all players individualy considering the initial project will never have more than 10 players per game, and most of the time only 4, but i though that it would be better pratice to just make a generic class to spawn more players, and the server take care of how many, i kinda try to build my game just like it would be an mmo, without actualy beign an mmo, partly cause i might do this as a seccond project later, but also beacause it is probably better pratice, and more convenient for modification (i eventualy plan to make some kind of different matches that would involve more players, and a less fixed number, so that would be very usefull then).
And if i use the parameter solution, something like player(mSceneMgr){ /* code */ } would work? i mean, its passing the adress of the other class, so i dont need to return it back, right? (gotta say, i havent used much pointer and heritage before, only did some simpler coding)
Also, for what i wana do, i might want to use multiple scene manager, or wipe/recreate, aint sure, anyway, to switch from the GUI based lobby to the 3d match, so... would singleton would involve any limitation? aint sure but i were wondering.
and, yeah i though of not putting the media, but since i were kinda wondering if problems would come of my scene, or something during the loading, so i placed it just in case someone want to actualy test the result.
And for the details, i think the post you linked to me will help alot! i briefly read it since i'm just on my lunch break, but tonight i will look into all of this more attentively. and realy, i do apreciate all of the help, thanks!
-
- Halfling
- Posts: 42
- Joined: Tue Jun 28, 2011 3:56 am
Re: Class that spawn a model
Just to say, got something working out, its probably not perfect but the basic is most definitely working! thanks again