Results 1 to 3 of 3

Thread: How to make a Scripting Treasury Mod AI only in factions LUA?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default How to make a Scripting Treasury Mod AI only in factions LUA?

    Completed several searches using appropriate keywords and came up with nothing.

    So, how does one make the cm:treasury_mod function with an AI only condition without crashing the game or messing up other functions in the faction's LUA script now?

    Currently working with this and it crashes on campaign start:
    Spoiler Alert, click show to read: 
    function faction_new_sp_game_startup()
    output("faction_new_sp_game_startup() called");

    if not conditions.FactionIsHuman("att_fact_western_roman_empire", context) then
    cm:treasury_mod("att_fact_western_roman_empire", 170000)
    end;
    -- put stuff here to be initialised on a new singleplayer game
    end;

    If I remove "end;" for my new "IF" it does not crash but messes up the opening camera pan and advisor speech when starting that faction.

    If I remove the condition the mod works, naturally, but for players too, which is not the intended effect.

    Under rome 2 it worked seamlessly in the general .lua script with this syntax:
    Spoiler Alert, click show to read: 
    local function OnFactionTurnStart(context)

    if conditions.FactionName("att_fact_western_roman_empire", context) and not conditions.FactionIsHuman("att_fact_western_roman_empire", context) then
    scripting.game_interface:treasury_mod("att_fact_western_roman_empire", 170000)
    end

    end

    I used my previous change above as a base to work from, I removed the condition to check for which faction as I assume that is redundant with the new lua scripts for each playable faction in Attila and switched the scripting.game_interface to the new syntax it prefers.

    So what am I missing, other syntax changes that I am unaware of that the updated engine prefers in these lua files?
    Last edited by Shaxx; September 02, 2015 at 04:52 AM.

  2. #2
    Inevitability won
    Patrician Citizen

    Join Date
    Mar 2010
    Posts
    9,594

    Default Re: How to make a Scripting Treasury Mod AI only in factions LUA?

    Hi,

    You're essentially doing everything correctly, all you've done wrong is just missed one of the nuances of the scripting used in the game, which is that many of the functions have the context parameter.

    So when you used the treasury function in Rome II you used it inside another function ("OnFactionTurnStart") that had the context argument. The "conditions.FactionIsHuman" function needs the context argument passed to it for it to work, hence why when you remove the if statement you see your script as working. Likewise when you remove the "end" for this if statement the entire file has a syntax error which the game detects and hence the script isn't run, so you get a messed up camera as the intro scripts don't run.

    i.e. This line gives you the context argument from you're script in Rome II:
    Code:
    local function OnFactionTurnStart(context)
    Now in ATTILA you're using the treasury function, but this time you're using it inside of a different function ("faction_new_sp_game_startup") that has no context argument. Thus when you're if statements function that checks if that faction is human tries to execute it still uses the argument context which you passed to it, however as context is not recieved from the encapsulating function it is nil, hence you crash the game as the function call breaks.

    What I would suggest you do is this:
    (I've commented some lines in to explain whats going on)
    Code:
    function faction_new_sp_game_startup()
    output("faction_new_sp_game_startup() called");
    
    -- Here we get the faction object of the key we pass to this function, store it in the 'roman_faction' variable.
    local roman_faction = cm:model():world():faction_by_key("att_fact_western_roman_empire");
    
    -- Now we access the faction object 'roman_faction' and check if it is a human player, if not run the treasury function.
    if roman_faction:is_human() == false then
    cm:treasury_mod("att_fact_western_roman_empire", 170000)
    end;
    
    -- put stuff here to be initialised on a new singleplayer game
    end;
    Hopefuly that should work, and you can see the issue you were having.

    ~ Mitch
    Last edited by .Mitch.; September 02, 2015 at 01:01 PM.

  3. #3

    Default Re: How to make a Scripting Treasury Mod AI only in factions LUA?

    Quote Originally Posted by .Mitch. View Post
    Hi,

    You're essentially doing everything correctly, all you've done wrong is just missed one of the nuances of the scripting used in the game, which is that many of the functions have the context parameter.

    So when you used the treasury function in Rome II you used it inside another function ("OnFactionTurnStart") that had the context argument. The "conditions.FactionIsHuman" function needs the context argument passed to it for it to work, hence why when you remove the if statement you see your script as working. Likewise when you remove the "end" for this if statement the entire file has a syntax error which the game detects and hence the script isn't run, so you get a messed up camera as the intro scripts don't run.

    i.e. This line gives you the context argument from you're script in Rome II:
    Code:
    local function OnFactionTurnStart(context)
    Now in ATTILA you're using the treasury function, but this time you're using it inside of a different function ("faction_new_sp_game_startup") that has no context argument. Thus when you're if statements function that checks if that faction is human tries to execute it still uses the argument context which you passed to it, however as context is not recieved from the encapsulating function it is nil, hence you crash the game as the function call breaks.

    What I would suggest you do is this:
    (I've commented some lines in to explain whats going on)
    Code:
    function faction_new_sp_game_startup()
    output("faction_new_sp_game_startup() called");
    
    -- Here we get the faction object of the key we pass to this function, store it in the 'roman_faction' variable.
    local roman_faction = cm:model():world():faction_by_key("att_fact_western_roman_empire");
    
    -- Now we access the faction object 'roman_faction' and check if it is a human player, if not run the treasury function.
    if roman_faction:is_human() == false then
    cm:treasury_mod("att_fact_western_roman_empire", 170000)
    end;
    
    -- put stuff here to be initialised on a new singleplayer game
    end;
    Hopefuly that should work, and you can see the issue you were having.

    ~ Mitch
    Worked like a charm, thanks.

    Handing out initial walking around money to key AI nations that tend to collapse too soon or not do well enough despite their historical success.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •