Total War: WARHAMMER II

Total War: WARHAMMER II

Execute External Lua File (Modding Tool)
40 kommentarer
Lycia Pintella 17. apr. 2023 kl. 1:31 
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 24. maj 2022 kl. 14:15 
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 13. maj 2022 kl. 19:40 
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  [ophavsmand] 21. feb. 2022 kl. 3:39 
yes, whenever the workshop opens, nothing in this mod actually needs changing for WH3
JX83 16. feb. 2022 kl. 20:44 
You gonna make this for 3?
Azade12 12. aug. 2021 kl. 15:39 
i love you, pepe
prop joe  [ophavsmand] 12. aug. 2021 kl. 12:43 
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  [ophavsmand] 23. juni 2021 kl. 10:44 
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  [ophavsmand] 16. aug. 2020 kl. 10:53 
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 15. aug. 2020 kl. 17:40 
PEPE FEELZ GOOD TO DESPOIL
prop joe  [ophavsmand] 15. aug. 2020 kl. 4:08 
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 2. juli 2020 kl. 10:55 
PEPE. FEELS GOOD TO DESPOIL
ShadowCX 7. feb. 2020 kl. 8:15 
Thank you very much :)
prop joe  [ophavsmand] 7. feb. 2020 kl. 7:56 
ShadowCX 7. feb. 2020 kl. 6:27 
btw, can you please paste another invitation to the modding discord? I gotta ask something, maybe someone there knows an answer
ShadowCX 6. feb. 2020 kl. 16:08 
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  [ophavsmand] 5. feb. 2020 kl. 14:17 
I uploaded a version that could work in battles with F9, see if it breaks on anything.
ShadowCX 4. feb. 2020 kl. 12:05 
Sure, go for it, hope it helps the people that need it :steamhappy:
prop joe  [ophavsmand] 4. feb. 2020 kl. 7:19 
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 3. feb. 2020 kl. 7:23 
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  [ophavsmand] 3. feb. 2020 kl. 5:33 
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 31. jan. 2020 kl. 7:55 
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 31. jan. 2020 kl. 7:55 
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  [ophavsmand] 31. jan. 2020 kl. 5:04 
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 30. jan. 2020 kl. 10:53 
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  [ophavsmand] 29. jan. 2020 kl. 16:56 
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 29. jan. 2020 kl. 16:04 
If I may ask, is there any way of executing the external lua file during a battle and not just campaign?
ShadowCX 29. jan. 2020 kl. 10:21 
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 11. jan. 2020 kl. 8:32 
thank you!
prop joe  [ophavsmand] 11. jan. 2020 kl. 6:22 
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 10. jan. 2020 kl. 21:04 
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  [ophavsmand] 21. dec. 2019 kl. 9:07 
I wanna thank you, you're doing very impressive things around here.
Anyndel 21. dec. 2019 kl. 7:56 
Great stuff, thanks for the exec2 as well, making my modding go a lot faster.
prop joe  [ophavsmand] 10. dec. 2019 kl. 14:36 
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  [ophavsmand] 6. dec. 2019 kl. 9:26 
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  [ophavsmand] 23. nov. 2019 kl. 7:16 
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 21. nov. 2019 kl. 5:29 
Shit, just saw the github thing, seems it does, thats awesome. No more restarting the game 50 times per mod
Anyndel 21. nov. 2019 kl. 5:27 
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  [ophavsmand] 20. nov. 2019 kl. 12:29 
You can freely make edits and run the script again, it will just get run once on every F9 and that's that.
Nanu 20. nov. 2019 kl. 12:02 
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?