Total War: WARHAMMER II

Total War: WARHAMMER II

Execute External Lua File (Modding Tool)
40 Comments
Lycia Pintella Apr 17, 2023 @ 1:31am 
I thought if we put our lua files under mods/script in the game folder they would be able to run (ofc they have to be under the right campaign etc right?) and I can run modded alarielle code with her filename just fine that way, but when I try to run custom stuff I get no result at all. Any ideas?
DudeChris33 May 24, 2022 @ 2:15pm 
Is this reverse compatible with other Total War games? Obviously the individual lua files you want to execute would need to be changed depending on the game but would the mod still work in them or does it reference TWW2-specific things.
madocs May 13, 2022 @ 7:40pm 
this isn't working: my lua file... any ideas?

add_bunch_of_ancies = function()
local ancies = {
"wh2_dlc10_anc_talisman_shieldstone_of_isha",
"wh2_dlc15_anc_weapon_star_lance", }
local f_obj = cm:get_faction(cm:get_local_faction_name(true))

for _, anci in ipairs(ancies) do
cm:add_ancillary_to_faction(f_obj, anci, false)
end
end

addsometraits = function()

if not mod.char_cqi then return end
local char = cm:get_character_by_cqi(mod.char_cqi)
if not char or char:is_null_interface() then return end

cm:force_add_trait(cm:char_lookup_str(char), "wh2_dlc15_trait_dragon_moon_imrik", true, 1, true)
cm:force_add_trait(cm:char_lookup_str(char), "wh2_dlc15_trait_dragon_star_imrik", true, 1, true)

end
prop joe  [author] Feb 21, 2022 @ 3:39am 
yes, whenever the workshop opens, nothing in this mod actually needs changing for WH3
JX83 Feb 16, 2022 @ 8:44pm 
You gonna make this for 3?
Azade12 Aug 12, 2021 @ 3:39pm 
i love you, pepe
prop joe  [author] Aug 12, 2021 @ 12:43pm 
Updated:
Added exec_frontend.lua that gets executed when you press the chat button in the frontend. Note that callback doesn't work in the frontend, but you can use real_timer to delay stuff in the frontend.
This will dump the UI after a 5 sec delay: https://gist.github.com/Shazbot/edb8a3af536e35c030c1c29f46d5b800
prop joe  [author] Jun 23, 2021 @ 10:44am 
Updated:
Added some error catching code that will catch errors inside callbacks and listeners, that would otherwise cause a script break. I have this in my script mods but it's now included here as well since it should be very useful just for people writing scripts as well.
prop joe  [author] Aug 16, 2020 @ 10:53am 
Added exec_first_tick.lua.lua that gets auto-executed inside a cm:add_first_tick_callback, so you can test stuff that goes in there.
Azade12 Aug 15, 2020 @ 5:40pm 
PEPE FEELZ GOOD TO DESPOIL
prop joe  [author] Aug 15, 2020 @ 4:08am 
Now if code run with Execute External Lua File has an error the error message will also show ingame if you have the Draw Debug Logs mod active.
Azade12 Jul 2, 2020 @ 10:55am 
PEPE. FEELS GOOD TO DESPOIL
ShadowCX Feb 7, 2020 @ 8:15am 
Thank you very much :)
prop joe  [author] Feb 7, 2020 @ 7:56am 
ShadowCX Feb 7, 2020 @ 6:27am 
btw, can you please paste another invitation to the modding discord? I gotta ask something, maybe someone there knows an answer
ShadowCX Feb 6, 2020 @ 4:08pm 
Well done, sir! This is working great so far!
Indeed, having the Battle Manager itself do the pcall was the key to the solution afterall, but using bm:callback as a wrapper and adding it to the listener.. that's genious!! ...also kind of ridiculous ^^'
prop joe  [author] Feb 5, 2020 @ 2:17pm 
I uploaded a version that could work in battles with F9, see if it breaks on anything.
ShadowCX Feb 4, 2020 @ 12:05pm 
Sure, go for it, hope it helps the people that need it :steamhappy:
prop joe  [author] Feb 4, 2020 @ 7:19am 
Wow, that's amazing, great work on fixing that!

I'd like to make it a part of this mod, so they're all in one place.
ShadowCX Feb 3, 2020 @ 7:23am 
It is weird indeed, but I got some good news. After ages of playing around with it, I figured out a workaround that actually works 100% from what I can tell. It still uses your base code, but instead of registering the function to a core_object callback, it now registers directly to the battle_manager as part of a command handler callback. This means it's less elegant now than it was before: you can't press F9 anymore. Instead you simply double click any of your unit cards and exec_battle.lua will be executed. So yea, less elegant for sure, but it works great with any script I have thrown at it so far. I will send you the dropbox link. You can upload it to the workshop or if you want I'll do it, fine either way. Here you go: https://www.dropbox.com/s/dhjtg5rlc8se0b8/pj_loadfile_battle_fixed.pack?dl=0
prop joe  [author] Feb 3, 2020 @ 5:33am 
It returns a function that then returns a function, and so on recursively.

Definitely some weird stuff, and I can't explain it. Don't believe it's due to missing libraries, I can force_require libs and it's still not working.
ShadowCX Jan 31, 2020 @ 7:55am 
2. bm:out("Player alliance number: " .. tostring(bm:get_player_alliance_num()));

--> Log Output >> Player alliance number: function: 000000009FF322B8

Tried using "bm", since bm:pause() does somehow work. However, as you can see even the simplest function "get_player_alliance_num()" does not return the intended integer, but rather the memory address of the function itself.


I think there's something seriously wrong.. well, anyways, I hope this was helpful to you?
ShadowCX Jan 31, 2020 @ 7:55am 
I understand that Battle Scripting is not your forte, but I have done some more tests and this time I got some error messages from the logs for you. Maybe this is gonna ring a bell?

1. local my_bm = battle_manager:new(empire_battle:new());

--> Log Output >> LOADFILE_BATTLE: [string "c:\games\steam\steamapps\common\total war warhammer ii\exec_battle.lua"]:8: attempt to index global 'battle_manager' (a nil value)

So apparently he doesn't recognize the "battle_manager" object at all, maybe some library is missing or something?
prop joe  [author] Jan 31, 2020 @ 5:04am 
The crappy answer is I have no idea, I really didn't mess with battle scripts much.

I'd try running a complicated battle script mod like Find Idle Units and see if that works.

dout is tied to the campaign script, yeah, but it could be an easy port.
ShadowCX Jan 30, 2020 @ 10:53am 
First off, thanks for your amazing work and contributions to the modding scene!
I've tried out the new loadfile_battle mod and I noticed the following issues:

1. tried to run a few extremely simple commands via the Battle Manager and not sure what to make of it:

a) bm:pause();
--> worked fine

b) bm:show_ui(false);
--> did not do anything

c) bm:show_army_panel(false);
--> did not do anything

d) dout("TEST")
--> does not work in conjunction with your other mod "Draw Debug Logs"

So d) I understand, it probably does not work in Battle Mode. But as for b) and c) I'm not sure what to make of it. I feel like that should work, so not sure if there's some issue with the mod or just my own stupidity at work here.


2. During Battle Mode the F9 Key is already bound to some Camera Bookmark, so pressing the key makes the camera jump (no idea if that might cause any of the aforementioned issues?). However, unbinding F9 breaks the mods ability to run the external .lua it seems.
prop joe  [author] Jan 29, 2020 @ 4:56pm 
Use this, I'll upload it to the workshop later:
https://www.dropbox.com/s/aw0quk59j2lkp97/pj_loadfile_battle.pack?dl=0
it uses a different file exec_battle.lua (or exec/exec_battle.lua) that gets run with F9
when using out in battles convert things to strings because the out function in battles is a different one than in campaign and does some concatenation internally, and that requires input to be a string:
out(tostring(get_bm()))
ShadowCX Jan 29, 2020 @ 4:04pm 
If I may ask, is there any way of executing the external lua file during a battle and not just campaign?
ShadowCX Jan 29, 2020 @ 10:21am 
This is insanely usefull! Also that little link you provided to the scripting docs is a game changer for me.. doesn't seem like it's possible to find via Google, so I had already abandoned all hope of getting my hands on it :3
Sorin Jan 11, 2020 @ 8:32am 
thank you!
prop joe  [author] Jan 11, 2020 @ 6:22am 
try here: https://tw-modding.com/docs/lua-tutorials/
also complete the Javascript tutorial at codecademy.com , coding is coding, the languages aren't that different and the concepts carry over

join the modding discord and ask any modding/scripting questions you have there:
https://discord.gg/ZsT7KG
Sorin Jan 10, 2020 @ 9:04pm 
great mod!I want to bind any other command,I ve never been to learned lua language,could you please give me a simple tutorial?
prop joe  [author] Dec 21, 2019 @ 9:07am 
I wanna thank you, you're doing very impressive things around here.
Anyndel Dec 21, 2019 @ 7:56am 
Great stuff, thanks for the exec2 as well, making my modding go a lot faster.
prop joe  [author] Dec 10, 2019 @ 2:36pm 
Updated:
Fixed a pretty big issue with globals from the _lib folder missing in exec scripts: stuff like random_army_manager and invasion_manager.
prop joe  [author] Dec 6, 2019 @ 9:26am 
Updated:
Added an additional file you can run: exec2.lua with F10.

Also:
If the mod doesn't find exec.lua inside the Total War WARHAMMER II folder it will also check Total War WARHAMMER II/exec/exec.lua.
So basically you can put exec.lua and exec2.lua inside an intermediate exec folder so things are better structured.
But you don't have to.

If you're using vscode I suggest you add the exec folder to a modding workspace.
prop joe  [author] Nov 23, 2019 @ 7:16am 
Yeah, the script gets run ingame, there really is no difference as far as the game is concerned.

Note that since the point in time when the script gets executed can differ, that could matter in some scripts. I had an issues where effect.get_localised_string ended up getting called too early once put into a pack, and the game would crash.

And not sure if require would behave differently, would have to test.
Anyndel Nov 21, 2019 @ 5:29am 
Shit, just saw the github thing, seems it does, thats awesome. No more restarting the game 50 times per mod
Anyndel Nov 21, 2019 @ 5:27am 
It probably doesnt, but just in case, does it work for listeners too? If I put a listener on the external lua for something to happen on turn end, press f9 and then end turn, will it execute it? Still a great idea.
prop joe  [author] Nov 20, 2019 @ 12:29pm 
You can freely make edits and run the script again, it will just get run once on every F9 and that's that.
Nanu Nov 20, 2019 @ 12:02pm 
This is fantastic, but I feel like I have to ask, will it break everything If i make edits to the script while the game is running or does it just use a file path to find and execute the script when F9 is pressed?