AI Goal: Follow Class
Moderator: Moderators
- Lagomorphia
- Major

- Posts: 502
- Joined: Sun Dec 13, 2009 10:02 am
AI Goal: Follow Class
For some reason, the search function doesn't allow two letter words, so despite the fact that this has probably been answered already (as I've seen it done before) I can't find the topic explaining how.
How do you set an AI goal so a team follows a certain class of unit? I'm using this script to spawn a small team of units, and I want them to follow the unit class that spawns them. I've successfully spawned them and they obey their current conquest goal to the letter, but it's a pain having to use the follow command every single time, the AI never tell them to follow, and I plan to make the unit that spawns them a hero, which means I need them coded to follow anyway. There is only one of the class that spawns them on the battlefield at once. Ideally, I want them to follow whoever is spawned as that one unit of that class, be they AI or human.
Also, how do I modify that script to trigger when the character spawns rather than when they use a certain weapon? I tried something involving OnCharacterSpawn a few days ago, but my ineptitude at LUA coding defeated me.
How do you set an AI goal so a team follows a certain class of unit? I'm using this script to spawn a small team of units, and I want them to follow the unit class that spawns them. I've successfully spawned them and they obey their current conquest goal to the letter, but it's a pain having to use the follow command every single time, the AI never tell them to follow, and I plan to make the unit that spawns them a hero, which means I need them coded to follow anyway. There is only one of the class that spawns them on the battlefield at once. Ideally, I want them to follow whoever is spawned as that one unit of that class, be they AI or human.
Also, how do I modify that script to trigger when the character spawns rather than when they use a certain weapon? I tried something involving OnCharacterSpawn a few days ago, but my ineptitude at LUA coding defeated me.
-
THEWULFMAN
- Space Ranger
- Posts: 5557
- Joined: Tue Aug 17, 2010 3:30 pm
- Projects :: Evolved 2
- Location: Columbus, Ohio
- Contact:
Re: AI Goal: Follow Class
your not going to believe this, but I was coming to GT to start a near indentical thread
no, like, really
Anyway, I am looking into this myself,because I want my 2 clones to follow their commander or captain.
I am looking into maybe trying to trick the AI into thinking you have a flag. Stand by.
still
about this thread.
no, like, really
Anyway, I am looking into this myself,because I want my 2 clones to follow their commander or captain.
I am looking into maybe trying to trick the AI into thinking you have a flag. Stand by.
still
- Lagomorphia
- Major

- Posts: 502
- Joined: Sun Dec 13, 2009 10:02 am
Re: AI Goal: Follow Class
Probably because both ideas were spawned off of the idea I told you two days ago about those droid squads.
- DarthD.U.C.K.
- Master of the Force

- Posts: 6027
- Joined: Wed Sep 27, 2006 11:05 am
- Location: Duckburg, Germany
Re: AI Goal: Follow Class
if you want them to follow the unit that spawned them:
put it in the "OnCharacterDispenseControllableTeam"-function
im not sure how to tell them to follow one class, im not sure if that possible.
edit: the last parameter doesnt work, im not sure which function to use but because the weapon is used primarily by the player you can just put a zero as alast argument and theyll follow him.
Code: Select all
Supportgoal = AddAIGoal (SupportTeam, "Follow", 100, GetEntityPtr(character))
im not sure how to tell them to follow one class, im not sure if that possible.
edit: the last parameter doesnt work, im not sure which function to use but because the weapon is used primarily by the player you can just put a zero as alast argument and theyll follow him.
Last edited by DarthD.U.C.K. on Mon Apr 18, 2011 4:23 pm, edited 1 time in total.
- Lagomorphia
- Major

- Posts: 502
- Joined: Sun Dec 13, 2009 10:02 am
Re: AI Goal: Follow Class
Thanks!
By the way, if I give them a Conquest goal weighted at, say, 50, will they proceed with Conquest objectives after their 'master' dies?
EDIT: Where in the code do I put it?
By the way, if I give them a Conquest goal weighted at, say, 50, will they proceed with Conquest objectives after their 'master' dies?
EDIT: Where in the code do I put it?
Hidden/Spoiler:
- DarthD.U.C.K.
- Master of the Force

- Posts: 6027
- Joined: Wed Sep 27, 2006 11:05 am
- Location: Duckburg, Germany
Re: AI Goal: Follow Class
you can put it under local supportteam.
about conquest: put a conquesgoal with a priority of 1 above/below the other goal
about conquest: put a conquesgoal with a priority of 1 above/below the other goal
- [RDH]Zerted
- Gametoast Staff

- Posts: 2982
- Joined: Sun Feb 26, 2006 7:36 am
- Projects :: Bos Wars AI - a RTS game
- xbox live or psn: No gamertag set
- Location: USA
- Contact:
Re: AI Goal: Follow Class
You can't tell the AI to follow a class, unless you add a new goal each time a unit of that class spawns (don't do that). What you want to do is have the AI follow the character's unit.
Make sure to clean out old AI goals as the game has a max goal limit (19). Store the goal in a variable (followGoal = AddAIGoal(...)) and delete it whenever you change it (DeleteAIGoal(followGoal)). If you're always wiping out all of a team's AI goals, forget about the variable and just use ClearAIGoals(<team number>).
The AI attempts to do all AI goals. If there are 8 AI units, a Conquest goal of 75, a Follow goal of 25, and a Deathmatch goal of 100:
* 3 units doing Conquest (75 is 3/8th of the total goal count of 200)
* 1 unit doing Follow (25 is 1/8th of 200)
* 4 units doing Deathmatch (100 is half of 200 and 4 units are half of the 8 total units)
If you don't want AI units doing Conquest until after their master dies, then don't add that goal until after the master dies (and don't forget to delete/clear the follow master goal).
Only in SP. The human's id is always zero in single-player, so the AI would always follow the human. If you want it to work in MP, use the character's unit.weapon is used primarily by the player you can just put a zero as alast argument and theyll follow him
Make sure to clean out old AI goals as the game has a max goal limit (19). Store the goal in a variable (followGoal = AddAIGoal(...)) and delete it whenever you change it (DeleteAIGoal(followGoal)). If you're always wiping out all of a team's AI goals, forget about the variable and just use ClearAIGoals(<team number>).
The AI attempts to do all AI goals. If there are 8 AI units, a Conquest goal of 75, a Follow goal of 25, and a Deathmatch goal of 100:
* 3 units doing Conquest (75 is 3/8th of the total goal count of 200)
* 1 unit doing Follow (25 is 1/8th of 200)
* 4 units doing Deathmatch (100 is half of 200 and 4 units are half of the 8 total units)
If you don't want AI units doing Conquest until after their master dies, then don't add that goal until after the master dies (and don't forget to delete/clear the follow master goal).
- Lagomorphia
- Major

- Posts: 502
- Joined: Sun Dec 13, 2009 10:02 am
Re: AI Goal: Follow Class
I sort of understand, but I'll need an example script if I'm going to get it working.
-
kinetosimpetus
- Imperial Systems Expert

- Posts: 2381
- Joined: Wed Mar 25, 2009 4:15 pm
- Projects :: A secret project
Re: AI Goal: Follow Class
I wanted this a long time ago, so I'm really interested in how this turns out, and I'm writing up some pseudo-code right now that, once turned into actual code, should effectively make AI follow every unit that spawns as a certain class or classes.
EDIT:
EDIT:
Code: Select all
--AI Follow Officers -- pseudocode
local officerCount = 0
local officerUnit1
local officerUnit2
local officerUnit3
...
local officerUnit7
local officerUnit8
local followGoal1
...
local followGoal8
OnCharacterSpawn(character)
if (character.getUnit().getUnitClass() == "rep_inf_ep2_officer") then
addOfficer(character)
return
end
end
function addOfficer(character)
if (officerCount == 0) then
officerUnit1 = character.getUnit()
followGoal1 = AddAIGoal(character.getTeam(), "follow", 50)
elseif(officerCount == 2)
...
end
officerCount = officerCount+1
end
OnCharacterDeath(character)
if (character.getUnit().getUnitClass() == "rep_inf_ep2_officer" then
removeOfficer(character)
end
end
function removeOfficer(character)
If (character.getUnit() == officerUnit1) then
DeleteAIGoal(followGoal1)
Elseif...
end
officerCount = officerCount-1
if followgoal1 == null then
cleanupOfficers(1)
End
If followgoal2 == null then
cleanupOffucers(2)
end
...
If followgoal7 == null then
cleanupOffucers(7)
end
end
function cleanupOfficers(index)
While index is less than 8
officerUnit<index> = officerUnit<index+1>
Index = index + 1
End
End-
THEWULFMAN
- Space Ranger
- Posts: 5557
- Joined: Tue Aug 17, 2010 3:30 pm
- Projects :: Evolved 2
- Location: Columbus, Ohio
- Contact:
Re: AI Goal: Follow Class
Looks interesting enough to make it into my mod. I fixed the 2 spelling errors btw. I will test asap.(That includes getting it turned into a proper script)kinetosimpetus wrote:I wanted this a long time ago, so I'm really interested in how this turns out, and I'm writing up some pseudo-code right now that, once turned into actual code, should effectively make AI follow every unit that spawns as a certain class or classes.
EDIT:Hidden/Spoiler:
-
kinetosimpetus
- Imperial Systems Expert

- Posts: 2381
- Joined: Wed Mar 25, 2009 4:15 pm
- Projects :: A secret project
Re: AI Goal: Follow Class
Here's what I have now, untested, but it munged ok.
It's a little wordy...
It could be made smaller (and better in many other ways) if it was done in a more "Object Oriented" way, but idk how to do that with lua...
Hidden/Spoiler:
It could be made smaller (and better in many other ways) if it was done in a more "Object Oriented" way, but idk how to do that with lua...
- DarthD.U.C.K.
- Master of the Force

- Posts: 6027
- Joined: Wed Sep 27, 2006 11:05 am
- Location: Duckburg, Germany
Re: AI Goal: Follow Class
is there really nothing like an array in lua? that would really shrink the code.
-
kinetosimpetus
- Imperial Systems Expert

- Posts: 2381
- Joined: Wed Mar 25, 2009 4:15 pm
- Projects :: A secret project
Re: AI Goal: Follow Class
I think there is, but idk how to use it.
-
THEWULFMAN
- Space Ranger
- Posts: 5557
- Joined: Tue Aug 17, 2010 3:30 pm
- Projects :: Evolved 2
- Location: Columbus, Ohio
- Contact:
Re: AI Goal: Follow Class
Am I the only one who doesnt care about the length of their lua? I would be willing to put that in every one of my scipts, and more, oh wait, thats right, I am off the walls crazy dedicated to modding this game
I am testing now, will edit the results in later
I am testing now, will edit the results in later
-
kinetosimpetus
- Imperial Systems Expert

- Posts: 2381
- Joined: Wed Mar 25, 2009 4:15 pm
- Projects :: A secret project
Re: AI Goal: Follow Class
well it would be easier to modify and maintain if it were smaller, especially if you could have it in its own script and call it with 50 other scripts instead of making the same edits to 50 scripts if there was a bug or you just wanted to change something.
EDIT: doesn't work yet. debug log shows this...
EDIT: doesn't work yet. debug log shows this...
Code: Select all
K15: Debug: officer_follow_spawn: character spawned
Message Severity: 3
.\Source\LuaHelper.cpp(312)
CallProc failed: (none):0: attempt to call global `GetUnitClass' (a nil value)
stack traceback:
(none): in function <(none):70>- Lagomorphia
- Major

- Posts: 502
- Joined: Sun Dec 13, 2009 10:02 am
Re: AI Goal: Follow Class
Couldn't you have it as a seperate script that the LUA calls for, like AI heroes?
-
kinetosimpetus
- Imperial Systems Expert

- Posts: 2381
- Joined: Wed Mar 25, 2009 4:15 pm
- Projects :: A secret project
Re: AI Goal: Follow Class
Yes, but I haven't successfully made that type of script.
Edit: It works now, kinda, the AI will follow each other.
However, I once saw a group of AI with one officer running around in circles, actually, there were 2 groups like that.
another thing, the debug output shows that the code does not run in sync, if 2 officers spawn really close together, they might both get an AIGoal even if the max is reached already (8 supposedly, if the code ran in sync) but afaik, there's no crashes right now unless it crashes when AI goals totally max out.
continuing testing...
Edit: It works now, kinda, the AI will follow each other.
However, I once saw a group of AI with one officer running around in circles, actually, there were 2 groups like that.
another thing, the debug output shows that the code does not run in sync, if 2 officers spawn really close together, they might both get an AIGoal even if the max is reached already (8 supposedly, if the code ran in sync) but afaik, there's no crashes right now unless it crashes when AI goals totally max out.
continuing testing...
