• Civilization 7 has been announced. For more info please check the forum here .

ChatGPT and coding

UncivilizedGuy

The Village Idiot
Joined
May 24, 2012
Messages
965
Location
Land of the Lost
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.

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.
 
I really can't see this as "an amazing tool for coding" when what it's output here is not only a rabbit warren of a mess but also just doesn't work; obviously it's impressive that it's able to retrieve the correct syntax and structure (presumably through internet querying), but this code is still so fundamentally broken that an actual person will have to edit it anyway and so by that point why wouldn't you just be asking said actual people for advice on how to go about the prompt in the first place - an experience which is better suited for learning anyway.

Regardless,
Spoiler :


local iBuildingDefine = GameInfoTypes["BUILDING_"]
local iUnitDefine = GameInfoTypes["UNIT_"]

function C15_UG_CityCanConstruct(playerID, cityID, iBuilding)
if iBuildingDefine == iBuilding then
local pPlayer = Players[playerID]
local pCity = pPlayer:GetCityByID(cityID)
local pUnit = pCity:GetGarrisonedUnit() -- This is my interpretation of the ambiguous "building that requires a specific unit"; the AI interpreted it as "the city needs to be producing such a unit" but that's obviously a mutually exclusive condition to the city being able to build the building
if pUnit then -- GetGarrisonedUnit only detects "properly" garrisoned units, i.e. those which have the unique UI effect; civilian, air, and naval units on the tile won't be found by it, and so if the unit in question is to be one of these then you would instead need to test by iterating all the units on the plot - I have a utility which can help you do this, incidently. An AI couldn't tell you that...
if pUnit:GetUnitType() == iUnitDefine then
return true
end
end

return false
end

return true -- Always return true at the end of CanConstruct logic like this, so that buildings unrelated to the logic can still be built
end

GameEvents.CityCanConstruct.Add(C15_UG_CityCanConstruct)
 
I really can't see this as "an amazing tool for coding" when what it's output here is not only a rabbit warren of a mess but also just doesn't work; obviously it's impressive that it's able to retrieve the correct syntax and structure (presumably through internet querying), but this code is still so fundamentally broken that an actual person will have to edit it anyway and so by that point why wouldn't you just be asking said actual people for advice on how to go about the prompt in the first place - an experience which is better suited for learning anyway.

Regardless,
Spoiler :


local iBuildingDefine = GameInfoTypes["BUILDING_"]
local iUnitDefine = GameInfoTypes["UNIT_"]

function C15_UG_CityCanConstruct(playerID, cityID, iBuilding)
if iBuildingDefine == iBuilding then
local pPlayer = Players[playerID]
local pCity = pPlayer:GetCityByID(cityID)
local pUnit = pCity:GetGarrisonedUnit() -- This is my interpretation of the ambiguous "building that requires a specific unit"; the AI interpreted it as "the city needs to be producing such a unit" but that's obviously a mutually exclusive condition to the city being able to build the building
if pUnit then -- GetGarrisonedUnit only detects "properly" garrisoned units, i.e. those which have the unique UI effect; civilian, air, and naval units on the tile won't be found by it, and so if the unit in question is to be one of these then you would instead need to test by iterating all the units on the plot - I have a utility which can help you do this, incidently. An AI couldn't tell you that...
if pUnit:GetUnitType() == iUnitDefine then
return true
end
end

return false
end

return true -- Always return true at the end of CanConstruct logic like this, so that buildings unrelated to the logic can still be built
end

GameEvents.CityCanConstruct.Add(C15_UG_CityCanConstruct)
I appreciate your response. I did generate some code and test it. It didn't work of course. Figured something was missing.
 
Top Bottom