Small Questions Thread

Sorry for asking you again Prof.Garfield, but I'm confused because whatever I do, the code doesn't work for me. I don't understand this because in your game it works.
I've checked the code JPetroski is using in his Cold War scenario and changed my code because I would also like to have a text message showing the player how many cities are under his control and how much money he got the last turn.

For me the following code looks correct, at least I don't see anything unlogical.
Code:
-- American cities east of the Appalachians controlled by the Brits giving the player 5 pound for each city/each turn.
local AmericanCities = {object.lHartford, object.lNewHaven, object.lSalisbury, object.lSavannah, object.lAugusta, object.lBoston,
object.lPortsmouth, object.lBiddeford, object.lBaltimore, object.lAnnapolis, object.lHagerstown, object.lConcord, object.lBennington, object.lMontpellier,
object.lTrenton, object.lNewYork, object.lKingston, object.lAlbany, object.lSaratoga, object.lNewBern, object.lRaleigh, object.lSalem, object.lCharlotte,
object.lWilmington, object.lPhiladelphia, object.lHarrisburg, object.lWilkesBarre, object.lNewport, object.lBeaufort, object.lCharlesTown, object.lCamden,
object.lCowpens, object.lFredericksburg, object.lCharlottesville, object.lRoanoke, object.lRichmond, object.lYorktown, object.lNorfolk}

local function giveMoneyForOwnedCities()
    local AmericanCities = 0
    for __,AmericanCity in pairs (AmericanCities) do
        if AmericanCity.owner == object.pBritishEmpire then
            AmericanCities = AmericanCities+1
        end
    end
    return AmericanCities
end
    
local function BritishTaxColonies()
    local AmericanCities = giveMoneyForOwnedCities()
    local AmericanCityBonusPerCity = 1000
    local BritishBonus = math.abs (AmericanCities * AmericanCityBonusPerCity)

if turn >=1 and turn <= 100 and object.pBritishEmpire.isHuman == true then
    object.pBritishEmpire.money = object.pBritishEmpire.money + BritishBonus
    local multiLineText = "As we are controlling " ..tostring(AmericanCities).." former rebellious cities in the colonies we have received a bonus of " ..tostring(BritishBonus).." pound this turn"
        text.addMultiLineTextToDialog(multiLineText,dialog)   
        dialog:show()
end   
end

Could it doesn't work because the code is located in the file 'onTurn.lua'?
Btw, I've made an update on GitHub, you should see the latest files there. In 'onTurn.lua' you will find the above code.
 
As far as I can tell, you haven't called the functions BritishTaxColonies() or giveMoneyForOwnedCities() inside an event function, like onTurn.onTurn.

I've pushed a fix to GitHub, along with some fixes to the functions you defined (e.g. you used the variable name AmericanCities both for the city count as well as the list of city locations).

Let me know if you need more help.
 
Many thanks Prof.Garfield, the code works now.

Am I right that I always have to register or call functions like BritishTaxColonies() within an event function like onTurn too otherwise lua doesn't know that it has to execute the code?
It makes sense to me.
 
Last edited:
Am I right that I always have to register or call functions like BritishTaxColonies() within an event function like onTurn too otherwise lua doesn't know that it has to execute the code?
That's right.
 
Hello @Prof. Garfield,
in registerCombatModifiers.lua I can give certain units an attack bonus if a specific technology is reseached. Would it be possible to double the effect if a second technology is researched?
For example the technologies 'Musketeers' and Musketeers 2' should both giving each an attack bonus of 2. If both technologies are researched will lua give an attack bonus of 4 or only of 2?
 
Hello @Prof. Garfield,
in registerCombatModifiers.lua I can give certain units an attack bonus if a specific technology is reseached. Would it be possible to double the effect if a second technology is researched?
For example the technologies 'Musketeers' and Musketeers 2' should both giving each an attack bonus of 2. If both technologies are researched will lua give an attack bonus of 4 or only of 2?
You can register 2 combat modifiers, one for Musketeers and another for Musketeers 2. Another option is to register a function(attacker,defender)-> nil|number for the corresponding bonus value, and then have that function compute the appropriate bonus. Let me know if you need an example.
 
Back
Top Bottom