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.
Re: AI Goal: Follow Class
Posted: Mon Apr 18, 2011 9:15 am
by THEWULFMAN
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.
Re: AI Goal: Follow Class
Posted: Mon Apr 18, 2011 9:24 am
by Lagomorphia
Probably because both ideas were spawned off of the idea I told you two days ago about those droid squads.
Re: AI Goal: Follow Class
Posted: Mon Apr 18, 2011 10:03 am
by DarthD.U.C.K.
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.
Re: AI Goal: Follow Class
Posted: Mon Apr 18, 2011 10:18 am
by Lagomorphia
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?
OnCharacterDispenseControllableTeam(
function(character,controlled)
if GetEntityClass(controlled) == GetEntityClassPtr(TroopSpawnerWeapon) then
local SupportTeam = 3 --Team the new spawned character is in (change this)
local teamSize = GetTeamSize(SupportTeam)
for i = 0, teamSize-1 do
local characterIndex = GetTeamMember(SupportTeam, i)
local charUnit = GetCharacterUnit(characterIndex)
if not charUnit then
local destination = GetEntityMatrix(GetCharacterUnit(character))
SpawnCharacter(characterIndex,destination)
end
end
end
end,
CIS --User team (change this)
)
Re: AI Goal: Follow Class
Posted: Mon Apr 18, 2011 10:26 am
by DarthD.U.C.K.
you can put it under local supportteam.
about conquest: put a conquesgoal with a priority of 1 above/below the other goal
Re: AI Goal: Follow Class
Posted: Mon Apr 18, 2011 8:57 pm
by [RDH]Zerted
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.
weapon is used primarily by the player you can just put a zero as alast argument and theyll follow him
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.
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).
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 3:43 am
by Lagomorphia
I sort of understand, but I'll need an example script if I'm going to get it working.
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 10:06 am
by kinetosimpetus
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.
--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
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 10:47 am
by THEWULFMAN
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:
--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
cleanupOfficers(2)
end
...
If followgoal7 == null then
cleanupOfficers(7)
end
end
function cleanupOfficers(index)
While index is less than 8
officerUnit<index> = officerUnit<index+1>
Index = index + 1
End
End
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)
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 1:01 pm
by kinetosimpetus
Here's what I have now, untested, but it munged ok.
Hidden/Spoiler:
[code]
------------------------
-- AI Follow Officers --
------------------------
--kinetosimpetus 4/19/2011
local officerCount = 0
local officerMax = 8
local officerCharacter1
local officerCharacter2
local officerCharacter3
local officerCharacter4
local officerCharacter5
local officerCharacter6
local officerCharacter7
local officerCharacter8
local followGoal1
local followGoal2
local followGoal3
local followGoal4
local followGoal5
local followGoal6
local followGoal7
local followGoal8
if (identified == 0) then
print("K15: Debug: removeOfficer: character unidentified as officer")
end
if followgoal1 == null then
cleanupOfficers(1)
end
if followgoal2 == null then
cleanupOffucers(2)
end
if followgoal3 == null then
cleanupOffucers(3)
end
if followgoal4 == null then
cleanupOffucers(4)
end
if followgoal5 == null then
cleanupOffucers(5)
end
if followgoal6 == null then
cleanupOffucers(6)
end
if followgoal7 == null then
cleanupOffucers(7)
end
end
if (identified == 0) then
print("K15: Debug: cleanupOfficers: index not recognised, not shifting")
end
end
----------------------------
-- End AI Follow Officers --
----------------------------
[/code]
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...
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 1:10 pm
by DarthD.U.C.K.
is there really nothing like an array in lua? that would really shrink the code.
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 1:14 pm
by kinetosimpetus
I think there is, but idk how to use it.
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 1:16 pm
by THEWULFMAN
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
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 4:11 pm
by kinetosimpetus
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.
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>
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 5:46 pm
by Lagomorphia
Couldn't you have it as a seperate script that the LUA calls for, like AI heroes?
Re: AI Goal: Follow Class
Posted: Tue Apr 19, 2011 6:09 pm
by kinetosimpetus
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.