How do I make my mod work on an existing save?

The place to discuss scripting and game modifications for X4: Foundations.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

user1679
Posts: 1088
Joined: Fri, 20. Jul 18, 23:20

How do I make my mod work on an existing save?

Post by user1679 »

This is actually a two-part question: How do I make my mod work on an existing game and when I update my mod, allow users to update without starting a new game?

For the first question, this is what happens right now:

1. Start game (without my mod)
2. Save Game
3. Exit
4. Install mod
5. Load game (mod is loaded, I see my show_help message but the mod doesn't works)
6. Save game
7. Load game (mod works)

In my "init" cue, I'm listening for <event_cue_signalled cue="md.Setup.Start" /> because this is triggered on new game and loaded game. The problem is, I need to call but the problem is my CUE has a subcue that listens for <event_game_loaded/> so I can call <signal_cue_instantly/> on a cue that I cannot call from my "Init" cue.

Essentially it looks like:

Code: Select all

<cue name="init">
    <conditions>
        <event_cue_signalled cue="md.Setup.Start" />
    </conditions>
    <actions>
        <set_value name="$ModName" exact="'CUE Test'" />
        <set_value name="$ModVer" exact="'1.0.0'"/>
        
        <show_help duration="15s" custom="'%s'.[$ModName] + ' v%s'.[$ModVer] + ' loaded'"/>
        
        <!-- THIS DOESN"T WORK
        
                It seems the subcue isn't instantiated yet? I need to be able to call this on a save load
                so users can add my mod to an existing save. Problem is if I move do_spawn outside
                the CUES tag, then the other cues can't call it.
        -->
        <signal_cue_instantly cue="do_spawn" />
        
    </actions>
    <cues>
        <cue name="mod_save_loaded" instantiate="true">
            <conditions>
                <event_game_loaded/>
            </conditions>
            <actions>
                <!-- Do some stuff -->
            </actions>
        </cue>
        <cue name="do_spawn" instantiate="true">
            <conditions>
                <event_cue_signaled />
            </conditions>
            <actions>
                <!-- Do some stuff -->
            </actions>
        </cue>
    </cues>
</cue>
As you can see from my comment, when the mod is installed and a save is loaded that didn't have my mod, it properly executes the INIT cue but it doesn't signal the subcue. This means the mod works for a new game but only for save games that were created with my mod already installed.

This brings me to the second question, which I believe is probably related. If I decide to change what the cue "do_spawn" does, it doesn't seem to replace the cue that was saved in the save game. I would like to be able to change my mod and upload it so users can simply replace it without breaking their save game. I checked the X:Rebirth documentation but the details on CUE behavior are minimal. Looking through the extracted X4 files, it's not clear what order CUEs get loaded or called, when to instantiate a cue and when not to.

Any advice would be appreciated.
kuertee
EGOSOFT
EGOSOFT
Posts: 818
Joined: Sun, 14. Dec 03, 13:05
x4

Re: How do I make my mod work on an existing save?

Post by kuertee »

add a first run cue that signals "mod_save_loaded"

e.g. firstly, you'll need to add the "event_cue_signalled" to "mod_save_loaded" conditions:

Code: Select all

        <cue name="mod_save_loaded" instantiate="true">
            <conditions>
                <check_any>
                    <event_game_loaded/>
                    <event_cue_signalled />
                </check_any>
            </conditions>
       </cue>
2ndly, add this cue after "mod_save_loaded". it will only activate once when the parent cue, "init" is activated:

Code: Select all

<cue name="first_run">
    <actions>
        <signal_cue cue="mod_save_loaded" />
    </actions>
</cue>
i also suggest that you add "event_game_loaded" to the "init" cue. this will allow the mod to work from an existing save.

with your set-up, loading a game that doesn't have the mod installed yet, "mod_save_loaded" will not run on "event_game_loaded"
1. because it's not yet enabled ...
2. because it's parent cue, "init" is not yet enabled ...
3. because it is only enabled when "md.Setup.Start" is signalled - which it missed because the mod was installed mid-game.

because "init" is already "active", and has stopped listening for its conditions, it won't run again at the next time the game is loaded.
only the "event_game_loaded" event will trigger "mod_save_loaded" because it is instantiated when its conditions activate it.

make sure you do a "cancel_cue" on the "mod_save_loaded" instance whenever it is finished doing whatever its doing.
otherwise, you'll get multiple instances of it in your save game because everytime you load a game, it is activated.
this causes problems like:
1. save game bloat.
2. multiple "mod_save_loaded" active.
this is especially true if "mod_save_loaded" has sub-cues.
user1679
Posts: 1088
Joined: Fri, 20. Jul 18, 23:20

Re: How do I make my mod work on an existing save?

Post by user1679 »

kuertee wrote: Sat, 13. Mar 21, 08:49 add a first run cue that signals "mod_save_loaded"

e.g. firstly, you'll need to add the "event_cue_signalled" to "mod_save_loaded" conditions:

Code: Select all

        <cue name="mod_save_loaded" instantiate="true">
            <conditions>
                <check_any>
                    <event_game_loaded/>
                    <event_cue_signalled />
                </check_any>
            </conditions>
       </cue>
2ndly, add this cue after "mod_save_loaded". it will only activate once when the parent cue, "init" is activated:

Code: Select all

<cue name="first_run">
    <actions>
        <signal_cue cue="mod_save_loaded" />
    </actions>
</cue>
i also suggest that you add "event_game_loaded" to the "init" cue. this will allow the mod to work from an existing save.

with your set-up, loading a game that doesn't have the mod installed yet, "mod_save_loaded" will not run on "event_game_loaded"
1. because it's not yet enabled ...
2. because it's parent cue, "init" is not yet enabled ...
3. because it is only enabled when "md.Setup.Start" is signalled - which it missed because the mod was installed mid-game.

because "init" is already "active", and has stopped listening for its conditions, it won't run again at the next time the game is loaded.
only the "event_game_loaded" event will trigger "mod_save_loaded" because it is instantiated when its conditions activate it.

make sure you do a "cancel_cue" on the "mod_save_loaded" instance whenever it is finished doing whatever its doing.
otherwise, you'll get multiple instances of it in your save game because everytime you load a game, it is activated.
this causes problems like:
1. save game bloat.
2. multiple "mod_save_loaded" active.
this is especially true if "mod_save_loaded" has sub-cues.
Thanks, I got it worked out with my init cue using

<event_cue_signalled cue="md.Setup.Start" />
<event_player_created/>


and then I have two separate subcues, one listens for <event_cue_signalled cue="md.Setup.Start" /> and the other <event_game_loaded/>. When I added a few <show_help> directives, I got the proper order triggered to cover starting a new game, loading a save or adding my mod to an existing save that had never installed it previously

Now, the only thing is patching... The documentation says you can change the body of a cue as long as you don't change the header. But I find this doesn't seem to work as expected. When I save my game, exit and modify a cue, often I don't see my changes in game. If I change a simple <set_value> it seems to work but if I add an entirely new line of code like <show_help> it doesn't. I'd really like to be able to modify my mod and allow people to be able to update without starting a new game but the patching section of the help isn't exactly clear to me.
kuertee
EGOSOFT
EGOSOFT
Posts: 818
Joined: Sun, 14. Dec 03, 13:05
x4

Re: How do I make my mod work on an existing save?

Post by kuertee »

user1679 wrote: Sun, 14. Mar 21, 07:42 ...Now, the only thing is patching... The documentation says you can change the body of a cue as long as you don't change the header. But I find this doesn't seem to work as expected. When I save my game, exit and modify a cue, often I don't see my changes in game. If I change a simple <set_value> it seems to work but if I add an entirely new line of code like <show_help> it doesn't. I'd really like to be able to modify my mod and allow people to be able to update without starting a new game but the patching section of the help isn't exactly clear to me.
what you need is the versioning patch - not the XML Patching that allows you to change the base game's XML code.

Here's the versioning patch. Search for "Patching" to get to the relevant section of the page: https://www.egosoft.com:8444/confluence ... ctor+Guide

EDIT: if you're modifying a cue that's already been activated (i.e. it's not waiting to check for its conditions), you won't see those changes until either:
1. after the cue is activated again,
2. a subcue applies the changes you want,
OR
3. there's a sinceversion patch
user1679
Posts: 1088
Joined: Fri, 20. Jul 18, 23:20

Re: How do I make my mod work on an existing save?

Post by user1679 »

kuertee wrote: Sun, 14. Mar 21, 08:20
user1679 wrote: Sun, 14. Mar 21, 07:42 ...Now, the only thing is patching... The documentation says you can change the body of a cue as long as you don't change the header. But I find this doesn't seem to work as expected. When I save my game, exit and modify a cue, often I don't see my changes in game. If I change a simple <set_value> it seems to work but if I add an entirely new line of code like <show_help> it doesn't. I'd really like to be able to modify my mod and allow people to be able to update without starting a new game but the patching section of the help isn't exactly clear to me.
what you need is the versioning patch - not the XML Patching that allows you to change the base game's XML code.

Here's the versioning patch. Search for "Patching" to get to the relevant section of the page: https://www.egosoft.com:8444/confluence ... ctor+Guide

EDIT: if you're modifying a cue that's already been activated (i.e. it's not waiting to check for its conditions), you won't see those changes until either:
1. after the cue is activated again,
2. a subcue applies the changes you want,
OR
3. there's a sinceversion patch
I tried the sinceversion method but it didn't work.

What I have is an NPC that you can talk to. When I use a save game with a new version of my mod, I can no longer talk to the NPC, nothing happens. If I start a new game it works fine.

I assumed the NPC's cue was in a waiting state because I had to set "instantiate = true" in order to make it work in the original version. So I tried this in my Init cue (which actually gets called in the upgrade)

<patch sinceversion="140">
<reset_cue cue="npc_talk/>
<reset_cue cue="npc_spawn"/>
</patch>

but this had no effect. The NPC remains spawned an no longer responds to interaction. Both cues have subcues and I've tried various combinations of instantiate="true" for those and even calling <cancel_cue cue="this"/> (and reset_cue) from inside the cue itself (as mentioned in the docs) but when I do that, it breaks the whole thing even for a new game.

Because those cues are waiting for user interaction, they can't be reset or canceled from inside themselves.
kuertee
EGOSOFT
EGOSOFT
Posts: 818
Joined: Sun, 14. Dec 03, 13:05
x4

Re: How do I make my mod work on an existing save?

Post by kuertee »

user1679 wrote: Sun, 14. Mar 21, 11:47
kuertee wrote: Sun, 14. Mar 21, 08:20
user1679 wrote: Sun, 14. Mar 21, 07:42 ...Now, the only thing is patching... The documentation says you can change the body of a cue as long as you don't change the header. But I find this doesn't seem to work as expected. When I save my game, exit and modify a cue, often I don't see my changes in game. If I change a simple <set_value> it seems to work but if I add an entirely new line of code like <show_help> it doesn't. I'd really like to be able to modify my mod and allow people to be able to update without starting a new game but the patching section of the help isn't exactly clear to me.
what you need is the versioning patch - not the XML Patching that allows you to change the base game's XML code.

Here's the versioning patch. Search for "Patching" to get to the relevant section of the page: https://www.egosoft.com:8444/confluence ... ctor+Guide

EDIT: if you're modifying a cue that's already been activated (i.e. it's not waiting to check for its conditions), you won't see those changes until either:
1. after the cue is activated again,
2. a subcue applies the changes you want,
OR
3. there's a sinceversion patch
I tried the sinceversion method but it didn't work.

What I have is an NPC that you can talk to. When I use a save game with a new version of my mod, I can no longer talk to the NPC, nothing happens. If I start a new game it works fine.

I assumed the NPC's cue was in a waiting state because I had to set "instantiate = true" in order to make it work in the original version. So I tried this in my Init cue (which actually gets called in the upgrade)

<patch sinceversion="140">
<reset_cue cue="npc_talk/>
<reset_cue cue="npc_spawn"/>
</patch>

but this had no effect. The NPC remains spawned an no longer responds to interaction. Both cues have subcues and I've tried various combinations of instantiate="true" for those and even calling <cancel_cue cue="this"/> (and reset_cue) from inside the cue itself (as mentioned in the docs) but when I do that, it breaks the whole thing even for a new game.

Because those cues are waiting for user interaction, they can't be reset or canceled from inside themselves.
send me (kuertee@gmail.com) your main XML file, and I'll have a look at it.
user1679
Posts: 1088
Joined: Fri, 20. Jul 18, 23:20

Re: How do I make my mod work on an existing save?

Post by user1679 »

kuertee wrote: Sun, 14. Mar 21, 12:38
user1679 wrote: Sun, 14. Mar 21, 11:47
kuertee wrote: Sun, 14. Mar 21, 08:20

what you need is the versioning patch - not the XML Patching that allows you to change the base game's XML code.

Here's the versioning patch. Search for "Patching" to get to the relevant section of the page: https://www.egosoft.com:8444/confluence ... ctor+Guide

EDIT: if you're modifying a cue that's already been activated (i.e. it's not waiting to check for its conditions), you won't see those changes until either:
1. after the cue is activated again,
2. a subcue applies the changes you want,
OR
3. there's a sinceversion patch
I tried the sinceversion method but it didn't work.

What I have is an NPC that you can talk to. When I use a save game with a new version of my mod, I can no longer talk to the NPC, nothing happens. If I start a new game it works fine.

I assumed the NPC's cue was in a waiting state because I had to set "instantiate = true" in order to make it work in the original version. So I tried this in my Init cue (which actually gets called in the upgrade)

<patch sinceversion="140">
<reset_cue cue="npc_talk/>
<reset_cue cue="npc_spawn"/>
</patch>

but this had no effect. The NPC remains spawned an no longer responds to interaction. Both cues have subcues and I've tried various combinations of instantiate="true" for those and even calling <cancel_cue cue="this"/> (and reset_cue) from inside the cue itself (as mentioned in the docs) but when I do that, it breaks the whole thing even for a new game.

Because those cues are waiting for user interaction, they can't be reset or canceled from inside themselves.
send me (kuertee@gmail.com) your main XML file, and I'll have a look at it.
Thanks so much but I actually got part of it to work.

I can install my mod on a game that has never had it installed and it loads properly. I probably can't use the patch method because I didn't use versioning in my first release and I actually removed some cues in this version. Right now I've shifted my focus to another problem where I crash the map when I hire more than one NPC at a time.

Return to “X4: Foundations - Scripts and Modding”