Make sure you understand what events are. When c_scan() detects an object, it will try to call your entity's event function, which will automatically set event_type, which in turn is a variable that lets you determine what kind of event has been triggered. But, if you don't have an event function attached to your entity, event_type won't be set.

You might therefore think that all you have to do is this:
Code:
function enemy_event ()
{
	return;
}

action enemy_act ()
{
	my.event = enemy_event; // the function to be called when an event is triggered
}


Because you only need the event to set event_type so that state_wait can evaluate its event_type comparison, but this would be a mistake. You see, as soon as the c_scan instruction detects an entity, it pauses the entire function it is running inside and changes the scheduler so that the entity's event function is executed right after c_scan. If there is nothing in the event instruction, though, then the event function will end and the scheduler will move onto the next function in its list, while keeping your first function paused. Thus if any further events are called after your first event, the first event will not register in the next cycle when the function finally moves on from the c_scan instruction into the event_type comparison!

So this is what you really need to do:
Code:
action state_wait ()
{
	c_scan ( my.x,my.pan, vector ( 360,0,1000 ), SCAN_ENTS | IGNORE_ME );

	// once c_scan is called, this function will be paused and enemy_event() will be executed.
}

function enemy_event ()
{
	if( event_type == EVENT_DETECT )
	{
		// switch to attack mode
	}
}

action enemy_act ()
{
	my.event = enemy_event; // the function to be called when an event is triggered

	while( my )
	{
		state_wait ();

		wait (1);
	}
}


In addition to that, you must also set the event mask of the entity, otherwise Gamestudio will not bother calling the event function when c_scan initiates the EVENT_DETECT event. Here's how you do that:
Code:
action state_wait ()
{
	c_scan ( my.x,my.pan, vector ( 360,0,1000 ), SCAN_ENTS | IGNORE_ME );

	// once c_scan is called, this function will be paused and enemy_event() will be executed.
}

function enemy_event ()
{
	if( event_type == EVENT_DETECT )
	{
		// switch to attack mode
	}
}

action enemy_act ()
{
	my.emask |= EVENT_DETECT; // now c_scan will call enemy_event with EVENT_DETECT
	my.event = enemy_event; // the function to be called when an event is triggered

	while( my )
	{
		state_wait ();

		wait (1);
	}
}


This code won't work as is, but basically if you fill in the empty spots it will work like this:

1. enemy_act is run.
2. the event mask of the entity is set to include EVENT_DETECT.
3. the event function of the entity is set. The event function is called when a function like c_scan detects an entity.
4. state_wait() is called.
5. in state_wait(), c_scan is called.
6. c_scan detects an entity and immediately calls enemy_event, the enemy's event function.
7. the event function automatically sets a global variable, event_type, which allows the developer to compare the type of the event that has been triggered.
8. event_type is determined to be EVENT_DETECT, thus the contents of the brackets below the "if" statement are computed.
9. The event ends. Other actions and game functions are run.
10. Next cycle: state_wait() picks up where it left off after c_scan() and continues running until another event is called, or until it ends.

I hope that helps, if you still have questions feel free to ask. Events confused me when I first started using them too. wink


Eats commas for breakfast.

Play Barony: Cursed Edition!