Page 1 of 1

making enemy turn when i get close

Posted: Sat Feb 05, 2005 11:07 am
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-

Posted: Sun Feb 06, 2005 12:41 pm
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???

Posted: Wed Feb 09, 2005 2:05 pm
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 . . .

Posted: Wed Feb 09, 2005 10:39 pm
by cartoonit
Is l1smgr in scope of the check function??

Posted: Thu Feb 10, 2005 7:49 am
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.

Posted: Thu Feb 10, 2005 7:55 am
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

Posted: Thu Feb 10, 2005 11:37 am
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-

Posted: Thu Feb 10, 2005 9:10 pm
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
}

Posted: Fri Feb 11, 2005 1:23 am
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...

Posted: Fri Feb 11, 2005 8:41 am
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.

Posted: Fri Feb 11, 2005 12:28 pm
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-

Posted: Fri Feb 11, 2005 1:48 pm
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.

Posted: Fri Feb 11, 2005 2:05 pm
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...