Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / can someone help me understand functions better?

Author
Message
David iz cool
19
Years of Service
User Offline
Joined: 21st Sep 2005
Location: somewhere lol :P
Posted: 16th Mar 2008 18:08
i know how to make functions to create boxes or check distance,or color an object. but what im trying to do is make 4 enemies attack my player using a function.


ive tried using if distance to player is less than # but it doesnt work.

most of my coding is if then do this if then do this

how do you setup a function to make the enemy move toward a player,attack,then move away,then repeat????
ive searched the forum btw.
xtom
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Ireland
Posted: 16th Mar 2008 21:03
Quote: "how do you setup a function to make the enemy move toward a player,attack,then move away,then repeat????"


What you're asking for is basically ai, which could be made up of multiple functions but it doesn't necessarily have to use functions.

You could use a for-next loop to go through each enemy, check the distance between player and enemy, move enemy closer, if enemy is within certain range then fire, then retreat until fire is recharged maybe and repeat over and over. Best bet might be to code it without functions initially and then look over your code for code that repeats itself and then make them functions.

StevetS
20
Years of Service
User Offline
Joined: 19th May 2004
Location:
Posted: 17th Mar 2008 15:14
Quote: "ive tried using if distance to player is less than # but it doesnt work."


There's nothing wrong with doing it this way but as xtom said you need to cycle through each enemy one at a time. You also don't want to be doing a lot of cycling/operations in your actual functions if you can get away with it - try to split the operations down. Having a single function to do everything will probably slow your code down as it'll be sitting in a sub-loop for longer than necessary

You want to set up something like (pseudocode):


and cycle through each of your enemies either with a (for/next) loop or a counter (a bit more flexible).

David iz cool
19
Years of Service
User Offline
Joined: 21st Sep 2005
Location: somewhere lol :P
Posted: 18th Mar 2008 08:14
thanks ill try that! i didnt think about breaking it down with phases instead. tks!

Login to post a reply

Server time is: 2024-09-27 12:25:53
Your offset time is: 2024-09-27 12:25:53