[TOTPP] Lua Scenario Template

Hello @Prof. Garfield, it's me again.
I'm currently using the following code which includes a choice option:
Code:
if  turn ==20 and object.pOttomanEmpire.isHuman == true then
    gen.justOnce("Celali Rebellions", function()
    local dialog = civ.ui.createDialog()
        dialog.title = "Celali Rebellions"
        dialog.width = 700
    local multiLineText = "A series of rebellions ..."
        text.addMultiLineTextToDialog(multiLineText,dialog)
    if object.pOttomanEmpire.money >= 700 then   
        dialog:addOption("Invest money (costs 700 ducats).", 1)
        dialog:addOption("Crush down the rebellions....", 1)
    local choice = dialog:show()
    if choice == 1 then
        object.pOttomanEmpire.money = object.pOttomanEmpire.money - 700
    end   
    elseif choice == 2 then
        flag.setTrue("CelaliRebellions")
    end   
    end)   
end
I never had problems when I used the variable
Code:
local choice == dialog:show()

But this time Lua shows me the following error message, the variable 'choice' doesn't exist:
upload_2022-7-2_19-4-27.png


What do I have to do for making this code running again?
 
Code:
dialog:addOption("Invest money (costs 700 ducats).", 1) 
dialog:addOption("Crush down the rebellions....", 1)
Both these options are given the value '1'. That might be causing your issue.
 
Oh, that's it. It should be
Code:
local choice = dialog:show()
instead. You use = to assign, which is what we want to do here, and == to check if 2 things are the same.

I didn't notice that earlier.
 
I've changed the code like you sugested but it doesn't work. Everytime I get the same error message:
'The variable name 'choice' doesn't match any available local variables.'

I'm a little bit confused because I thought with using 'local choice' variable it should work.
 
I found the solution of my problem which I caused by myself :hammer2::hammer2:. I just set one 'end' on the wrong place. Since I'm using the code as follows it works now.
Code:
if  turn ==20 and object.pOttomanEmpire.isHuman == true then
    gen.justOnce("Celali Rebellions", function()
    local dialog = civ.ui.createDialog()
        dialog.title = "Celali Rebellions"
        dialog.width = 700
    local multiLineText = "A series of rebellions..."
        text.addMultiLineTextToDialog(multiLineText,dialog)
    if object.pOttomanEmpire.money >= 700 then   
        dialog:addOption("Invest money...", 1)
    end   
        dialog:addOption("Crush down the ...", 2)
    local choice = dialog:show()
    if choice == 1 then
        object.pOttomanEmpire.money = object.pOttomanEmpire.money - 700
    elseif choice == 2 then
        flag.setTrue("CelaliRebellions")
    end
    end)   
end
 
if Lua shows me the following error message does it mean that I have too many units at a certain square if the event is fired?
Line 926 in concolidated is the 'civlua.createUnit(...' code
Check the locations table that you provided, since that is where the error is in the civlua.lua file. Remember that even if you have only one location, it must be of the form {{x,y,z}}. (If you have multiple locations, it is {{x,y,z},{x,y,z}}.) You can also try gen.createUnit, which is a bit more flexible about the arguments it can take.
 
Hello @Prof. Garfield,
would it be possible to fire an event if a unit is destroyed on a certain terrain tile?
For example, I would like to fire an event if the player destroys the Fortress unit on tile 184, 110.
1659033611144.png


What code do I have to use, if something like this can be realized with lua?
 
would it be possible to fire an event if a unit is destroyed on a certain terrain tile?
For example, I would like to fire an event if the player destroys the Fortress unit on tile 184, 110.
This is very easy to do.

First, make sure you've defined the relevant location in the object file (and the unit type)
Code:
object.lSpecialFortressTile = civ.getTile(184,110,0)

Then in your unit killed event, just test if the loser is a fortress, and if the loser's location is at the special tile. (untested code)

Code:
function discreteEvents.onUnitKilled(loser,winner,aggressor,victim,loserLocation,winnerVetStatus,loserVetStatus) 
    if loser.type == object.uFortress and loserLocation == object.lSpecialFortressTile then
        text.simple("An island Fortress was destroyed")
    end
end
 
I've updated the template. Aircraft carriers should now work pretty much like other ships (but you must use navySettings.lua to set holds, etc). I also made a fix for the promotion error and make @Knighttime 's suggested modification to helpkey.

@civ2units , @tootall_2012 (and anyone else using the most recent version of the template), I've added a fix to events.lua.

Technical details about events.lua fix.
To make the 'onEnterTile' execution point, I used the activateUnit trigger, and saved some information about the last unit to be activated (to see if it moved). This works fine, except for the last unit to move in a turn (since there is no subsequent unit activated). The endOfTurn event works for the AI, but a human player might do stuff after the last unit moved. I originally tried to leverage the unreleased 'civ.scen.onDrawTile' event for this, but that caused some errors. I've now leveraged the getFormattedDate execution point, since that runs every time the mouse is clicked (since changing a tile updates the status window, and viewing a city also gets the date). This appears to work.
 

Attachments

  • events.lua.zip
    10 KB · Views: 13
@Prof. Garfield, I used your latest events.lua file but unfortunately the lua console showed me the following long list of errors:
1660492147451.jpeg

1660492154250.jpeg


I have to say that I'm currently using not the latest version of your lua template. Maybe this is why the error appears.
I'm now using the former events.lua again file which works without problems.
 
I have to say that I'm currently using not the latest version of your lua template. Maybe this is why the error appears.
I'm now using the former events.lua again file which works without problems.
That error should be fixed by updating the discreteEventsRegistrar.lua file (in the LuaCore). It shouldn't really matter to use the events.lua that you already have. The only change is that if the very last unit available to a player triggers the "region event" I wrote for you on its very last movement point, the penalty won't be triggered until the player's turn ends, rather than when the player clicks a mouse button.
 

Attachments

  • discreteEventsRegistrar.lua.zip
    2.3 KB · Views: 13
Many thanks for the updated file. Now everything works fine again.
I would like to use the mercenary purchase menu you wrote for my first scenario version, but unfortunately I don't know how to define the key '3' in the current lua template.
In the old template there was a file called 'keyPressEvents.lua' but this file doesn't exist anymore in the new version.
 
Many thanks for the updated file. Now everything works fine again.
I would like to use the mercenary purchase menu you wrote for my first scenario version, but unfortunately I don't know how to define the key '3' in the current lua template.
In the old template there was a file called 'keyPressEvents.lua' but this file doesn't exist anymore in the new version.
There is keyPressSettings.lua in the MechanicsFiles folder. onKeyPress is also in the consolidatedEvents.lua, and you can make a discrete event in any required folder (keyPressSettings.lua has examples of this).
 
I'm adding functions that control the visibility of units on the map (unit.visibility) to the General Library. My plan is to let the functions accept both unitObjects and tileObjects as arguments, since I'm guessing that there are use cases for both revealing all the units on a tile, and revealing a particular unit. (From some brief tests, either all units on the tile are revealed or none are, and it looks like the result of tile.units() gives the unit that determines visibility. So, the entire tile of units must be revealed.)

Revealing or hiding units makes sense. However, the problem comes with checking if the units on a tile are revealed. I want to have a function
Code:
gen.isUnitVisible(unitOrTile,tribe) --> boolean
however, it is not clear whether true or false should be returned if there are no units on the tile. Does anyone have any thoughts?
 
There is keyPressSettings.lua in the MechanicsFiles folder. onKeyPress is also in the consolidatedEvents.lua, and you can make a discrete event in any required folder (keyPressSettings.lua has examples of this).

Thanks @Prof. Garfield .
I'm using keyPressSettings.lua for setting the key. It works fine. The two examples in the file made it more easier for me to set the key.
 
Top Bottom