| General | Hosted Sites | Civ5 | CivRev | Civ4Col | Civ4 | Civ3 | Civ2 | Civ1 | Misc | Marketplace |
![]() |
|
|
Welcome to Civilization Fanatics' Center. You are currently viewing our site as a guest which gives you limited access to our site features. By joining our free community, you will be able to participate in the discussions, search the forum, send private messages, vote in polls, upload your own screenshots to the gallery, and access many other special features. Registration is fast, simple and absolutely free, so sign up today! If you have any problems with the registration process or your account login, please contact support. |
|
|||||||
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Chieftain
Join Date: Oct 2010
Posts: 82
|
Creating UI mods with lua & xml
This tutorial is meant to help you to create a simple UI mod. You will learn how to create a simple button which will change it's caption once you press it. It will also give you tips on using lua-scripts, xml-elements, the Tuner and how to write not-corefile-dependant mods. Before reading this tutorial, I'd recommend first reading this excellent Modders Guide from Kael.
Changelog: 2010-11-25 Added chapter 10 about source code 1. You are trying to trick me into reading some kind of manual! No way, Jose! Here's a quick list of steps how to create a simple mod with an UI element.
There are two types of xml-elements used in Civ: <GameData> and <Context>. GameData-elements changes the game rules and has nothing to do with this tutorial. Context-elements on the other hand create all the windows and buttons you can see. Actually almost everything you can see in Civ is created with Context-elements. So when ever this tutorial refers to xml it's always about Context-elements. 3. Local warming a.k.a why my neighbors ice caps aren't melting There are three different levels of environment scope in Civ's Lua-engine. If you fail to change a valua or call a function then it most likely because it doesn't exist in your current environment.
If all your mods values and functions are within a single thread then you can eg. call a function from anywhere simply like this: MyFunction() If your mods values and functions are spread in different threads then it's not that simple anymore. You can't call a function within a different thread with normal methods. You have to do that using LuaEvent's or ShareData. I would recommend that you try to create a single thread for your mod if it's possible. If you have only one lua- and xml-file in your mod then that's easy:
If you have multiple files in your mod then you can put them in a single thread using this method.
Normally you would access an xml-element within your thread like this: Code:
Controls.MyButton:SetHide(true) Code:
ContextPtr:LookUpControl("PracticeMod2Button1/MyButton"):SetHide(true)
6. OMG! My Tuner is live! Or on fire? Personally I could almost live without ModBuddy, but I would refuse to do any modding without Firaxis Live/FireTuner. This program is a Lua console that you can use to either keep track watch happening with all the lua stuff behind the game engine or run your own lua commands. Here's what you can do with it.
7. Waiter, there's a ContextPtr in my Tooltip! ContextPtr object can be accessed from all threads. You can use it to either create new threads or manipulate xml-elements in any thread.
Code:
--This command creates a new thread. The functions argument is the name
--of the lua- and xml-file you wish to use.
--You don't normally need to use this command.
ContextPtr:LoadNewContext("FileNameWithoutTail")
Code:
--Will hide your context-element and everything in it. ContextPtr:SetHide(true) Code:
--Everytime when you hide or show this Context-element then this function is called. ContextPtr:SetShowHideHandler(ShowHideFunction) Code:
--Everytime when you press a key or move your mouse this function is called. --Be careful with this one since it's called very often. ContextPtr:SetInputHandler(InputHandlerFunction) Code:
--This is a powerful command to refer to any element (not just Context) with ID
--within any thread. You could use it to manipulate your own sub-threads
--or even the elements in core files.
ContextPtr:LookUpControl("/ParentThreadName/SubThreadName/ElementId")
In Civ you can have multiple mods functioning at the same time so you should try to design your mods so that they will interface other mods as little as possible. Maybe the biggest question is when and how you should modify the core-files (eg. InGame.xls)? Core-files are the lua- and xml-files that come with the vanilla Civ. You can overwrite those core-files by simply adding a file with the same name into your mod. If possible you should avoid modifying them or at least do it the "right way".
__________________
Civ 5: Blog about development of CivWillard 2.0 -> One Lucky Build Civ 5: [UI]CivWillard - Easy City & Diplo management Civ 5: [TUTORIAL] Creating UI mods with lua & xml Last edited by Onni; Nov 25, 2010 at 12:49 AM. |
|
|
|
|
|
#2 |
|
Chieftain
Join Date: Oct 2010
Posts: 82
|
9. Mom! Where are my socks!?
I've attached templates from the mods that I used to create these examples. You can create a new project in ModBuddy using them as the template so that you can see for yourself how they work. To be able to see these templates you should save them (do not extract them, but just leave them as zip's) into this directory: "..\My Documents\Firaxis ModBuddy\Templates\ProjectTemplates\" (eg. ..\ProjectTemplates\PracticeMod.zip) Remember to look into the top level folder in ModBuddy to see these templates: 10. The source is out there Here is the source code for PracticeMod: Spoiler:
And the source code for PracticeMod2: Spoiler:
Seen you in the game!
__________________
Civ 5: Blog about development of CivWillard 2.0 -> One Lucky Build Civ 5: [UI]CivWillard - Easy City & Diplo management Civ 5: [TUTORIAL] Creating UI mods with lua & xml Last edited by Onni; Nov 25, 2010 at 12:45 AM. |
|
|
|
|
|
#3 |
|
Chieftain
Join Date: Oct 2010
Posts: 82
|
Again reserved for later use...
__________________
Civ 5: Blog about development of CivWillard 2.0 -> One Lucky Build Civ 5: [UI]CivWillard - Easy City & Diplo management Civ 5: [TUTORIAL] Creating UI mods with lua & xml |
|
|
|
|
|
#4 |
|
Knight
Join Date: Aug 2005
Posts: 750
|
Do you know how to edit the UI for trade table in diplomacy windows?
I tried editing SimpleDiploTrade.xml but no change occurs in the game. |
|
|
|
|
|
#5 |
|
The Mad Modder
Join Date: Sep 2010
Posts: 1,321
|
Ooooo this is just what I need! Thanks, I'll be looking through this.
__________________
Art of War Mod! by Ordian. It rocks. France- A double Anceint Era Declaration of War on Deity against two clearly conquest geared civs that dredged on for 1500 years. Okay this one makes the above one look like a bunch of preschoolers out at a picnic getting attacked by baby ants. THIS ONE is awesome. Read it. Mega Mod What started as a balance fix is now getting massive. Now accepting suggestions (units/buildings in particular) Beatles/Elvis -4.3 Talker/Listener +7.2 |
|
|
|
|
|
#6 |
|
Running Spider
|
Yeah, i agree... this whole fantastic stuff should be directly linked with the Wiki.
While i'm here. Onni, could you look into "adding" some virtual button for quick access to Fire-Tuner without having to alt-tab out of everything during runtime?
__________________
Z'Stuff Reference Threads... Advisors, Eras+Center, UI, LeoPaRd, -- yep, Life itself is ART. __________________ Z'Helps while... SpreadSheet'ing the Facts, TerraForming and, Trying to Resolve Tactical 1upT or Multiplying CS Variety! |
|
|
|
|
|
#7 |
|
Warlord
Join Date: Oct 2010
Posts: 111
|
This was some quite usefull information. Especially the Contextptr info. I need to digest this information and do some experimenting. Thanx a lot!
|
|
|
|
|
|
#8 |
|
Warlord
Join Date: Oct 2010
Posts: 111
|
You know if it is possible to change the order in which the different lua-parts are anchored in the toppanel? Like if I would like the Gold per Turn to display on the right, is that possible?
|
|
|
|
|
|
#9 | |||
|
Chieftain
Join Date: Oct 2010
Posts: 82
|
Sorry for the late answers, but I was taking a little break from Civ modding..
![]() Quote:
Quote:
Quote:
Code:
ContextPtr:BuildInstanceForControlAtIndex( "ItemInstance", controlTable, Controls.Stack, i-1 );
__________________
Civ 5: Blog about development of CivWillard 2.0 -> One Lucky Build Civ 5: [UI]CivWillard - Easy City & Diplo management Civ 5: [TUTORIAL] Creating UI mods with lua & xml |
|||
|
|
|
|
|
#10 |
|
Knight
Join Date: Aug 2005
Posts: 750
|
Do you know how to change the order of displayed contents in lua, like bringing a box element forward. I know that elements are shown in order of their location in the xml file, is there a lua function to change this order?
__________________
Modding:..Food Economy - Tradable Bonus Resources .... Resource Generator - Lua component .... Have Fun - Cheat Engine |
|
|
|
|
|
#11 |
|
Eat, Sleep, Mod
Join Date: May 2008
Location: CA, USA
Posts: 1,182
|
This is a great tut - I used it to start my UI mod a while ago which I have been taking a break from recently - but could I get a tut on how to do all this with dynamic buttons? Sure it's pretty handy for a static button format, but my mod is constantly updating with in-game progress and as random things through random events unlock, more buttons automatically spawn. I need to know how to get the Lua to understand which button was pressed since dynamic box instances all contain the same name / ID.
__________________
"The sweet taste of victory is a flavor that never runs bland" - Civ Fuehrer Last edited by Civ Fuehrer; Mar 22, 2011 at 10:00 AM. |
|
|
|
|
|
#12 |
|
Prince
Join Date: Nov 2010
Location: Belgium
Posts: 566
|
Great guide! I was getting lost in my erratic lua code; the info here made things clear for me! Why didn't I read this earlier...
__________________
Building Made Fun going strong with 25,000+ downloads Terra Nostra's last version got thumbs up by 500+ people Uomo Universalis wraps it all up: Try it now! (vanilla only)
|
|
|
|
![]() |
| Bookmarks |
|
| Thread Tools | |
|
|