1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

Can Lua be written differently?

Discussion in 'Civ2 - Scenario League' started by Konig15, Mar 7, 2020.

Tags:
  1. Konig15

    Konig15 Warlord

    Joined:
    Nov 4, 2007
    Messages:
    152
    I've not been wanting to use lua since I came back to Civ 2 modding, I feel dizzy every time I look at it. Cause to take the Napoleon scenario, a lua event looks like this:


    -- Napoléon administrative bonus
    if activeUnit.type.name == "Napoléon I" then
    local ParisLocation = civ.getTile(58,60,0)
    if activeUnit.location == ParisLocation then
    local dialog = civ.ui.createDialog()
    dialog.title = "Select Administrative Bonus"
    dialog.width = 535
    dialog:addText("Please select the Napoléon administrative bonus you prefer:")
    dialog:addOption("Add to the Treasury and increase scientific research", 1)
    local unitCost = 500
    if France.money >= unitCost then
    dialog:addOption("Build two Régiment de Ligne units at a cost of " .. unitCost .. " Francs", 2)
    else
    civ.ui.text("If the treasury contains at least " .. unitCost .. " Francs, Napoléon will have an additional administrative bonus option of building two Régiment de Ligne units.")
    end
    dialog:addOption("Exit (do not utilize the administrative bonus)", 0)
    local result = dialog:show()
    if result == 1 then
    local goldAmount = 200
    local scienceAmount = 100
    if state.napoleonMarriageMarieLouise == true then
    goldAmount = 150
    scienceAmount = 50
    end
    civ.ui.text(func.splitlines("Napoléon administrative bonus:\n^\r^" .. goldAmount .. " Francs, " .. scienceAmount .. " research beakers"))
    changeMoney(France, goldAmount)
    France.researchProgress = France.researchProgress + scienceAmount
    local administrativeUnit = findUnitTypeByName("Napoléon I");
    activeUnit.moveSpent = administrativeUnit.move
    elseif result == 2 then
    local unitTypeName = "Régiment de Ligne"
    civ.ui.text(func.splitlines("Napoléon administrative bonus:\n^\r^2 " .. unitTypeName .. " units are recruited at a cost of " .. unitCost .. " Francs"))
    changeMoney(France, unitCost * -1)
    createUnitsByName("Régiment de Ligne", France, {{58,60,0}}, {count = 2, homeCity = NONE, veteran = true})
    local administrativeUnit = findUnitTypeByName("Napoléon I");
    activeUnit.moveSpent = administrativeUnit.move
    end
    else
    civ.ui.text("In order for Napoléon to provide an administrative bonus, he must be located in Paris!")
    end
    end


    It's actually worse in the file. For me to get anywhere near this, I'd need AT LEAST the events to look like this:

    +++++
    Napoléon administrative bonus
    if activeUnit.type.name == "Napoléon I" then
    local ParisLocation = civ.getTile(58,60,0)
    if activeUnit.location == ParisLocation then
    local dialog = civ.ui.createDialog()
    dialog.title = "Select Administrative Bonus"
    dialog.width = 535
    dialog:addText("Please select the Napoléon administrative bonus you prefer:")
    dialog:addOption("Add to the Treasury and increase scientific research", 1)
    local unitCost = 500
    if France.money >= unitCost then
    dialog:addOption("Build two Régiment de Ligne units at a cost of " .. unitCost .. " Francs", 2)
    else
    civ.ui.text("If the treasury contains at least " .. unitCost .. " Francs, Napoléon will have an additional administrative bonus option of building two Régiment de Ligne units.")
    end
    dialog:addOption("Exit (do not utilize the administrative bonus)", 0)
    local result = dialog:show()
    if result == 1 then
    local goldAmount = 200
    local scienceAmount = 100
    if state.napoleonMarriageMarieLouise == true then
    goldAmount = 150
    scienceAmount = 50
    end
    civ.ui.text(func.splitlines("Napoléon administrative bonus:\n^\r^" .. goldAmount .. " Francs, " .. scienceAmount .. " research beakers"))
    changeMoney(France, goldAmount)
    France.researchProgress = France.researchProgress + scienceAmount
    local administrativeUnit = findUnitTypeByName("Napoléon I");
    activeUnit.moveSpent = administrativeUnit.move
    elseif result == 2 then
    local unitTypeName = "Régiment de Ligne"
    civ.ui.text(func.splitlines("Napoléon administrative bonus:\n^\r^2 " .. unitTypeName .. " units are recruited at a cost of " .. unitCost .. " Francs"))
    changeMoney(France, unitCost * -1)
    createUnitsByName("Régiment de Ligne", France, {{58,60,0}}, {count = 2, homeCity = NONE, veteran = true})
    local administrativeUnit = findUnitTypeByName("Napoléon I");
    activeUnit.moveSpent = administrativeUnit.move
    end
    else
    civ.ui.text("In order for Napoléon to provide an administrative bonus, he must be located in Paris!")
    end
    end

    And preferably like this:

    Napoléon administrative bonus
    if activeUnit.type.name == "Napoléon I" then
    local ParisLocation = civ.getTile(58,60,0)
    if activeUnit.location == ParisLocation then

    local dialog = civ.ui.createDialog()
    dialog.title = "Select Administrative Bonus"
    dialog.width = 535
    dialog:addText("Please select the Napoléon administrative bonus you prefer:")
    dialog:addOption("Add to the Treasury and increase scientific research", 1)

    local unitCost = 500
    if France.money >= unitCost then
    dialog:addOption("Build two Régiment de Ligne units at a cost of " .. unitCost .. " Francs", 2)
    else
    civ.ui.text("If the treasury contains at least " .. unitCost .. " Francs, Napoléon will have an additional administrative bonus option of building two Régiment de Ligne units.")
    end

    dialog:addOption("Exit (do not utilize the administrative bonus)", 0)
    local result = dialog:show()

    ****
    if result == 1 then
    local goldAmount = 200
    local scienceAmount = 100
    if state.napoleonMarriageMarieLouise == true then
    goldAmount = 150
    scienceAmount = 50
    end

    civ.ui.text(func.splitlines("Napoléon administrative bonus:\n^\r^" .. goldAmount .. " Francs, " .. scienceAmount .. " research beakers"))
    changeMoney(France, goldAmount)
    France.researchProgress = France.researchProgress + scienceAmount
    local administrativeUnit = findUnitTypeByName("Napoléon I");
    activeUnit.moveSpent = administrativeUnit.move

    ****
    elseif result == 2 then
    local unitTypeName = "Régiment de Ligne"
    civ.ui.text(func.splitlines("Napoléon administrative bonus:\n^\r^2 " .. unitTypeName .. " units are recruited at a cost of " .. unitCost .. " Francs"))
    changeMoney(France, unitCost * -1)
    createUnitsByName("Régiment de Ligne", France, {{58,60,0}}, {count = 2, homeCity = NONE, veteran = true})
    local administrativeUnit = findUnitTypeByName("Napoléon I");
    activeUnit.moveSpent = administrativeUnit.move
    end

    else
    civ.ui.text("In order for Napoléon to provide an administrative bonus, he must be located in Paris!")
    end

    end
    +++++++++

    This way everything's pinned to the left side like old script and doesn't force my eyes to dance around the screen which overwhelms me and makes me sick. with the **** and the ++++ functioning as paragraphs and chapter creaks respectively, it makes the information digestible.

    Can I do something like this? The third way, if it can work is WAY less nausea-inducing then the first.
     
  2. Konig15

    Konig15 Warlord

    Joined:
    Nov 4, 2007
    Messages:
    152
  3. Knighttime

    Knighttime Warlord

    Joined:
    Sep 20, 2002
    Messages:
    148
    Prof. Garfield is certainly welcome to answer, but as the lead programmer for the Napoleon events you cited, I'd also like to respond.

    First of all, forum posts often strip or ignore whitespace when you don't intend it. If you want to post blocks of code, I recommend using the [ CODE ] and [ /CODE ] tags (without any spaces inside the brackets) which will preserve formatting of everything between the tags, exactly as you enter it. I think because you posted everything as regular text, some of the differences you intended to illustrate were lost.

    I believe nearly all professional programmers would agree that indentation, done well (not inconsistently or haphazardly), is very useful and makes code easier to read and comprehend. Many IDEs (integrated development environments -- advanced editors used specifically to facilitate writing software) will automatically add indentation as you type. In fact, many/most software development managers would reject code from a team member that was written entirely left-justified, and require it to be reformatted.

    Blank lines might be a bit more subjective... some programmers probably have much stronger feelings about this than others. I will admit I'm not 100% consistent about when I use them.

    That being said, Lua is not a "whitespace sensitive" language. You can add or remove indentation, as well as line breaks and blank lines, with no effect at all on the way the program runs. So if you really prefer to have all the code left-justified, and add extra blank lines between sections, you certainly can do that. You're not working on a software team and you have the freedom to set your own personal standards and use your own style. (But I do think in the long run, you would benefit from learning to appreciate indentation instead of rejecting it.)

    To add a “marker” like **** or ++++, or any text that isn’t code, you have to make it a comment or else it will cause a syntax error and prevent the script from running at all. There are two types of comments in Lua: block comments start with --[[ (hyphen hyphen left-bracket left-bracket) and end with ]] (right-bracket right-bracket). Anything in between those symbols is a comment and ignored when the program runs. This can be a portion of one line, with code continuing later on that line, or the comment can span multiple lines. On the other hand, single line comments start with -- (hyphen hyphen) and always continue to the end of that line. So you can't just insert **** on a line by itself, but --**** or --++++ would work fine.
     
    Last edited: Mar 7, 2020
  4. Prof. Garfield

    Prof. Garfield Deity Supporter

    Joined:
    Mar 6, 2004
    Messages:
    2,637
    Location:
    Ontario
    What text editor are you using to look at the code? You will almost certainly be able to change how many spaces a tab takes up. If you are having trouble following code when tabs are being rendered as 8 spaces, you might find it easier when they are rendered as 2 or 4 spaces instead.

    I can't think of much else to say on the matter that Knighttime hasn't already covered. You don't have to indent, but I would also recommend that you do. You can't get rid of the 'end' words, but if you really want to, you could put them at the end of a line, instead of on their own lines. However, you start to find that confusing when your blocks of code get larger.
     
  5. CurtSibling

    CurtSibling ENEMY ACE™ SLeague Staff Supporter

    Joined:
    Aug 31, 2001
    Messages:
    28,761
    Gender:
    Male
    Location:
    Innsmouth
    I would amiably ask @Konig15 not to come up with pointless problems and distracting busy creators with spurious "bugs".

    Good sir, if you want the forum's attention, make a scenario. We've had this discussion already.
     
  6. Konig15

    Konig15 Warlord

    Joined:
    Nov 4, 2007
    Messages:
    152
    Good sir, I'm not sure what your objection is here. I'm not reporting a bug, I'm asking if something can be done in lau. I AM using an event from Napoleon, not to modify that scenario or complain about it, but to demonstrate how lua could be more manageable for me to dabble with.

    I AM working on a scenario, but in terms of knowledge, I'm way WAY behind the curve. I'm trying to figure out how all the systems play.

    I'm sorry if I offended, but this isn't remotely about bugs, and for me personally, the spacing issue is a HUGE problem.
     
  7. Knighttime

    Knighttime Warlord

    Joined:
    Sep 20, 2002
    Messages:
    148
    @Konig15 Personally, I didn’t really have a problem with your question. I probably could have given a much shorter answer — “Yes, what you want to do is possible, as long as you use comments correctly” — but I chose to go into a little more detail, to help you understand why Napoleon events were done as they were, and to try to share information that will help you succeed with Lua.

    As a friendly reminder, though, there are many resources online about all aspects of the Lua language: syntax, rules, best practices, etc. Many of the questions you will have, as you begin experimenting with Lua, can probably be answered with a Google search and some reading. So perhaps Curt is emphasizing that this forum is best used for Lua questions that are related to using it in TOT scenarios, rather than for questions about the language (or about programming) in general — at least not as a first stop.
     
    Last edited: Mar 8, 2020
  8. Konig15

    Konig15 Warlord

    Joined:
    Nov 4, 2007
    Messages:
    152

    I had no idea! I thought this was in house, based on something but the peccadillos were all built around the quirks of the Civ 2 engine. With that said, yes yes I will seek out general tutorials then.

    Thank you!
     
  9. CurtSibling

    CurtSibling ENEMY ACE™ SLeague Staff Supporter

    Joined:
    Aug 31, 2001
    Messages:
    28,761
    Gender:
    Male
    Location:
    Innsmouth
    I'll be blunt, @Konig15 ,
    You have a history of pestering forum members over total nonsense.
    I was merely heading off what looked like another of those situations.

    Good luck with the scenario, also. :)
     
  10. tootall_2012

    tootall_2012 Prince

    Joined:
    Feb 11, 2012
    Messages:
    561
    Hi Konig,

    Since you reached out for advice and I have some experience in being newly converted to the usage of the lua language I thought I would chime in a little.

    Firstly, TNO’s Test of Time Patch Project and the library of functions (found here) he created for supporting lua can be considered in house projects. Lua itself is an actual programming language which is used in many different applications including I believe for gaming platforms.

    With regards the use of indentations it is considered by many professionals to be part of the best practices because it makes reading, understanding and debugging code much easier. As Knighttime indicated, there is a great deal of literature on the subject on the web. Of course, you aren’t required to use indentations, but as somewhat still a novice myself, I can confirm that not using them will make your introduction to the language much much harder for the same reasons described above.

    As Prof. Garfield asked what text editor are you using? If you simply use NotePad then I would recommend you use NotePad ++ which is a free software which can be downloaded from here.

    From within this application you can go to the Language tab sub-menu and select LUA. This will have the great advantage of highlighting in color the language’s key programming terms.

    In the attached images I have two examples of the same simple bit of code, one that isn’t color coded or indented (code1.png) and one that uses NotePad++ with indentation (code2.png). As you can tell in the second example the structure of the code is much clearer and this is a very simple example. Imagine a situation where you have multiple if/else statements within the same bloc of code that runs 30, 50 or 100 lines and you will quickly come to appreciate the value of proper indentation.

    Now imagine that same bloc of code within a lua file that has thousands of lines of code with hundreds of if/end statements and you will see you can very quickly and easily become lost without it.

    I hope this helped. Good luck.
     

    Attached Files:

    Last edited: Mar 9, 2020
  11. Prof. Garfield

    Prof. Garfield Deity Supporter

    Joined:
    Mar 6, 2004
    Messages:
    2,637
    Location:
    Ontario
    @Konig15 I also think your question was perfectly legitimate.

    There are a few good websites offering documentation/tutorials on the different components and functions of Lua. However, they do assume a bit of competence with Lua or at least programming in general. Before writing my Lua Lessons, I tried to find a 'introduction to programming' resource that used Lua, but I didn't find one, which is part of the reason I wrote the lessons.

    However, I subsequently found a Lua programming environment ZeroBraneStudio which does have a basic tutorial mode. You can't test civ related stuff, of course, but it is a place to start. You (or others) might find it useful.
     
    Konig15 likes this.

Share This Page