UncivilizedGuy
The Village Idiot
One of my biggest handicaps in coding is Lua and I have even considered taking a class to improve my skills. But now we have ChatGPT. I have read article after article talking about what an amazing tool this is for coding. I haven't seen any conversations here about this so I decided to start one.
Below is an example of what ChatGPT created for me. I haven't tested this. My hope is that someone with Lua skills can look at this and tell me if this will work. I do plan on testing this soon. What I find amazing about this is how the response goes above and beyond what I asked it. Just that fact that it was trained on coding specific to Civilization V fascinates me too. If this works then modding will be all too easy and allows an amateur like me create next level work.
Prompt: Create lua code for a building that requires a specific unit in Civilization V.
Here is an example of Lua code for a building that requires a specific unit in Civilization V:
-- Define the building object
local building = {
ID = "BUILDING_MY_BUILDING", -- unique identifier for the building
Type = "BUILDINGCLASS_MY_BUILDING", -- type of building class
RequiredUnit = "UNIT_MY_UNIT", -- required unit to construct the building
Cost = 100, -- cost to construct the building
Prerequisite = nil, -- prerequisite technology to unlock the building
Yield = GameInfo.Yields["YIELD_GOLD"].ID, -- yield provided by the building
}
-- Define the building class object
local buildingClass = GameInfo.BuildingClasses["BUILDINGCLASS_MY_BUILDING"]
buildingClass.MaxGlobalInstances = 1 -- limit the number of global instances of the building
buildingClass.MaxPlayerInstances = 1 -- limit the number of player instances of the building
-- Define the required unit object
local requiredUnit = GameInfo.Units["UNIT_MY_UNIT"]
requiredUnit.PrereqTech = "TECH_MY_TECH" -- set the prerequisite technology for the unit
-- Register the building, building class, and required unit objects
GameEvents.CityCanConstruct.Add(function(playerID, cityID, buildingTypeID)
local city = Players[playerID]:GetCityByID(cityID)
if buildingTypeID == GameInfoTypes[building.ID] then
if not city:IsHasBuilding(GameInfoTypes[building.ID]) then
local unit = city:GetProductionUnit()
if unit and unit:GetUnitType() == GameInfoTypes[building.RequiredUnit] then
return true -- allow construction of the building if the required unit is being produced
end
end
return false -- prevent construction of the building if the required unit is not being produced
end
return true -- allow construction of all other buildings
end)
-- Initialize the required unit's prerequisite technology
GameEvents.UnitCreated.Add(function(playerID, unitID, unitType)
if unitType == GameInfoTypes[requiredUnit.Type] then
local player = Players[playerID]
if not player:HasTech(GameInfoTypes[requiredUnit.PrereqTech]) then
player:SetResearchProgress(GameInfoTypes[requiredUnit.PrereqTech], 1)
end
end
end)
In this code, we define a building object with a unique identifier, building class, required unit, cost, prerequisite technology, and yield. We also define a building class object with a limit on the number of global and player instances. We define a required unit object with a prerequisite technology. We register two event listeners - one to prevent construction of the building if the required unit is not being produced, and one to initialize the prerequisite technology for the required unit.
Below is an example of what ChatGPT created for me. I haven't tested this. My hope is that someone with Lua skills can look at this and tell me if this will work. I do plan on testing this soon. What I find amazing about this is how the response goes above and beyond what I asked it. Just that fact that it was trained on coding specific to Civilization V fascinates me too. If this works then modding will be all too easy and allows an amateur like me create next level work.
Prompt: Create lua code for a building that requires a specific unit in Civilization V.
Spoiler ChatGPT example :
Here is an example of Lua code for a building that requires a specific unit in Civilization V:
-- Define the building object
local building = {
ID = "BUILDING_MY_BUILDING", -- unique identifier for the building
Type = "BUILDINGCLASS_MY_BUILDING", -- type of building class
RequiredUnit = "UNIT_MY_UNIT", -- required unit to construct the building
Cost = 100, -- cost to construct the building
Prerequisite = nil, -- prerequisite technology to unlock the building
Yield = GameInfo.Yields["YIELD_GOLD"].ID, -- yield provided by the building
}
-- Define the building class object
local buildingClass = GameInfo.BuildingClasses["BUILDINGCLASS_MY_BUILDING"]
buildingClass.MaxGlobalInstances = 1 -- limit the number of global instances of the building
buildingClass.MaxPlayerInstances = 1 -- limit the number of player instances of the building
-- Define the required unit object
local requiredUnit = GameInfo.Units["UNIT_MY_UNIT"]
requiredUnit.PrereqTech = "TECH_MY_TECH" -- set the prerequisite technology for the unit
-- Register the building, building class, and required unit objects
GameEvents.CityCanConstruct.Add(function(playerID, cityID, buildingTypeID)
local city = Players[playerID]:GetCityByID(cityID)
if buildingTypeID == GameInfoTypes[building.ID] then
if not city:IsHasBuilding(GameInfoTypes[building.ID]) then
local unit = city:GetProductionUnit()
if unit and unit:GetUnitType() == GameInfoTypes[building.RequiredUnit] then
return true -- allow construction of the building if the required unit is being produced
end
end
return false -- prevent construction of the building if the required unit is not being produced
end
return true -- allow construction of all other buildings
end)
-- Initialize the required unit's prerequisite technology
GameEvents.UnitCreated.Add(function(playerID, unitID, unitType)
if unitType == GameInfoTypes[requiredUnit.Type] then
local player = Players[playerID]
if not player:HasTech(GameInfoTypes[requiredUnit.PrereqTech]) then
player:SetResearchProgress(GameInfoTypes[requiredUnit.PrereqTech], 1)
end
end
end)
In this code, we define a building object with a unique identifier, building class, required unit, cost, prerequisite technology, and yield. We also define a building class object with a limit on the number of global and player instances. We define a required unit object with a prerequisite technology. We register two event listeners - one to prevent construction of the building if the required unit is not being produced, and one to initialize the prerequisite technology for the required unit.