making enemy turn when i get close

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.
Post Reply
databandit
Posts: 32
Joined: Tue Dec 21, 2004 11:36 am

making enemy turn when i get close

Post by databandit »

hi . . . . i have a piece of code which, after you pass a certain point, executes . . . . but it doesnt execute and the whole program crashes...

Code: Select all

ISceneNode *checkcam;
core::vector3df checkpos;

Code: Select all

while(l1device->run())
	{
		u32 now = l1device->getTimer()->getTime();
			if (now - sceneStartTime > timeForThisScene && timeForThisScene!=-1)
				SwitchToNextScene();
					
		l1driver->beginScene(true, true, SColor(0,200,200,200));
		l1env->drawAll();
		l1smgr->drawAll();
		l1driver->endScene();
		swprintf(l1tmp, 255, L"%s fps:%d", l1driver->getName(),	l1driver->getFPS());
		l1statusText->setText(l1tmp);
		Check();
		
	}
and the function:

Code: Select all

void Check()
{
	checkcam = l1smgr->getActiveCamera();

	checkpos = checkcam->getPosition();
	if(checkpos.Z > 65)
	{
		enemynode->setLoopState(false);
		enemynode->setLoopCallback(dooranim);
		enemynode->setFrameLoop(0,1000);
		enemynode->setAnimationSpeed(50);	
}
-zubair-
cartoonit
Posts: 286
Joined: Mon Nov 15, 2004 6:36 pm

Post by cartoonit »

Code: Select all

   
 swprintf(l1tmp, 255, L"%s fps:%d", l1driver->getName(),   l1driver->getFPS()); 
      l1statusText->setText(l1tmp); 
      Check();
Before IrrDevice->endScene();???

If not at which point does it crash???
databandit
Posts: 32
Joined: Tue Dec 21, 2004 11:36 am

Post by databandit »

cartoonit wrote:

Code: Select all

   
 swprintf(l1tmp, 255, L"%s fps:%d", l1driver->getName(),   l1driver->getFPS()); 
      l1statusText->setText(l1tmp); 
      Check();
Before IrrDevice->endScene();???

If not at which point does it crash???
it crashes when it reaches the check function...

when i remove the : checkpos = checkcam->getAbsolutePosition() . . . .it becomes ok . . .
cartoonit
Posts: 286
Joined: Mon Nov 15, 2004 6:36 pm

Post by cartoonit »

Is l1smgr in scope of the check function??
Guest

Post by Guest »

Never mind scope - how about this bit:

Code: Select all

void Check() 
{ 
...
      enemynode->setLoopState(false); 
      enemynode->setLoopCallback(dooranim); 
      enemynode->setFrameLoop(0,1000); 
      enemynode->setAnimationSpeed(50);    
} 
enemynode is not a local variable, and is also not a member variable, so it must be a global variable. But it has to be initialised or you get a "segmentation violation".
I.e. you should not execute that part of Check() if enemynode is NULL.

Of course it doesn't seem like a good idea to have just one pointer to an "enemy node" in the first place - because you're likely to want multiple enemies at some point.
Guest

Post by Guest »

Actually - don't disregard the "scope" remark either.

The term scope is confusing - but all pointers have to be valid.
It just seems more likely that you forgot to initialise enemynode than l1driver, l1env, l1smgr or l1statustext
databandit
Posts: 32
Joined: Tue Dec 21, 2004 11:36 am

Post by databandit »

this is easier:
could someone please give me a piece of code where it will store the current . . . . . . X value of the camera? (every frame)

thanks,
-zubair-
cartoonit
Posts: 286
Joined: Mon Nov 15, 2004 6:36 pm

Post by cartoonit »

I don't find the term scope confusing, I imagine most other programmers don't either, if someone wants it explaining I will tell them, but it should be something as a programmer knows. No disrespect...

Its a good point that enemyNode might not be initialised, must have had my eyes closed!!!

Store it what a temp variable that will get over written every frame??? I would imagine something like:

Code: Select all

core::vector3df Pos; 

while(device->run())
{
  //do all ur stuff here
  Pos = camera->getAbsolutePosition();
  Pos.X; //this must be ur X position
}
databandit
Posts: 32
Joined: Tue Dec 21, 2004 11:36 am

Post by databandit »

cartoonit wrote:I don't find the term scope confusing, I imagine most other programmers don't either, if someone wants it explaining I will tell them, but it should be something as a programmer knows. No disrespect...

Its a good point that enemyNode might not be initialised, must have had my eyes closed!!!

Store it what a temp variable that will get over written every frame??? I would imagine something like:

Code: Select all

core::vector3df Pos; 

while(device->run())
{
  //do all ur stuff here
  Pos = camera->getAbsolutePosition();
  Pos.X; //this must be ur X position
}
my code is exactly the same as yours, yet it crashes...
Guest

Post by Guest »

OK, here's why I think the term "scope" is confusing here:

it refers to which functions have access to which variable, not to whether a variable has valid contents.

Say you have 3 variables, all named SomeVar:

char *SomeVar;

void func1()
{
int SomeVar;
...
}

void func2()
{
float SomeVar;
...
}

All three have the same name, but depending on scope, a different actual variable is used. That is called scope, and it is checked by the compiler. If a variable name is not in scope, you get a compiler error (something along the lines of "SomeVar not defined").

In a situation such as above, you may initialise a local variable while thinking you've initialised a global variable, so it CAN case the problem of dereferencing an invalid pointer, but scope is not the only possible cause of it.

Anyway, databandit, if your code crashes when it accesses camera->..., then perhaps the camera variable is not initialised properly.
databandit
Posts: 32
Joined: Tue Dec 21, 2004 11:36 am

Post by databandit »

Anonymous wrote:OK, here's why I think the term "scope" is confusing here:

it refers to which functions have access to which variable, not to whether a variable has valid contents.

Say you have 3 variables, all named SomeVar:

char *SomeVar;

void func1()
{
int SomeVar;
...
}

void func2()
{
float SomeVar;
...
}

All three have the same name, but depending on scope, a different actual variable is used. That is called scope, and it is checked by the compiler. If a variable name is not in scope, you get a compiler error (something along the lines of "SomeVar not defined").

In a situation such as above, you may initialise a local variable while thinking you've initialised a global variable, so it CAN case the problem of dereferencing an invalid pointer, but scope is not the only possible cause of it.

Anyway, databandit, if your code crashes when it accesses camera->..., then perhaps the camera variable is not initialised properly.
the variable is a global variable defined as core::vector3df . . .. the error i get is not a normal VC++ error . . . . it's an application error where it says send the info to microsoft or dont send it (i hope u get what i mean)

thanks
-zubair-
Guest

Post by Guest »

Well, assuming something goes wrong in DirectX, (which might prompt a "send to microsoft" dialog): it might still be caused by an error in your code - after all, in order for something to be fast, you'll want to do as little error-checking as you can get away with.

So if I were you, I'd make absolutely sure to ASSERT all my pointers while trying to find where it goes wrong.
If they're all valid, then maybe there's a problem with one of your drivers.
databandit
Posts: 32
Joined: Tue Dec 21, 2004 11:36 am

Post by databandit »

i've got another function which is almost the same except that it does not get the camera position every frame, but it get the camera position every time i press space...
Post Reply