Problems with pointers [new title]

Posted By: Ditje

Problems with pointers [new title] - 07/30/10 11:09

When I try to shoot my enemies, I use ent_remove(you). But if I hit the target, I (players ship) got removed not the hidden target grin

Code:
function move_bomb() 
{
	c_setminmax(me); // bounding box setzten
	my.STATE = 1;
	my.pan = your.pan;
	my.tilt = your.tilt;
	my.roll = your.roll;
	
	while(1)
	{
		// Bombe flieg
		if (my.STATE == 1) 
		{
			c_move(me, vector(0, 50 * time_step, 0), vector(0, 0, 0), IGNORE_YOU);
			if(my.y >= 980 || my.z >= 580) 
        	{ // levelende erreicht?
        		my.STATE = 2;
        	}
        	if (HIT_TARGET) 
        	{
				//ent_create("explosion+4.bmp",vector(my.x,my.y,my.z+20),NULL); // particle effect planned
        		ent_remove(you);
        		my.STATE = 2;
        	}
		}
		// Bombe verschwinde
		if (my.STATE == 2) 
		{
        	ent_remove(me);
        	
        	bomb_anzahl --;
        	return; // !!! prevents further access to removed entity
			
		}
		wait(1);
	}
}



an this is the player action, from where function move_bomb is called:

Code:
action spieler_aktion()
{
	spieler = my;
	my.STATE = 1;
	
	while(1)
	{
		// auf und ab drehen
                ...
                 code for ship controll
		...



		// Schiessen - code for fireing
		if (my.STATE == 1) // kein Schuss
	   {
			if(mouse_left) my.STATE = 2;
		}
		if(my.STATE == 2) // Schuss  
		{
			if(last_bomb_x == 15) last_bomb_x = -15;
			else last_bomb_x = 15;
			if(bomb_anzahl < 4)
			{
				ent_create("bomb.mdl",vector(my.x+last_bomb_x,my.y+10,my.z),move_bomb);
				bomb_anzahl ++;
			}
			my.STATE = 3;
		}
		if (my.STATE == 3)
		{
			if (!mouse_left) my.STATE = 1;
		}
		wait(1);
	}
}



Cheers

Ditje
Posted By: Pappenheimer

Re: who is you? - :-D funny effect - 07/30/10 11:16

does this help?

"hit.entity ENTITY* pointer, or NULL when a level surface was hit."

http://www.conitec.net/beta/hit.htm
Posted By: Ditje

Re: who is you? - :-D funny effect - 07/30/10 13:15

May be - I don`t know how to use "hit" Do I have to check if "bomb" hits or if enemy got "hit"?

1.) where do I have to use the function in my shooting-function or enemy-function?

2.) how do I get the pointer?

Thx Ditje
Posted By: bart_the_13th

Re: who is you? - :-D funny effect - 07/30/10 13:32

try to avoid using ent_remove(me) inside a loop since it could mess your code and put it at the end of your code.

Try this code to replace your move bomb
Code:
function move_bomb() 
{
	c_setminmax(me); // bounding box setzten
	my.STATE = 1;
	my.pan = your.pan;
	my.tilt = your.tilt;
	my.roll = your.roll;
	
	while(my.STATE==1)
	{
		// Bombe flieg
		c_move(me, vector(0, 50 * time_step, 0), vector(0, 0, 0), IGNORE_YOU);
		if(my.y >= 980 || my.z >= 580) 
        	{ // levelende erreicht?
        		my.STATE = 2;
        	}
        	if (HIT_TARGET || my.y >= 980 || my.z >= 580) 
        	{
				//ent_create("explosion+4.bmp",vector(my.x,my.y,my.z+20),NULL); // particle effect planned
        		my.STATE = 2;
                        if(HIT TARGET)
                        {
                        //do something here to the HIT_TARGET
                        }
        	}
		wait(1);
        }
        ent_remove(me);
}


Posted By: Ditje

Re: who is you? - :-D funny effect - 07/30/10 13:41

Ah looks nice. Thank you. I will try it on monday - SED is already closed. I have to prepare me for Open Air laugh

Cheers

Ditje
Posted By: Ditje

Re: Problems with pointers [new title] - 08/02/10 14:38

Hi

I figured out a little bit how to use pointers. But not everything. Below you find my code - not needed is deleted.

1.) I want to call a function instead of only remove enemy and have no idea how to do it.

2.) After I have shoot some enemies I get 2 error messages. Invalid argument and SYS crash in move_bomb(). I can "play" further. After some time I get Invalid argument at every hit.

May be calling the function is already the solution. But I don`t know the reason for errors.

Cheers

Ditje

Code:
// enemy functions ///////////////////////////////////////
function start_level_type1()
{
...
   // a loop creates 40+n enemies
   enemy = ent_create("bee1.mdl", vector(50, 1000, 500), start_bee_wave1);
...
}

function start_bee_wave1
{
   // moves the enemies until until they reach their temporary position
}

function attack_player
{
   // not done yet - get called 
   // if a all enemies reached final posion
   // or if an enemy got killed
   // of if an enemy got back from attack to temporary postion
}


// bomb functions /////////////////////////////////////////
function move_bomb() 
{
   ...
   if (HIT_TARGET) 
   {
      snd_play(snd_explode,100,0);
      punkte_spieler += 30;
      ///!!!!!!! Here is my problem !!!!!!!!!! ///////////
      ent_remove(enemy);
      my.STATE = 3;
   }
   ...
}


// player action (added in WED) ///////////////////////////
action spieler_aktion()
{
...
   bomb = ent_create("bomb.mdl",vector(my.x+last_bomb_x,my.y+10,my.z),move_bomb);
...
}


Posted By: DJBMASTER

Re: Problems with pointers [new title] - 08/02/10 15:53

Hi, using pointers in Lite-C is pretty easy. I assume you have worked through the Lite-C workshop.

1) You can call a function from anywhere. All you have to do is create a new function with the code you want to call, give it a useful name and call it.
Code:
void main()
{
beep(); // make the engine 'beep'.
}


The above code using a seperate function...
Code:
void CallBeep()
{
beep();
}

void main()
{
CallBeep();
}


This is a useless example but shows you that you can break any code in various functions. This is called modular programming.

2) The reason the 'ent_remove(enemy);' call crashes is because the 'enemy' pointer is NULL. Removing a NULL pointer results in a crash. Make sure that 'enemy' isn't being removed already.
Posted By: Ditje

Re: Problems with pointers [new title] - 08/02/10 16:23

Simply calling a function is not my problem. But in this case. I have a HIT_TARGET event and my function adresses the hit target, how do I call the function and give the hit ENTITY as parameter?

I noticed already, that my problem must already earlier, when I creat my enemies:

enemy = ent_create("bee1.mdl", vector(-100, 800, 400), start_bee_wave5);

This is done in a loop - so "enemy" got overwritten every time and keeps the value of the last loop. That`s the reason why I get the errormessage only sometimes. Can I create ENTITIES in an array like this?

enemy[i] = ent_create("bee1.mdl", vector(-100, 800, 400), start_bee_wave5);
Posted By: TrackingKeks

Re: Problems with pointers [new title] - 08/02/10 16:37

Yes^^
Posted By: Ditje

Re: Problems with pointers [new title] - 08/02/10 17:36

hmmm in my case sadly not. Error

Cannot convert POINTR to 'struct ENTITY'

Edit again: There are 2 problems this is getting the hit TARGET but there is an additional problem decribed in the following post
laugh sorry for to much posts

I still suppose, that I haven`t found the right (english) description for my problem. This must be absolute basic laugh

- I have a variable ammount of enemies created by ent_create
- I have a variable ammount of bombs

Any bomb can hit any enemy. If (HIT_TARGET), there are a lot of things to do:

- remove bomb
- remove enemy
- count up score
- play sound
- particle effect

So if I create functions which are called by if(HIT_TARGET) the functions have to know which ENTITIES (bomb OR enemy) depend on.

Now I check (HIT_TARGET) on my bombs and try to ent_remove(enemy). functionname(enemy) should work, to. But it doesn`t. I am sure solution with an array of enemies is not the right solution.

I supposed that I can call the TARGET by "you". But doesn`t work. (see above: players ENTITY is "you"). Very weird, because it got not HIT. So how do I get the information which enemy is HIT?


German:
Das HIT_TARGET treibt mich echt in den Wahnsinn. Habe ich das Problem einigermaßen verständlich beschrieben? Bis jetzt habe ich das Gefühl: NEIN laugh

Es muss doch ultra simpel sein, die getroffene ENTIY an (HIT_TARGET) zurück zu geben. Wozu sollte die Funktion sonst sein?
Posted By: Ditje

Re: Problems with pointers [new title] - 08/02/10 18:30

Another mistake must be earlier.

I have a player (the starship) created in WED and added an action with lite-c:

Code:
// Spieler Raumschiff
ENTITY* spieler;

action spieler_aktion()
{
	spieler = my;
	my.STATE = 1;
	
	while(1)
	{
		...

		// Schiessen - Shot
		if (my.STATE == 1) // kein Schuss
	   {
			if(mouse_left) my.STATE = 2;
		}
		if(my.STATE == 2) // Schuss  
		{
			if(last_bomb_x == 15) last_bomb_x = -15;
			else last_bomb_x = 15;
			if(bomb_anzahl < 4)
			{
				bomb = ent_create("bomb.mdl",vector(my.x+last_bomb_x,my.y+10,my.z),move_bomb);
				bomb_anzahl ++;
			}
			my.STATE = 3;
		}
		if (my.STATE == 3)
		{
			if (!mouse_left) my.STATE = 1;
		}
		wait(1);
	}
}



If I try "if(HIT_TARGET) remove(you);" ENTITY 'spieler' got removed when bomb hits an enemy ENITY
Posted By: Pappenheimer

Re: Problems with pointers [new title] - 08/02/10 18:49

I don't know where the heck you found this "HIT_TARGET" thing!
Look into the manual for the event handling and the different events.

http://www.conitec.net/beta/aevent_entity.htm

EVENT_ENTITY should do the job for you, because it returns 'you' as the pointer to the touched/hit entity, and then you can 'ent_remove(you);'.
Posted By: Ditje

Re: Problems with pointers [new title] - 08/02/10 19:35

Yeah - that was it - oh my god so silly - I`ve got the HIT_TARGET from a tutorial wink Haven`t understood the manual first - now I tried again step by step and it works Hurray

Vielen vielen Dank!!!

Ditje
Posted By: Pappenheimer

Re: Problems with pointers [new title] - 08/02/10 21:31

You are really fast in learning Lite-C.
Do you have already experiences with other programming languages?
Posted By: Ditje

Re: Problems with pointers [new title] - 08/03/10 07:54

Oh yeah I just noticed - just half an hour. I`ve read about ENABLE_ENTITY before. But I had to read twice to understand laugh

You`ll find a post about my experiences at the end of

page 3 in the project thread - My first 10 seconds...

and a new thread about my last game here:

A really old project "StapelGabeln"

Cheers

Ditje


Posted By: Ditje

New problems with you pointer - 08/03/10 15:03

next step next problem laugh I am still working on my function, when enemy got hit. There are several cases.

My problem is case 4. It should just switch skins and change you.TYPE

If I hit one enemy of TYPE 4, all enemies TYPE 4 switch their skins and switch{} doesn`t break;

you.TYPE is now 3 and case 3 done (punkte_spieler+=240 ent_remove(you))


Thank you

Ditje


Code:
...
#define TYPE		 skill4
...
my.TYPE = 4; // capture
...

// Gegner getroffen /////////////////////////////////////////////////
function treffer() 
{ 
		snd_play(snd_explode,100,0);
		switch(you.TYPE)
		{
			case 1: 
				punkte_spieler += 50;
				ent_remove(you);	
				break;
			case 2: 
				punkte_spieler += 80;
				ent_remove(you);
				break;
			case 3:
				punkte_spieler += 240;
				ent_remove(you);
				break;
			case 4:
				// switch skins
				ent_mtlset(you,mtl_rosa,1);
				ent_mtlset(you,mtl_blau,2);
				ent_mtlset(you,mtl_lila,3);
				
				you.TYPE = 3;
				break;
			case 5:
				// code for bonus level
				break;
		}
		wait(1);
		return;
}
my.emask = (ENABLE_ENTITY);
my.event = treffer;
...
function move_bomb()
{
...
   if (event_type == EVENT_ENTITY) // Kollisionsabfrage wird etwas getroffen(?)
   {
       my.STATE = 3;
   }
...
}


Posted By: Helghast

Re: New problems with you pointer - 08/03/10 15:28

You have to ent_cloneskin before you change it. look it up in the manual under ent_clone

regards,
Posted By: Ditje

Re: New problems with you pointer - 08/03/10 17:24

yeah - dank je well

Ditje

edit: hmm but switch{} statement still got not stopped by break;
© 2024 lite-C Forums