Access scene nodes from out of Main.cpp

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Access scene nodes from out of Main.cpp

Post by Zanman777 »

I've been trying to code an Event receiver on a dedicated .cpp file. At certain point, I need to access the camera scene node from Main.cpp... But on Receiver.cpp's scope, there is no such thing as "camera", "smgr", etc (which gives me a compile error).

I've tried using extern keyword, but it won't work.

How should I do what I want?

Code: Select all

bool MyEventReceiver::OnEvent (const SEvent& event)
{
    float oldMousePos[2];
    float newMousePos[] = {event.MouseInput.X, event.MouseInput.Y};
    float mouseMove []= {newMousePos[0] - oldMousePos[0], newMousePos[1] - oldMousePos[1]};
    if (event.EventType == irr::EET_KEY_INPUT_EVENT)
              KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
 
 
    if (event.EventType == EET_MOUSE_INPUT_EVENT)
    {
        if (event.MouseInput.Event == EMIE_MOUSE_MOVED)
        {
            camera->setTarget(vector3df(50, 50, 50)); //just a test target. Here is where the error occurs.
        }
    }
    return false;
}
 
CuteAlien
Admin
Posts: 9680
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Access scene nodes from out of Main.cpp

Post by CuteAlien »

You really should work your way through some more c++ tutorials/books...

In short - MyEventReceiver is a class and you can put variables into classes (that's actually on of their main reasons of existence). And you can pass variables as parameters as well (when variables are public you can set them directly, otherwise you have to set them over functions - that's more or less what all those set functions in Irrlicht-classes do). So you put for example a pointer to camera in your MyEventReceiver and then set it to the value of your main-camera. Check example 05, that's one of the examples doing something like this.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Re: Access scene nodes from out of Main.cpp

Post by Zanman777 »

I do know all that (what classes are, and that MyEventReceiver is a class). But both the definition and implementation of MyEventReceiver are outside the scope of the Irrlicht device and scene nodes definitions/initializations, so the problem persists even with your solution. I mean, I could create a pointer as a member variable of MyEventReceiver, but then how would I get the address of "camera" from Main.cpp? It's still out of scope...

PS: tutorials and books only help you to a certain point. I've read enough of them already, now I'm applying what I've learnt. I do search in C++ references or even tutorials to remind some forgotten concepts, etc, but this one won't be solved by tutorials... It's a specific question, I don't believe it reveals lack of C++ knowledge by itself. I'm looking for a solution, not a lesson :wink:
CuteAlien
Admin
Posts: 9680
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Access scene nodes from out of Main.cpp

Post by CuteAlien »

Did you even take a look at the example I recommended? To use MyEventReceiver you have to create an instance anyway. Classes are description of how your objects will look like. You still have to create the objects.

P.S: Dismissing free lessons while you don't even know what you know yet... I wonder how far you'll get with that attitude.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Re: Access scene nodes from out of Main.cpp

Post by Zanman777 »

Oh, now I get what you mean!! Yeah, that should work alright! Thanks!
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Re: Access scene nodes from out of Main.cpp

Post by Zanman777 »

Ok, I added a private ICameraSceneNode pointer (named "intcamera") to my MyEventReceiver class, set up a function to allow passing on the camera from Main.cpp to Receiver.cpp, like this:

Code: Select all

 
void MyEventReceiver::setCamera(ICameraSceneNode *passedCamera)
{
    intcamera = passedCamera; //internal camera = camera passed on
}
 
Now, I can compile but it gives me a runtime error at start... What am I doing wrong? I know it's this line, because if I remove it or stop trying to assign a value to "intcamera" the program doesn't crash anymore... Why is this?

PS: I don't dismiss free lessons, I just don't like being told to "go study more C++" when this isn't about needing to learn C++ anymore. It's like 9 out of 10 topics on the Beginners help subforum get an "automatic" response that tells the OP to go study and "stop bothering the grown-ups"... Guess what. I've read about 6 or 7 different C++ tutorials in my life, and I'm still stuck in this particular question right now. Want me to go read a tutorial for the 8th time? Seems pretty useless to me... It's like killing flies with atom bombs. "You don't know how to spell Jack? Go read the whole library closest your house". :P
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Access scene nodes from out of Main.cpp

Post by serengeor »

Zanman777 wrote:What am I doing wrong? ... Why is this?
Zanman777 wrote:I do search in C++ references or even tutorials to remind some forgotten concepts, etc, but this one won't be solved by tutorials... It's a specific question, I don't believe it reveals lack of C++ knowledge by itself.
What you're doing wrong is that you believe that you already know what is needed from c++ and general programming to do what you're trying to do.
It also seams that you either don't know what debugging is or you want us to debug your program and make it work for you, so you could drink some tea in the mean time.
Zanman777 wrote:I'm looking for a solution, not a lesson
Yes, and I'm looking for a prewriten mmorpg server/client that would be customized to my needs and ideas, so I could get rich :twisted:


I guess this sounded a bit rude, but all I'm trying to say is that you shouldn't be so naive and think you're ready to do this. Spend some more time examining examples and try taking smaller steps if you can't manage this :wink:
Working on game: Marrbles (Currently stopped).
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Re: Access scene nodes from out of Main.cpp

Post by Zanman777 »

You guys must be oversensitive to these guys who come in here asking for guidance to do the MMORPG of the century...

Smaller steps than trying to make a camera that is user-controllable? It doesn't get much smaller than that... What am I supposed to do, keep examining examples my whole life without ever trying to do on my own? You learn by doing, you know...

But ok, I get it, you guys have got more important things to do than help a BEGINNER. Why don't you close this subforum after all? Doesn't make much sense if not even a question like mine is considered pertinent...

And no, I don't know how to do debugging. But I'll learn it by myself. Just like I learnt to interpret the Irrlicht API documentation on my own. Can't expect to be spoonfed at everything, heh? ;)
CuteAlien
Admin
Posts: 9680
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Access scene nodes from out of Main.cpp

Post by CuteAlien »

No, we actually help a lot of beginners. But you give up way too fast and go back to asking other people instead of trying to find the answer yourself. I mean hell - on my first answer you took 15 minutes already to diss it. Now you have written 3 lines get a crash and first thing you do is ask again? 90% of programming is figuring out crashes, bugs - and learning how to do that is just part of the game. Serengeor gave you one of the important hints - for finding crashes you have to learn how to use the debugger (your IDE already gives you an easy interface to one). Which is way less scary than it sounds - in short a debugger allows you looking at the values of each variable on a crash and so you can see if things are strange. It also allows you setting breakpoints - which is just a line where it stops running the program and you can look at all variable values then. And best of all it - on crashes it stops exactly at the line where it crashed and shows you a callstack (which is nothing else but the line and function where it crashed and the functions which called that function including parameter values).

For your case - the code you posted is not the problem (no really not, I swear). Without seeing the real code there I can only guess... if it crashes in that function (compile with debug-info and you see that - maybe you have to enable some debug-windows like callstack and watches) then the most likely bug is that the object you used to call that function is invalid.

P.S: C++ is a lot about memory management, that's because it's a low-level language. If your tutorial didn't drill that part then you will run into troubles all the time. Maybe you are used to another language like Javascript where classes are objects - then you really have to relearn that part or c++ will just not make any sense.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
dotProduct
Posts: 5
Joined: Sun Apr 22, 2012 3:26 am

Re: Access scene nodes from out of Main.cpp

Post by dotProduct »

Zanman777 wrote:Ok, I added a private ICameraSceneNode pointer (named "intcamera") to my MyEventReceiver class, set up a function to allow passing on the camera from Main.cpp to Receiver.cpp, like this:

Code: Select all

 
void MyEventReceiver::setCamera(ICameraSceneNode *passedCamera)
{
    intcamera = passedCamera; //internal camera = camera passed on
}
 
Now, I can compile but it gives me a runtime error at start... What am I doing wrong? I know it's this line, because if I remove it or stop trying to assign a value to "intcamera" the program doesn't crash anymore... Why is this?
Runtime errors you get from using pointers pretty much means you're using them wrong somehow. I don't see a problem with your code, but you haven't included the part where you call this function. I would guess that the exception fires when you manipulate or use your camera from within your event receiver class. If it's not that, maybe it's a type mismatch, but those things should be caught by the compiler.
Zanman777 wrote:PS: I don't dismiss free lessons, I just don't like being told to "go study more C++" when this isn't about needing to learn C++ anymore.
Your original problem belies a lack of understanding of C++, and I think that's okay. I don't think you should be expected to have mastered the language before coming here, largely because making games is a great way to learn programming and programming languages because your code translates into something you can see right away. All of the tutorials are shown as a single main.cpp file where almost everything happens in the main() function for the sake of simplicity. It's left as an exercise for the reader to translate these mechanics into projects with multiple source files and classes. There's a reasonable expectation that you already know this if you're here. If you need help with such things, I think it's okay to ask here (and I may be wrong), but you need to have some humility and patience if you expect others to be patient enough to help you. Understand that people won't be too terribly eager to help you if it means they need to burn more calories solving your problem than you're willing to burn.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Access scene nodes from out of Main.cpp

Post by serengeor »

Zanman777 wrote:You guys must be oversensitive to these guys who come in here asking for guidance to do the MMORPG of the century...
It's like saying that you will be a chess master just because you know how some of the figures go.
Zanman777 wrote:Smaller steps than trying to make a camera that is user-controllable? It doesn't get much smaller than that... What am I supposed to do, keep examining examples my whole life without ever trying to do on my own? You learn by doing, you know...
Yes there are smaller steps. Using a library that heavily uses pointer and classes everywhere might be an overkill if you don't really know how they work. The best way to do figure this stuff out (at least for me) is to create a new project, write some classes, do stuff with them and see how they work.
Zanman777 wrote:But ok, I get it, you guys have got more important things to do than help a BEGINNER. Why don't you close this subforum after all? Doesn't make much sense if not even a question like mine is considered pertinent...
Why don't you jump out the window? this forum is always helpful to those that want to help themselves.
Zanman777 wrote:And no, I don't know how to do debugging. But I'll learn it by myself. Just like I learnt to interpret the Irrlicht API documentation on my own. Can't expect to be spoonfed at everything, heh? ;)
That is would be a good start. Debugging is really needed in situations like yours, you should be able to find which line fails and why it fails.
Working on game: Marrbles (Currently stopped).
Zanman777
Posts: 17
Joined: Mon Apr 16, 2012 5:45 pm

Re: Access scene nodes from out of Main.cpp

Post by Zanman777 »

Alright, serengour, dotProduct and CuteAlien, I get your points of view. :) Sorry if it seemed like I wanted to have someone do the job for me, it was unintentional. I've already come a long way figuring it out by myself, just asked for help on this for a change. :)

Thanks, CuteAlien, I appreciate your explanation of debugging, that I didn't know.

No, I'm not used to any other language; in fact, C++ is the only one I know.

I do get that using uninitialized pointers, dereferencing null pointers, etc might create pitfalls on a program. It just seems not to be the case here. Ah, well, when I get home I'll use the debugger to figure this out.

Just in case you're wondering, I call the function from within Main(), like this:

Code: Select all

 
// camera scene node pointer named "camera" already defined and initializaed
MyEventReceiver receiver;
receiver->setCamera(camera);
 
You're right, I'll use the debugger once I get home. It should tell me if camera is being passed as a null pointer, or something. Thanks anyway. :)
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Access scene nodes from out of Main.cpp

Post by serengeor »

If you really want us to help and you can't find the mistake on your own, next time post some more relevant code to the problem. Like the whole contents of 'main.cpp' and maybe some other files if you think they might have to do anything with this.
In your code given code I can't really see where you create the camera object, how your loop runs, how you set up anything else, so it is a bit hard to guess why and where your problem happens.
Working on game: Marrbles (Currently stopped).
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Access scene nodes from out of Main.cpp

Post by randomMesh »

Asking smart questions is a skill that has to be learned too.
http://www.catb.org/~esr/faqs/smart-questions.html
"Whoops..."
gerdb
Posts: 194
Joined: Wed Dec 02, 2009 8:21 pm
Location: Dresden, Germany

Re: Access scene nodes from out of Main.cpp

Post by gerdb »

so we got 2 information from you

#1

Code: Select all

void MyEventReceiver::setCamera(ICameraSceneNode *passedCamera)
{
    intcamera = passedCamera; //internal camera = camera passed on
}
 

#2

Code: Select all

receiver->setCamera(camera);
but thats not enough.

first, what is camera???? is it a cake or maybe null?
second,

Code: Select all

 intcamera = passedCamera

does not check for nullptr, i.e.

Code: Select all

_IRR_DEBUG_BREAK_IF( !passedCamera )
or

Code: Select all

if ( !passedCamera ) cout<<"BREAK and eat CAKE"<<endl;
third, i guess your function-calls like moveCamera() do not check for a nullptrs either.
Post Reply