flag event

Posted By: Abarudra

flag event - 08/10/13 13:38

Hey there,

just a quick question. I have many entities in my level which dont have a while loop in their action. The only purpose of the action is to set the skills to certain values.
I now want to execute one function (which contains the entity-pointer as parameter) when one of the entitys flags is set to 1.
Do i realy have to add a while loop just to check once for if(my.flagX), or is it possible to have something like an event-function that is only triggered when a flag gets set to 1?

Regards
Posted By: CanadianDavid

Re: flag event - 08/11/13 00:23

No, there is no event for flag detection. However, if you are colliding with these entities you can check if is(you,FLAG1) returns True or False. If there are many entities you want to check if the FLAG is set you can scan through all entities on the map via ent_next and check if the returned entity pointer has its FLAG set.

You will have to describe your problem more fully (possibly with an example) for the best solution to be recommended. It is possible that you can use one of the many EVENTS already defined; please see the Gamestudio Manual for all the possible EVENTS.
Posted By: Uhrwerk

Re: flag event - 08/11/13 02:51

Originally Posted By: Abarudra
Do i realy have to add a while loop just to check once for if(my.flagX)

The while loop is one possibility.
Originally Posted By: Abarudra
or is it possible to have something like an event-function that is only triggered when a flag gets set to 1?

Not natively. But you can easily call that function every time after manipulating the skill:
Code:
my_special_ent->skill1 += 1;
skill_was_changed(my_special_ent);


If you're doing this frequently you can also write a setter function:
Code:
void set_skill1(ENTITY* ent,fixed value)
{
  if (!ent)
    return;

  ent->skill1 = value;

  skill_was_changed(ent);
}

Posted By: Abarudra

Re: flag event added - code example - 08/11/13 08:52

Thank you for your ideas, i will try it.

You are right i should have added more details. I have this situation.

The entities i mentioned are remote triggered explosives. The action sets several skills set the damage, delay, aoe etc. I store the pointer of this entity in a skill of the detonator-entity.
I want to detonate it by setting one of its flags to 1 and then execute the explosion function.

The problem is this function contains an ent_create function, and it is necessary that the you pointer for the created entity is set to the explosive and not to the detonator.
I will try to get my code in a readable state and post it here. Ok i dont have access to my computer right now, so i will just post a general example
-- EDIT --
Code:
1. The Detonator

void trigger_explo(ENTITY* explosive) // Will set the Flag of the saved entity to 1
{
   [...]
   set(explosive,FLAG1);
   [...]
}

action detonator()    // Has stored all explosives-pointer in its entity-skills
{
   [...]
   my.skill1 = spawn_explosive(); // create explosive and save its pointer
   while(1)
   {
      [...]
      if(i_want_to detonate)      // check if eg. button for detonation is pressed
      {
         trigger_explo((ENTIY*) my.skill1);
         [...]
      }
      wait(1);
   }
}

2. The Explosive

void detonate_ent()   // aoe damage and shrapnelspawning
{
   [...]
   ent_create("shrapnel.mdl",my.x,shrapnel_func);
   //you.parent = me;
}

void explosive_ent()
{
   [...]
   set_stats_from_other_source(source); // With this function i save the stats of the explosive and the generated shrapnels in skills of this entity 
   
   while(1)
   {
      if(is(my,FLAG1)){detonate_ent();} // i just would like to avoid many entities with short loops that check only for one changing flag
      wait(1);
   }
}

3. The Shrapnel

void set_stats_from(ENTITY* from) // all properties of the shrapnel are save in skills in the explosive_entiy
{
   my.skill10 = from.skill10;
}

void shrapnel_func()
{
   set_stats_from(you); // get stats from your creator
   [...]
   while(1)
   {
      do_stuff();
      wait(1);
   }
}

I know i can avoid the whileloop if i run detonate_ent() inside the detonator action. But then you would point to the detonator and not the explosive.
It would work if i save the stats in detonator.skills instead of explosive.skills, but i want to avoid that if possible, cause i dont have enough free skills if i want to support the number of explosives i need.


I hope my idea is now better to understand and i thank you for ideas / answers.

Regards
Posted By: Abarudra

Re: flag event added - code example - 08/11/13 14:10

added codeexample to previous post.
Posted By: WretchedSid

Re: flag event added - code example - 08/11/13 14:38

Set the you pointer yourself to the explosive, then call detonate_ent(). Something like this should work:

Code:
void trigger_explo(ENTITY* explosive) // Will set the Flag of the saved entity to 1
{
   wait(1); // This is important as it returns to the caller, and gives us the chance to alter the you pointer opaque to the caller but visible to our callees

   you = explosive;
   detonate_ent();
}

Posted By: Abarudra

Re: flag event added - code example - 08/11/13 14:48

Thank you JustSid,

i will give it a try. I had sth. similar before but i forgot the "wait(1);"

My approach was this

Code:
ENTITY* created = ent_create(...);
created.skill1 = explosive;

But it crashed because i checked the skill before it had the right value.

Posted By: Abarudra

Re: flag event added - code example - 08/11/13 15:44

Ok i have tried it.
But it seems i have interpreted your code wrong or i'm just stupid.
If i set the you pointer before the ent_create, shouldn't it be overwritten?

I wrote this code to check it
Code:
#include <acknex.h>
#include <atypes.h>
#include <default.c>

void bomb()
{
	my.skill3 = 100;
}

void shard()
{
        wait(1); // <-- added
	//my.skill1 = you.skill3;
        my.skill1 = my.parent.skill3; // <-- added
	while(1)
	{
		my.pan -= my.skill1*time_step;
		wait(1);
	}	
}

void detonate_ent(ENTITY* e)
{
	//ent_create(CUBE_MDL,vector(my.x,my.y,my.z+200),shard);
        ENTIY* created = ent_create(CUBE_MDL,vector(my.x,my.y,my.z+200),shard); //<-- added
        created.parent = e; // <-- added

}

void trigger_ent(ENTITY* e)
{	
	//wait(1);
	//you=e;
        // detoante_ent(you);
	detonate_ent(e); //<-- added
}

void detonator()
{
	ENTITY* test= ent_create(CUBE_MDL,vector(0,100,0),bomb);
	my.skill3 = 1;
	while(1)
	{
		if(key_d){trigger_ent(test);return;}
		wait(1);
	}
}

function main()
{
	video_switch(8,0,2);
	level_load(NULL);
	vec_set(screen_color,vector(255,0,0)); // blau 
	wait(3);
	fps_max = 60;
	ent_create(CUBE_MDL,nullvector,detonator);
	camera.x -= 1000;
}


The shard in this example is rotating slowly (as expected) cause its skill1 is set to 1 and not to 100 as i need it to be.
Ok i think i was too fast. I made a view changes and now i think its working for now.
The removed parts are commented and the new lines are marked with "//<-- added"
© 2024 lite-C Forums