How to add a LUA script with a UI context.

fekfek

Chieftain
Joined
Aug 14, 2021
Messages
10
Forgive me if this seems like a silly post, but I just spent about two hours googling for an answer to this problem, and found absolutely nothing until I stumbled onto a modding discord and dug through years of message history search results. I'd like to change that, and hopefully save some other newcomers some heartache.

(I'm also secretly hoping that Cunningham's Law comes into play, here, and someone else comes along and explains a simpler or easier answer to this question.)

If you've made it this far, you probably understand that scripts in Civ6 can exist in either a "Game" or a "UI" context, and some code only works in one context or the other. If you're trying to do something that only works in a UI context, you need to be running your script in a UI context. But that leads to a question that was surprisingly difficult to find an answer to:

So how do you run a script in UI context?

In your .modInfo file (or your Project Properties, in modbuddy), you need to add your script to your project's modInfo with a AddUserInterfaces action (instead of AddGameplayScripts).

That AddUserInterfaces action must have a Custom Property (name: "Context", value: "InGame").

Furthermore, even once it's successfully added to your project, the script only seems to run if you also include an identically-named XML file (for instance, "UI/myCode.lua" and "UI/myCode.xml") in the same AddUserInterfaces action, essentially tricking the game into "loading" that invisible new piece of the UI.

EDIT: After some playtesting and debugging, it actually looks like you ONLY want to add the XML file; the game sometimes yells at you if you include the XML file and the LUA file in your AddUserInterfaces action.
 
Last edited:
no easier way AFAIK, I would I've thought the process would be simpler in modbuddy.

And there has sadly been no update to the now obsolete modinfo reference thread since the syntax of that file completely changed.

Moderator Action: also moved to Tutorial & Reference.
 
Top Bottom