You Pointer still working in Lite-C?

Posted By: Trooper119

You Pointer still working in Lite-C? - 09/05/08 04:40

I'm using free Light-C and have come across either a really easy or really odd problem depending on what I'm doing wrong. I have the following code called when a projectile hits the character. I get an "Error E1515: Invalid arguments in modelHit" when the program first runs. When I remove the 'you' instructions (and replace them with 'my's) the error goes away. I can plenty of code and examples to show you guys if you want, but I'm hoping this is the source of the problem.

Code:
function modelHit()
{
	switch(event_type)
	{
		case EVENT_IMPACT:
			addLine("Characters are colliding, Oh my!");
			double modifierForUp = (3/4);
			double modifierForDown = (1/4);
			int launchAngle = 30;
			
			if(you.push == 2)		//A projectile has hit the character
			{
				//Check velocity of projectile, character will impact in the same direction regardless of where you hit the projectile
				//my.skill1 = damage, my.skill2 = orientation
				if(you.velX <= 0)
				{	//launch with a force 30 degrees into the air
					my.velX = -1 + you.damage * my.health * cos(launchAngle) * -1;
					my.velZ = 1 + you.damage * my.health * sin(launchAngle);
				}
				else  
				{	//launch with a force 30 degrees into the air
					my.velX = 1 + you.damage * my.health * cos(launchAngle);
					my.velZ = 1 + you.damage * my.health * sin(launchAngle);
				}  
				my.health += you.damage;

				you.state = dead;
				//previously had the following code
                                //wait(1);
                                //ent_remove(you);  //Now works just fine with
                    //these too remove, commented it out here so others could see
				return;
			}
       }
}


Code above works just fine now, previously had a wait(1); then a remove(you); statement, removed and handled elsewhere in the code did the trick.
Posted By: EvilSOB

Re: You Pointer still working in Lite-C? - 09/05/08 10:10

Im not big on events, but I feel the problem is somewhere here
Code:
   you.state = dead;
   wait(1);      //Let the object know it's dead and exit the function
   ent_remove(you);
If the YOU object has an action that makes it ent_remove itself on "dead" state, then it has time to do so during the wait(1).
So now the YOU pointer is empty in this function, and errors out on the ent_remove(you); line.

If the YOU entity does self-remove, then I suggest ditching the ent_remove(you), assuming the YOU can be trusted to 'die' properly.
Otherwise ditch the you.state and wait lines so it 'dies' here.

Let us know how it goes...
Posted By: Trooper119

Re: You Pointer still working in Lite-C? - 09/05/08 13:50

I changed my code above according to your suggestion, thanks a lot, I forgot about the wait() changing pointers, stupid mistake I should have seen.

I changed the way I handled death for my characters and projectiles and it all works fine now, thanks a bunch.
Posted By: EvilSOB

Re: You Pointer still working in Lite-C? - 09/05/08 13:57

No problem, glad I could help. wink

blush Even though I never 'meant' to suggest the YOU pointer changing just due to the wait.
I always forget about that too. Note to self Remember wait(?) means YOU cant be trusted anymore.
I actually meant that the Action the YOU entity was running may have been
ent_removing itself and so YOU has nothing to point at anymore.

You saw an answer I didnt intend to give. Well done.
Posted By: Trooper119

Re: You Pointer still working in Lite-C? - 09/05/08 19:08

rofl, that's great, no need to be so humble though. You helped me realize what I did wrong, now off to errors A-Q.

Good luck everyone.
© 2024 lite-C Forums