• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Efficient trigger for AI learn skill

Status
Not open for further replies.
Level 5
Joined
Jan 6, 2010
Messages
115
I didn't find much on this from searching the forums, and I find this to be surprisingly complicated;
I have a bunch of different AI heroes running around on the map, some of the same type, and they need to learn their skills (use their skill points).
Preferably, the function should feature a randomized skill-build and be non-leaking. And above all, as simple as possible. But I'm not sure what the best approach is..

Should I have periodic event or try to trigger it on level-up? The complication with the latter is that the heroes level several times at once on creation.

This trigger is probably something that is done a lot of times previously, so perhaps there's a well-established method out there? Grateful for all help :)
 
There does exist such an action: Hero - Learn Skill

Usually, yes for AI peridoc events are needed, but in this case, no. But the conditonal part is up to you!

Because you can use the event "A Unit Levels Up". (or something like this)
So when this happens, you check if he is an AI-hero, and then you might need to check for his HeroType and his CurrerntLevel to decide what Skill should be learned.
 
Level 5
Joined
Jan 6, 2010
Messages
115
I think I'll go for Periodic Event, randomly running Learn Skill action for all AI heroes. As far as I understand, I don't even have to check what kind of hero it is, since if the hero doesn't have the skill, the action will simply be ignored, right?
Then I also avoid the hassle of checking the current skill level and all that to make sure the skill upgrade is really available.

I'm not sure how I can manage this without using the leakey "pick all units in unit group" action, though. Learn Skill only lets me target a single unit variable.
If I use Set Variable - Random unit in unit group, and later remove the variable, it will still leak, right?
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
You should definitely go with IcemanBo's solution and use an Event as the hook in for having the AI hero learn a skill. Since the heroes will probably be owned by the same set of players (assuming a human player doesn't have any heroes automatically managed for them), you can just do a simple player check inside the trigger condition.

The periodic timer way is a really naive solution (feels like having a computer output random characters and one day hoping to get Shakespeare), and the majority of time you'll be wasting memory/resources, which is crucial since AI should be efficient and leakless as you said in your OP.

I've run into the problem too that the event - A Unit Levels Up, only runs once, even if the hero leveled up five times simultaneously. For that, you'll need to detect how many levels were gained. This could be done by assigning an integer variable to each AI hero, and having it mimic the AI's hero level. When the AI levels up, calculate the difference between their variable level and their new level (via Get Hero Level).

Then simply select a random skill for each new level (loop basically). That should get around the slight problem with A Unit Levels up event.

Afterwards, change the variable to reflect the AI hero's new level.

JASS:
local unit u = GetTriggerUnit() //the hero instance
local integer newLevel //the new level of the hero
local integer gain //how many levels the hero gained
if GetOwningPlayer(u) == AI_PLAYER then //make sure it's a computer hero
  set newLevel = GetHeroLevel(u)
  set gain = newLevel - oldLevel
  loop
    exitwhen gain == 0
    //now your code here for choosing a skill to learn
    //call LearnSkill(u, ...)
    ...
    set gain = gain - 1
  endloop
  set oldLevel = newLevel
endif
 
Level 5
Joined
Jan 6, 2010
Messages
115
Ok, you're right, triggering it on levelling event and retrieve unspent skill points is probably the best way.
I guess also that I should an ability-array and just randomize the index-number to choose a skill. Actually, if 'Learn Skill'-action has no effect if the hero doesn't have the skill, I can do with only one array and drop the unit-type check.
 
Status
Not open for further replies.
Top