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.

DarkBASIC Professional Discussion / Using for loop to control bunch of NPCs and excluding specific one (problem)

Author
Message
Opposing force
19
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 20th Mar 2014 01:48 Edited at: 20th Mar 2014 01:49
Okay, hopefully I can explain this clearly. I'm controlling a bunch of AI characters using a for loop. Basically:

P represents all the AI characters (10 to 29). Pretty straightforward, right? I have a function for distance and I want the AI to recognise if they're near another AI character so it should be something like "if distance(P,P)<10".

Obviously, this does not work because every AI character will treat its own ID as a different ID and think it's near another. I tried creating another for loop within (For E = 10 to 29) and having if E<>P before if distance(E,P)<10 but this doesn't work either.

So, question is...How can I isolate a specific ID within the for loop? (If that makes sense) I'm scratching my head trying to figure out what I'm doing wrong and I've looked around the forum but can't seem to find anything. Educate me please.

Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th Mar 2014 02:00
Quote: "I tried creating another for loop within (For E = 10 to 29) and having if E<>P before if distance(E,P)<10 but this doesn't work either. "


That should work, so what goes wrong exactly?



Powered by Free Banners
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 20th Mar 2014 03:45 Edited at: 20th Mar 2014 03:53
Here's a simple example (PB code)- It doesn't try to be clever, it just brute force searches the current character against all other characters. But the Principal is what you described really.

ShellfishGames
12
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 20th Mar 2014 14:28 Edited at: 20th Mar 2014 14:30
Alternatively you could do something like this:



But the simple if distinction is probably better. If it didn\'t work for you, there\'s probably just a minor mistake somewhere.

However, in case all AI units react the same way to being near another one, you could also make use of that, sort of like this:



Opposing force
19
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 20th Mar 2014 22:47 Edited at: 20th Mar 2014 22:51
Thanks for all the help guys, some really useful information. Appreciate it. Turns out, my code was missing "if object exist(e)" because not all NPCs get spawned at the start so the system wasn't working properly. I think I've figured it out now.

(contains pseudocode)


Login to post a reply

Server time is: 2025-05-15 16:34:54
Your offset time is: 2025-05-15 16:34:54