Last active
October 8, 2022 13:29
-
-
Save EddieEldridge/cf140b5c5f7e06101d3c6964109733ba to your computer and use it in GitHub Desktop.
M2TWEOP Tutorials: Part 1 - Scripting Basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- CHANGING THE SETTLEMENT MODEL WHEN A SETTLEMENT IS UPGRADED AND WHEN YOU RELOAD THE GAME/RELOAD THE CAMPAIGN MAP | |
-- Credits: Medik | |
-- EOP Documentation: https://youneuoy.github.io/M2TWEOP-library/_static/LuaLib/index.html | |
-- Check for nil values | |
function isNotNull(object) | |
if(object~=nil) then | |
return true; | |
else | |
return false; | |
end | |
end | |
-- Returns the settlement struct if the settlement exists | |
-- Credits to Fynn | |
function getSettlementbyName(name) | |
local factionsNum = stratmap.game.getFactionsCount(); | |
for i = 0, factionsNum - 1 do | |
local faction = stratmap.game.getFaction(i); | |
local settsNum = faction.settlementsNum; | |
for i = 0, settsNum - 1 do | |
local sett = faction:getSettlement(i) | |
if sett.name == (name) then | |
return sett; | |
end | |
end | |
end | |
end | |
-- Returns the building and settlement struct if the settlement has the specified building | |
function getBuildingInSettlement(buildingName, settlementName) | |
settlement = getSettlementbyName(settlementName); | |
for i = 1, settlement.buildingsNum do | |
building = settlement:getBuilding(i); | |
if building:getName() == buildingName then | |
return building, settlement; | |
else | |
return nil; | |
end | |
end | |
end | |
-- When the plugin is loaded, add our models for use later | |
function onPluginLoad() | |
-- We add the model paths and give them an id (e.g 1,2,3) | |
stratmap.objects.addModelToGame("data/models_strat/residences/faction_variants/gondor_t5_large_city.cas", 1); | |
stratmap.objects.addModelToGame("data/models_strat/residences/faction_variants/venice/evil_men_t3_large_town.cas", 2); | |
stratmap.objects.addModelToGame("data/models_strat/residences/desert_fort.cas", 3); | |
end | |
-- Called whenever a settlement is upgraded | |
function onSettlementUpgraded(settlement) | |
-- Print some info about the settlement | |
-- For more values, check the docs linked below | |
-- https://youneuoy.github.io/M2TWEOP-library/_static/LuaLib/index.html#settlementStruct | |
print(settlement.name .. " was upgraded."); | |
-- We should do this when the player upgrades the building, so he can immediately see the new model | |
if settlement.name == "West_Osgiliath" then | |
stratmap.objects.setModel(settlement.xCoord, settlement.yCoord, 3, 3); | |
end | |
end | |
-- Called whenever the campaign map is loaded | |
function onCampaignMapLoaded() | |
-- As the models are reset whenever the game is restarted, we should check if the player has already built | |
-- the building and change the model acordingly | |
-- Check if a building is already built in a given settlement | |
building, settlement = getBuildingInSettlement("fortress", "West_Osgiliath"); | |
-- If the settlement is not null (i.e if the building exists in the settlement), in this case a 'fortress' in Osgiliath | |
-- Set the model to a desert_fort that we added above in the onPluginLoad function | |
if isNotNull(building) then | |
stratmap.objects.setModel(settlement.xCoord, settlement.yCoord, 3, 3); | |
end | |
end | |
-- Called whenever the settlement is selected | |
-- We can use this event and code to find stuff out about the settlement to help us write our script | |
function onSettlementSelected(settlement) | |
print("Selected settlement " .. settlement.name); | |
-- Here we iterate or loop through all the buildings in the settlement | |
-- https://www.lua.org/pil/4.3.4.html | |
-- As we loop through, we use 'i' to access the current building index, get it's name and print it out | |
for i = 1, settlement.buildingsNum - 1 do | |
building = settlement:getBuilding(i); | |
local nameBuilding = building:getName(); | |
print(nameBuilding); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment