[Solved] Trouble with heroes in a user script
Posted: Sun Apr 19, 2020 2:50 pm
I made an AI hero user script. It has a few different modes like normal AI heroes or BF1 style heroes etc. Every mode works on it's own. Now I'm trying to implement a dice roll system which randomly selects a different mode at the start of each mission. The random number generation and switching modes also isn't a problem. Some modes let the player unlock heroes. Other modes need player heroes turned off eg BF1.
At the moment, when I play an instant action playlist, I go through a few missions where the player should be allowed to unlock the hero and they work fine. Then I'll roll BF1 and it will turn off the player's ability to unlock heroes so it's AI only for that mission (also what it should be doing). Then, the next mission in the playlist where the player should be able to unlock heroes, it doesn't work. The heroes stay locked, like it somehow remembers that I'd turned them off in a previous mission. I have been using EnableSPScriptedHeroes() to turn off the player's ability to unlock heroes. I can go through several missions in a row without hitting EnableSPScriptedHeroes(), then one mission will use it and suddenly it's permanently on. I can't tell what's happening.
So now I've made a test user script where I've taken out all the timers and pretty much everything I don't need in order to test this. It has 2 modes: in mode 1, I want AI heroes to be on and player heroes to be off; in mode 2, I want AI heroes to be off and player heroes to be on. The reason I'm alternating the AI heroes is so I can tell what mode the mission is supposed to be in when I'm playing it. This is the test user script (cut into 4 parts):
Section 1) This is the user script header. It's not a section of interest but I'm providing it for context.
Section 2) This is the random number generation which selects the mode. I'm confident it works.
Section 3) This is a whole bunch of functions which spawn AI heroes. I'm confident this works too. Also the first of these functions is referencing globals from another user script (SPB). SPB's user script has some code injected into SetHeroClass which copies the name of the hero class and saves it (in SPB_Hero1Store or SPB_Hero2Store, depending on the team). Everything else in SPB is unrelated to heroes; it's just replacing super battle droids and magnagaurds etc with patched versions. It does this for Han Solo which is why it hacks SetHeroClass.
Section 4) This is what I want everybody to look at. ScriptPostLoad() is not spawning AI heroes every mission so that much is working. It only does it when SPD_modeSelect == 1. But something about this user script is making it so that if EnableSPScriptedHeroes() get's used one mission, then every mission after that will remember it's been used and the AI heroes will stay locked. Even though EnableSPScriptedHeroes() is inside 'if SPD_modeSelect == 1 then' with the code that spawns AI heroes. I don't know what's wrong with it.
The user script is called SPD if I haven't made that obvious.
I'm trying to come up with alternative ways to lock heroes in a mission than using EnableSPScriptedHeroes(). I've tried hacking and preventing EnableSPHeroRules() like so:
This also doesn't work. I can get 'EnableSPHeroRules prevented' to come up in the debug log of BF2ModTools.exe but the game's acting like it doesn't need EnableSPHeroRules() and the heroes are still unlockable to the player (even though I'm pretty sure leaving this line out of the mission lua disables hero unlocks). Does anybody know how to use the other Hero lua like SetHeroUnlockRule or SetHeroRespawnRule? If so, I might be able to rescript the hero unlock rules with EnableSPScriptedHeroes() turned on but I don't know the arguments for these lua commands. Or can anybody think of an alternative way to turn heroes off (which doesn't involve disabling SetHeroClass either; I need that for the AI heroes to work)?
Most of this problem involves going several missions deep into an instant action playlist so I can't use BF2ModTools.exe to debug that much.
Edit: I have made a new thread about the problem with EnableSPScriptedHeroes(). If anyone wants to follow that they can find it here. It's not a replacement for this thread, because I'm mostly looking for ideas on how to turn heroes on and off in my user script without using EnableSPScriptedHeroes() here. Or, if anyone knows how to script heroes with the other lua that would be helpful too. The new thread on the other hand, is about lua breaking instant action playlists. Whereas this thread you're in now is about anything and everything that might get this user script to work.
Edit 2: Solution
In the end what I've done is keep EnableSPScriptedHeroes() in, but make a function to recreate some of the instant action hero rules. I haven't got it set up for points but here is a scripted hero spawn with a 60 second unlock timer (and 90 second timer for the 2nd, 3rd, 4th... unlocks):
This is designed so I can turn human hero unlocks off for some missions with SPD_HumanHeroSetup(0) and turn them on with SPD_HumanHeroSetup(1). It's not so robust that you can turn it on then off in the same mission, but it's good enough for what I'm doing. Totally ignores the instant action settings too, unfortunately. Discovered how to use SetHeroRespawnRule and SetHeroPlayerRule from an old gametoast post. Still no luck with SetHeroUnlockRule though.
At the moment, when I play an instant action playlist, I go through a few missions where the player should be allowed to unlock the hero and they work fine. Then I'll roll BF1 and it will turn off the player's ability to unlock heroes so it's AI only for that mission (also what it should be doing). Then, the next mission in the playlist where the player should be able to unlock heroes, it doesn't work. The heroes stay locked, like it somehow remembers that I'd turned them off in a previous mission. I have been using EnableSPScriptedHeroes() to turn off the player's ability to unlock heroes. I can go through several missions in a row without hitting EnableSPScriptedHeroes(), then one mission will use it and suddenly it's permanently on. I can't tell what's happening.
So now I've made a test user script where I've taken out all the timers and pretty much everything I don't need in order to test this. It has 2 modes: in mode 1, I want AI heroes to be on and player heroes to be off; in mode 2, I want AI heroes to be off and player heroes to be on. The reason I'm alternating the AI heroes is so I can tell what mode the mission is supposed to be in when I'm playing it. This is the test user script (cut into 4 parts):
Section 1) This is the user script header. It's not a section of interest but I'm providing it for context.
Hidden/Spoiler:
Hidden/Spoiler:
Hidden/Spoiler:
Hidden/Spoiler:
I'm trying to come up with alternative ways to lock heroes in a mission than using EnableSPScriptedHeroes(). I've tried hacking and preventing EnableSPHeroRules() like so:
Hidden/Spoiler:
Most of this problem involves going several missions deep into an instant action playlist so I can't use BF2ModTools.exe to debug that much.
Hidden/Spoiler:
Edit 2: Solution
In the end what I've done is keep EnableSPScriptedHeroes() in, but make a function to recreate some of the instant action hero rules. I haven't got it set up for points but here is a scripted hero spawn with a 60 second unlock timer (and 90 second timer for the 2nd, 3rd, 4th... unlocks):
Code: Select all
-- ========== SPD_HumanHeroSetup(flag) ==========
-- function to spoof some instant action hero rules
-- necessary for compatability with EnableSPScriptedHeroes()
-- locks human heroes when flag == 0
-- standard warning message
if SPD_HumanHeroSetup then
print("SPD (user script "..SPD_scriptnum.."): Warning: Function SPD_HumanHeroSetup already exists")
end
function SPD_HumanHeroSetup(flag)
EnableSPScriptedHeroes()
if flag == 1 then
-- hero first unlocked after a 60 second timer
CreateTimer("HumanHeroUnlock")
SetTimerValue("HumanHeroUnlock", 60)
StartTimer("HumanHeroUnlock")
OnTimerElapse(
function (timer)
-- unlock both heroes
for team = 1, 2 do
SetHeroPlayerRule("Best")
UnlockHeroForTeam(team)
-- 90 second hero respawn for humans
SetHeroRespawnRule("90")
end
DestroyTimer(timer)
end,
"HumanHeroUnlock"
)
end
end
-- ==============================================