Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (TipmyPip), 18,633 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 4 of 5 1 2 3 4 5
Re: Yet another question.. [Re: Panda_Dude] #367292
04/11/11 16:34
04/11/11 16:34
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA
What's to know?

vec_set(temp,your.x); //you is the player
vec_sub(temp,my.x); //me is the bad buy
vec_to_angle(my.pan,temp); // now MY looks at YOU
c_move(blah blah and in closing blah) now i move in that direction

Throw that in your code and you can get the AI to turn to any angle and move in that direction. Put that in the while loop and he will keep facing that direction if the target moves.


---------------------
There is no signature here.


QUIT LOOKING FOR ONE!
Re: Yet another question.. [Re: FoxHound] #367367
04/12/11 15:45
04/12/11 15:45
Joined: Mar 2010
Posts: 56
Hertfordshire, England
Panda_Dude Offline OP
Junior Member
Panda_Dude  Offline OP
Junior Member

Joined: Mar 2010
Posts: 56
Hertfordshire, England
it doesn't work... I even tried attaching the action to an enemy in WED to make sure it is attached.
Code:
var movement;
var allow_fire;
var p_angle;
var temp;

action state_wait ()
{
	c_scan ( my.x,my.pan, vector ( 360,0,1000 ), SCAN_ENTS | IGNORE_ME );
	if (event_type ==EVENT_SCAN) my.state = 2;
}
action state_attack ()
{
	vec_set(temp,player.x); //you is the player
	vec_sub(temp,my.x); //me is the bad buy
	vec_to_angle(my.pan,temp); // now MY looks at YOU
	c_move( me,vector( 4 * time_step,0,0 ), nullvector,GLIDE );
}

action enemy_act ()
{
	my.state = 1;
	while( my )
	{
		if (my.state == 2)
		{
			state_attack ();
		}
		else 
		{
			state_wait ();
		}
		wait (1);
	}
}



Last edited by Panda_Dude; 04/12/11 15:47.
Re: Yet another question.. [Re: Panda_Dude] #367369
04/12/11 16:41
04/12/11 16:41
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA
Time to debug. Throw sys_exit(""); in your script first at the start of your enemy_act. It should end your game pretty much instantly. If if it does then you know that function is called. Then put it after your while(my) that way if the game ends you know your while loop was either never called or only called once. Then throw it in different parts of your code to see what is called.

Also use beep(); so you know how often a function is called. Beep and sys_exit are my favorite tools to use in game development.


---------------------
There is no signature here.


QUIT LOOKING FOR ONE!
Re: Yet another question.. [Re: Panda_Dude] #367370
04/12/11 16:42
04/12/11 16:42
Joined: Dec 2008
Posts: 1,660
North America
Redeemer Offline
Serious User
Redeemer  Offline
Serious User

Joined: Dec 2008
Posts: 1,660
North America
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!
Re: Yet another question.. [Re: Redeemer] #367379
04/12/11 19:13
04/12/11 19:13
Joined: Mar 2010
Posts: 56
Hertfordshire, England
Panda_Dude Offline OP
Junior Member
Panda_Dude  Offline OP
Junior Member

Joined: Mar 2010
Posts: 56
Hertfordshire, England
@ FoxHound - Ok thanks for that advice. I'll remember that. I found out which parts of the script aren't running, so thanks for that laugh

@ Redeemer - Right, that makes sense. Thanks. I may have more questions yet though... laugh

EDIT: This isn't working. After using beep, the same two parts aren't working - action state_enemy and function state_attck (the enemy's event)
Here's the code for the two
Code:
function state_attack ()
{
	if (event_type == EVENT_DETECT)
	{
	vec_set(temp,player.x); //you is the player
	vec_sub(temp,my.x); //me is the bad buy
	vec_to_angle(my.pan,temp); // now MY looks at YOU
	c_move( me,vector( 4 * time_step,0,0 ), nullvector,GLIDE );
	}
}

action enemy_act ()
{
	my.emask |= EVENT_DETECT;
	my.event = state_attack; 
	
	while( my )
	{
		state_wait ();
		wait (1);
	}
}



Last edited by Panda_Dude; 04/12/11 19:28.
Re: Yet another question.. [Re: Panda_Dude] #367391
04/12/11 21:05
04/12/11 21:05
Joined: Dec 2008
Posts: 1,660
North America
Redeemer Offline
Serious User
Redeemer  Offline
Serious User

Joined: Dec 2008
Posts: 1,660
North America
Hmm. That code looks fine from what I can see.

Maybe the problem is that c_move() is a "dangerous instruction," meaning it can trigger events too. Therefore you could possibly get in a loop where c_move is called, triggering an event, which calls c_move, triggering an event, etc. and the compiler, knowing this, is skipping over the instruction so that this does not happen..?

As a test, you should try putting a wait(1) instruction before the c_move instruction, which, in the event of an infinite loop, will allow the engine to "breathe". This should satisfy the compiler.

If this isn't the problem, I'm out of ideas...


Eats commas for breakfast.

Play Barony: Cursed Edition!
Re: Yet another question.. [Re: Redeemer] #367397
04/12/11 22:58
04/12/11 22:58
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA
Try this insead of c_scan

if(200 > vec_dist(my.x,player.x))//player is closer than 200 quants
{
if(c_trace(my.x, player.x blah blah blah)//we can see the player from our position
{
beep();
}

}


Anytime the player is in a spot where he could be seen from your spot the system should beep. See if that will happen in your code and go from there. If that does work throw in some code and see if you are in a correct angel to have seen the player (that code would see the player even if he was behind you)

I generally try not to use events because of problems like this. That code should work however it does not because ... well none of us know.

Last edited by FoxHound; 04/12/11 23:01.

---------------------
There is no signature here.


QUIT LOOKING FOR ONE!
Re: Yet another question.. [Re: FoxHound] #367414
04/13/11 07:27
04/13/11 07:27
Joined: Mar 2010
Posts: 56
Hertfordshire, England
Panda_Dude Offline OP
Junior Member
Panda_Dude  Offline OP
Junior Member

Joined: Mar 2010
Posts: 56
Hertfordshire, England
@ Redeemer, that doesn't seem to be the problem.

@ FoxHound, apparently, there's a syntax error in the line
Code:
if(200 > vec_dist(my.x,player.x));


I can't see any syntax errors.

Re: Yet another question.. [Re: Panda_Dude] #367445
04/13/11 15:41
04/13/11 15:41
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA



try this straight from the manual


you = player;
distance = vec_dist(my.x,your.x); // calculate distance of MY and YOU entities

if(dist > 200)
{


---------------------
There is no signature here.


QUIT LOOKING FOR ONE!
Re: Yet another question.. [Re: FoxHound] #367446
04/13/11 15:41
04/13/11 15:41
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
FoxHound Offline
Expert
FoxHound  Offline
Expert

Joined: Jun 2004
Posts: 2,234
Wisconsin USA
Oh wait I see it, remove that semicolon


---------------------
There is no signature here.


QUIT LOOKING FOR ONE!
Page 4 of 5 1 2 3 4 5

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1