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 / Mutually exclusive events.

Author
Message
Butter fingers
18
Years of Service
User Offline
Joined: 20th Mar 2006
Location: Mecca
Posted: 3rd Mar 2008 17:25
Hey. Im dealing with AI at the moment. I'm not going to post my code, because there are too many arrays/types for it to make much sense on its own, but lets use some pseudo code.

In my AI the enemy detects it's distance from it's target, then decides upon an action. There are 4 posible distancces:
near
medium
far
distant

So in my code at the moment it says something like

FOR enemy=firstenemy to lastenemy
getdistancetotaget(enemy,target)

if distance=near
*do some stuff*
endif

if distance=medium
*do some stuff*
endif

if distance=far
*do some stuff*
endif

if distance=distant
*do some stuff*
endif

next enemy

That's all well and good, but really those 4 "ifs" are mutually exclusive. If the target is "medium" there's no need to run the other "ifs".

I did some sniffing around on how to create mutually exclusive events and Hands On DBpro said I should type it like this:

FOR enemy=firstenemy to lastenemy
getdistancetotaget(enemy,target)

if
distance=near:
*do some stuff*

distance=medium:
*do some stuff*
endif

distance=far:
*do some stuff*

distance=distant:
*do some stuff*

endif.

But that doesn't appear to work for me. I get:
Error : If condition only takes 1 parameter at line whatever.

So I'm asking for some help! How can I type this so it works?!?!

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Mar 2008 17:32
You can nest each if statement in the else of the previous one, although this is rather messy:



Or if near/medium/far/distant are constant values, you can use a select statement:



KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 3rd Mar 2008 17:37 Edited at: 3rd Mar 2008 17:37
How about this,

Select distance
case near
*do some stuff*
endcase

case medium
*do some stuff*
endcase

case far
*do some stuff*
endcase

case distant
*do some stuff*
endcase
endselect

In COBOL at least I was told that this is much less processor intensive than a series of if/thens, as there is a flag set once one of the cases is met, so it doesn't check the rest.

[edit]
Darnit, Ben beat me to it..


Don't think, just code.
Butter fingers
18
Years of Service
User Offline
Joined: 20th Mar 2006
Location: Mecca
Posted: 3rd Mar 2008 17:47 Edited at: 3rd Mar 2008 17:59
Yeah Near, far etc are string values created from the distance as an integer, i.e
0-20 = near
21-40= medium
etc,
so yes they are constant.
I'll give it a go. It's not like my code won't work without this, but I get the feeling it'll improve the speed of it all if it doesn't have to run through a whole bunch of IFs.

Thanks for your help guys.

Out of interest, am I best off putting the most frequently occuring case at the start? Thus reducing the number of times it has to run through, or will the flags mean that it doesn't matter the order of the cases?

EDIT

I'd still like to know the answer to the above, but thanks for the advice. I'd never (NOOB ALERT) used select/case before, and it's squeezed me some much needed FPS. So thank you very much...

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Mar 2008 17:53
Putting the more frequent cases at the start is better, because I believe the checks are done in that order. If the first check is true, it can jump to the first case straight away.

Quote: "but I get the feeling it'll improve the speed of it all if it doesn't have to run through a whole bunch of IFs"

It probably won't be a noticeable improvement, but it's good practice anyway.

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 3rd Mar 2008 18:42
It should be done that way and can make a difference when you are looking to optimise wherever possible. If it checks this case every cycle, that's a big heap of unnecessary checks you'll accumulate by doing it in the right order.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 3rd Mar 2008 18:46
Quote: "If it checks this case every cycle, that's a big heap of unnecessary checks you'll accumulate by doing it in the right order."

Compared to the unnecessary code the compiler generates, it's nothing.

Paul08
16
Years of Service
User Offline
Joined: 20th Feb 2008
Location: Oxford, UK
Posted: 3rd Mar 2008 19:45
cases every time

Paul

Login to post a reply

Server time is: 2024-09-27 12:14:32
Your offset time is: 2024-09-27 12:14:32