Tutorial:Pooled Resource Unit Costs: Difference between revisions

From Total War Modding
No edit summary
No edit summary
Line 18: Line 18:
=== 🔍 Finding UI Components: ===
=== 🔍 Finding UI Components: ===
[[File:Recruitment cost component.png|right|thumb]]
[[File:Recruitment cost component.png|right|thumb]]
The first step in building the UI is figuring out where you want your Pooled Resource cost to appear. For units this will probably be in the same place as normal recruitment and upkeep costs below the unit card.  
The first step in building the UI is figuring out where you want your Pooled Resource cost to appear, whether you want to keep the vanilla treasury recruitment cost as well as a Pooled Resource cost, or have several Pooled Resource costs. If you don't want to have any vanilla treasury recruitment cost and only one Pooled Resource cost then you can technically skip most of this section and just reuse the vanilla Recruitment Cost UIC, however it is still worth reading to help understand the process. For the purpose of this guide however we will assume you are using the vanilla treasury recruitment and upkeep costs and creating a new Pooled Resource cost which will go in the same place, below the unit card.  


In order to modify a part of the UI you need to find where it's located. To do this we can use the [https://tw-modding.com/wiki/Tutorial:Context_Viewer_(Warhammer_3) Context Viewer]. To find a UIC, open the Context Viewer, position your mouse on the screen where you want your UIC to go, and click with your mouse wheel. This will take you most, if not all, of the way to the path of your UIC. So click on the unit card then expand the unit card UIC in the Context Viewer until you find the UIC for recruitment cost. Once you've found it, paste <code>CopyFullPathToClipboard()</code> in the expression tester box. This copies the path to that UIC which is how we find it in the script.
In order to modify a part of the UI you need to find where it's located. To do this we can use the [https://tw-modding.com/wiki/Tutorial:Context_Viewer_(Warhammer_3) Context Viewer]. To find a UIC, open the Context Viewer, position your mouse on the screen where you want your UIC to go, and click with your mouse wheel. This will take you most, if not all, of the way to the path of your UIC. So click on the unit card then expand the unit card UIC in the Context Viewer until you find the UIC for recruitment cost. Once you've found it, paste <code>CopyFullPathToClipboard()</code> in the expression tester box. This copies the path to that UIC which is how we find it in the script.
Line 34: Line 34:


=== 📷 Creating / Copying Components: ===
=== 📷 Creating / Copying Components: ===
Next up you need to copy the recruitment cost UIC. To do that you do this and call it e.g. "prestige cost":
Next up you need to copy the recruitment cost UIC. To do that you need to use <code>[https://chadvandy.github.io/tw_modding_resources/WH3/campaign/uicomponent.html#function:uicomponent:CopyComponent uicomponent:CopyComponent()]</code> and name the new UIC e.g. "prestige cost" like this:
  <code>UIComponent(recruitment_cost_uic:CopyComponent("prestige_cost"))</code>
  <code>UIComponent(recruitment_cost_uic:CopyComponent("prestige_cost"))</code>


Line 41: Line 41:


=== ⬇️ Repositioning Components: ===
=== ⬇️ Repositioning Components: ===
This part is a bit more tedious. You will need to use the console (shift + f3) from Groove's Modding Development Tools to reposition the new "prestige_cost" UIC we've created. To do that you need to use <code>[https://chadvandy.github.io/tw_modding_resources/WH3/campaign/uicomponent.html#function:uicomponent:SetDockOffset uicomponent:SetDockOffset()]</code>
This part is a bit more tedious. You will need to use the console (shift + f3) from Groove's Modding Development Tools to reposition the new "prestige_cost" UIC we've created. To do that you need to use <code>[https://chadvandy.github.io/tw_modding_resources/WH3/campaign/uicomponent.html#function:uicomponent:SetDockOffset uicomponent:SetDockOffset()]</code> like this:
<code>local prestige_cost_parent_uic = find_uicomponent(unit_uic, "unit_icon", "prestige_cost")
prestige_cost_parent_uic:SetDockOffset(8, 19)</code>


The coordinates of x = 8, y = 19 are where I've worked out it needs to go but to play around with this yourself or for example for a different UIC such as lords you will need to choose some numbers and see where it ends up. Start small and work up. If you 'really' want to make sure it's in the right place you can do a bit of pixel peeping and count the pixels between e.g. the recruitment cost UIC and the upkeep cost UIC and that's how many pixels your new component should be below the recruitment cost UIC. This all changes however if you don't want any treasury recruitment cost at all and you only want the Pooled Resource cost. You wouldn't have to copy or reposition any UICs you could just reuse the existing recruitment cost UIC and follow the rest of the guide but now we're getting ahead of ourselves!


Next you need to move


UIComponent(recruitment_cost_uic:CopyComponent("prestige_cost"))
UIComponent(recruitment_cost_uic:CopyComponent("prestige_cost"))

Revision as of 06:20, 10 July 2023

Originally written by Pear

💬 Let's Talk Pooled Resources

Pooled Resources are an important part of what makes any faction unique. The problem is they're often not used for very much. This tutorial aims to fix that by explaining how to use Pooled Resources as costs using units in the main recruitment panel as an example. The theory used here can be applied to a range of other things such as RoRs, lords, agents, buildings, and technologies or anything you want really.

This guide is also my submission for Mod Jam #7, along with an example mod for making units cost Pooled Resources in the main recruitment panel.

🧑‍🎓 The Theory

If you're not familiar with Lua UI modding and UI components (UICs) or scripting in general then the UI Tutorial Series is a good place to start as this guide won't explain everything about UI, just the main things we need to know which are:

  1. Building the UI: This involves finding existing UICs, creating / copying UICs, and repositioning them.
  2. Handling the UI: This involves handling refreshes to the UI, making sure UIC are enabled / disabled, and have the right text, tooltips, and icons at the right time.
  3. Handling the Cost: This involves taking away / returning the Pooled Resource to the player in a multiplayer-friendly way.

Some other things you will need are my example mod, Groove Wizard's Visual Studio development environment for scripting, Groove's Modding Development Tools: Lua Console, and Groove's scripting documentation (yes this man is a machine).

👷 Building the UI

🔍 Finding UI Components:

Recruitment cost component.png

The first step in building the UI is figuring out where you want your Pooled Resource cost to appear, whether you want to keep the vanilla treasury recruitment cost as well as a Pooled Resource cost, or have several Pooled Resource costs. If you don't want to have any vanilla treasury recruitment cost and only one Pooled Resource cost then you can technically skip most of this section and just reuse the vanilla Recruitment Cost UIC, however it is still worth reading to help understand the process. For the purpose of this guide however we will assume you are using the vanilla treasury recruitment and upkeep costs and creating a new Pooled Resource cost which will go in the same place, below the unit card.

In order to modify a part of the UI you need to find where it's located. To do this we can use the Context Viewer. To find a UIC, open the Context Viewer, position your mouse on the screen where you want your UIC to go, and click with your mouse wheel. This will take you most, if not all, of the way to the path of your UIC. So click on the unit card then expand the unit card UIC in the Context Viewer until you find the UIC for recruitment cost. Once you've found it, paste CopyFullPathToClipboard() in the expression tester box. This copies the path to that UIC which is how we find it in the script.

When copied it will look like this: ":root:units_panel:main_units_panel:..." and so on.

Referencing my example mod, to find the recruitment cost UIC with a script you write it like this:

local recruitment_uic = find_uicomponent(core:get_ui_root(), "units_panel", "main_units_panel", "recruitment_docker", "recruitment_options", "recruitment_listbox", "local1", "unit_list", "listview", "list_clip", "list_box", "wh_main_emp_inf_swordsmen_recruitable", "unit_icon", "RecruitmentCost") 

It is fine as it is however it can be split into smaller, easier-to-access parts like this:

local recruitment_uic = find_uicomponent(core:get_ui_root(), "units_panel", "main_units_panel", "recruitment_docker", "recruitment_options", "recruitment_listbox", recruitment_type)
local listview_uic = find_uicomponent(recruitment_uic, "unit_list", "listview")
local unit_uic = find_uicomponent(listview_uic, "list_clip", "list_box", "wh_main_emp_inf_swordsmen_recruitable")
local recruitment_cost_uic = find_uicomponent(unit_uic, "unit_icon", "RecruitmentCost")

📷 Creating / Copying Components:

Next up you need to copy the recruitment cost UIC. To do that you need to use uicomponent:CopyComponent() and name the new UIC e.g. "prestige cost" like this:

UIComponent(recruitment_cost_uic:CopyComponent("prestige_cost"))

Using the paths we've previously established, the path for this new "prestige_cost" UIC we've created is therefore:

local prestige_cost_parent_uic = find_uicomponent(unit_uic, "unit_icon", "prestige_cost")

⬇️ Repositioning Components:

This part is a bit more tedious. You will need to use the console (shift + f3) from Groove's Modding Development Tools to reposition the new "prestige_cost" UIC we've created. To do that you need to use uicomponent:SetDockOffset() like this:

local prestige_cost_parent_uic = find_uicomponent(unit_uic, "unit_icon", "prestige_cost")
prestige_cost_parent_uic:SetDockOffset(8, 19)

The coordinates of x = 8, y = 19 are where I've worked out it needs to go but to play around with this yourself or for example for a different UIC such as lords you will need to choose some numbers and see where it ends up. Start small and work up. If you 'really' want to make sure it's in the right place you can do a bit of pixel peeping and count the pixels between e.g. the recruitment cost UIC and the upkeep cost UIC and that's how many pixels your new component should be below the recruitment cost UIC. This all changes however if you don't want any treasury recruitment cost at all and you only want the Pooled Resource cost. You wouldn't have to copy or reposition any UICs you could just reuse the existing recruitment cost UIC and follow the rest of the guide but now we're getting ahead of ourselves!

Next you need to move

UIComponent(recruitment_cost_uic:CopyComponent("prestige_cost"))

✋ Handling the UI

💰 Handling the Cost