[API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.32 (02/27/19))

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

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

morbideth
Posts: 391
Joined: Sun, 9. Nov 08, 03:07
x3tc

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.11

Post by morbideth » Mon, 7. Jan 19, 03:27

Bug Fix update v0.11 Fixed Right click API breaking some vanilla commands after the first click

Unclejack
Posts: 24
Joined: Wed, 5. Dec 18, 01:50
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.11

Post by Unclejack » Mon, 7. Jan 19, 04:07

Hello morbideth,

I have been working on a small lua patching script to alter the menu_map.lua and was replacing the ui.xml file in the ui/addons/egodetailmonitor.
However, I quickly noticed we can only get this to work using a substitution cat/dat archive, as you mentioned.
I have been unable to patch this ui.xml file with single lines to allow for compatability. As a result, I decided to integrate my script with your G_Work_Around API and it is working well so far.
I actually released this lua-based map enhancement on the nexusmods website (link) and included your G_Work_Around UI API v0.10.

Based on the feedback in this topic and the question of UniTrader, I hope this is alright. I did acknowledge you in the credits and provided a link to this topic for additional reference.
Thanks again for making this API! Finally, although I think you did great work, lets hope Egosoft eventually patches the _G=nil problem to smoothen the support for lua-based modding of the X4 ui.

Unclejack
Posts: 24
Joined: Wed, 5. Dec 18, 01:50
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.11

Post by Unclejack » Tue, 8. Jan 19, 03:48

Hello Again!

I have an important update/message.
Running multiple mods using the G_Work_Around will lead to conflicts if the different mods use the same md template for the work around (with a wait of 1 second).
What happens is that multiple md functions will call G_Work_Around with a param at the exact same time/frame, this will prevent G_work_Around from functioning as it cannot find all params at the same time and thus will not open the targeted lua scripts (it will actually open the same one multiple times as it happens that was the value of param at that time). This can be easily fixed by changing the timing in the md scripts between mods, but this is something to keep in mind!

Currently it is a bit unclear if this happens only when the target lua scripts are targeting the same egosoft.lua script, but it seems to be 100% reproducible with 2 mods targeting the same menu_map.lua script.

Originally reported by Forleyor on discord!

UniTrader
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 14571
Joined: Sun, 20. Nov 05, 22:45
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.11

Post by UniTrader » Tue, 8. Jan 19, 04:54

and i have an Improvement for this:
Add this md file to the G_ Workaround mod:
md/G_Work_Around.xml

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<mdscript name="G_Work_Around" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="md.xsd">
  <cues>
    <cue name="Root">
      <conditions>
        <check_any>
          <event_game_loaded/>
          <event_game_started/>
        </check_any>
      </conditions>
      <actions>
        <set_value name="$lua_files" exact="[]"/>
      </actions>
      <cues>
        <cue name="Signal" instantiate="true">
          <conditions>
            <event_cue_signalled/>
          </conditions>
          <actions>
            <append_to_list name="$lua_files" exact="event.param"/>
          </actions>
        </cue>
        <cue name="Lua_call" checktime="1s" checkinterval="0.1s">
          <conditions>
            <check_value value="$lua_files.count"/>
          </conditions>
          <actions>
            <open_menu menu="G_Work_Around" param="[0, 0, $lua_files.{1}]"/>
            <remove_value name="$lua_files.{1}"/>
            <reset_cue cue="this"/>
          </actions>
        </cue>
        <cue name="Reset">
          <delay exact="1min"/>
          <actions>
            <reset_cue cue="this.parent"/>
          </actions>
        </cue>
        <cue name="Reset_on_early_Save">
          <conditions>
            <check_any>
              <event_game_loaded/>
              <event_game_started/>
            </check_any>
          </conditions>
          <actions>
            <reset_cue cue="this.parent"/>
            <force_cue cue="this.parent"/>
          </actions>
        </cue>
      </cues>
    </cue>
  </cues>
</mdscript>
and modify the call this way:
<signal_cue_instantly cue="md.G_Work_Around.Signal" param="'.\\extensions\\ut_advanced_renaming\\utrenaming.lua'"/>
All calls will be forwarded to lua with a 0.1s time difference (i think it even ensures a time difference of at least 1 frame because of the behavior of md)
This way its even possible to make multiple calls right one after another in the same cue. its then spread out.
Signals are accepted for 1 minute after Game Start/Load because i dislike having constant polling in the background going on for a value thats extremely unlikely to change. This should be enough to register everything and set up own lua cally if needed. The old call still works as before, my script is just in-between to spread out the simulteneous calls.
if not stated otherwise everything i post is licensed under WTFPL

Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter ;)

I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help ;)

morbideth
Posts: 391
Joined: Sun, 9. Nov 08, 03:07
x3tc

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.11

Post by morbideth » Tue, 8. Jan 19, 08:14

UniTrader wrote:
Tue, 8. Jan 19, 04:54
and i have an Improvement for this:
Add this md file to the G_ Workaround mod:
md/G_Work_Around.xml
v0.20 Updated to include the above, thanks UniTrader. I've also included some debugging tools to help with writing UI mods. You can find more info in the 2nd post of this thread.

UniTrader
Moderator (Script&Mod)
Moderator (Script&Mod)
Posts: 14571
Joined: Sun, 20. Nov 05, 22:45
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by UniTrader » Tue, 8. Jan 19, 09:55

you should also mentoin the dependency node for the content.xml to ensure mod presence and version and load order ;)

Code: Select all

<content id=......>
 <dependency id="G_Work_Around" version="020" optional="false"/>
 <text language=..../>
</content>
if not stated otherwise everything i post is licensed under WTFPL

Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter ;)

I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help ;)

alexringess
Posts: 40
Joined: Tue, 25. Sep 18, 23:57
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.11

Post by alexringess » Tue, 8. Jan 19, 11:41

Unclejack wrote:
Tue, 8. Jan 19, 03:48
Hello Again!

I have an important update/message.
Running multiple mods using the G_Work_Around will lead to conflicts if the different mods use the same md template for the work around (with a wait of 1 second).
What happens is that multiple md functions will call G_Work_Around with a param at the exact same time/frame, this will prevent G_work_Around from functioning as it cannot find all params at the same time and thus will not open the targeted lua scripts (it will actually open the same one multiple times as it happens that was the value of param at that time). This can be easily fixed by changing the timing in the md scripts between mods, but this is something to keep in mind!

Currently it is a bit unclear if this happens only when the target lua scripts are targeting the same egosoft.lua script, but it seems to be 100% reproducible with 2 mods targeting the same menu_map.lua script.

Originally reported by Forleyor on discord!
You are making things more complicated than other things.
First, on the nexus page of your mod, it's difficult to know what downloading because you putted exactly the same warning on both version (with G_Workaround and without).
Second, you changed the folder name of your latest version so it's impossible to know if it's an upgrade or a substitution.
And finaly, IMO, I think it's better to simply give the link to the G_Workaround_UI_API on your mod, mentionning clearly that it is a dependancy of your mod, a requierement, and don't providing it on your own mod.
Usualy, when developpers use APIs, they provide the API separately, or publish a link to this API for the end users, and have to upgrade their software/mod/script when the API is upgraded.

morbideth
Posts: 391
Joined: Sun, 9. Nov 08, 03:07
x3tc

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by morbideth » Tue, 8. Jan 19, 11:52

UniTrader wrote:
Tue, 8. Jan 19, 09:55
you should also mentoin the dependency node for the content.xml to ensure mod presence and version and load order ;)

Code: Select all

<content id=......>
 <dependency id="G_Work_Around" version="020" optional="false"/>
 <text language=..../>
</content>
Didn't know there was such a thing, prolly hidden in the X:R documentation. I'll try and squeeze it in, near the text limit. Edit: it fit, I'll put a content.xml file in the templet next update.

Unclejack
Posts: 24
Joined: Wed, 5. Dec 18, 01:50
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by Unclejack » Tue, 8. Jan 19, 16:15

@Alexringess
I don't fully understand what you mean when mentioning both downloads have the same description/name. They never had the same name or description, this was also true for the original upload of the files:

Code: Select all

UJfactionspecific_objectcolours
UJfactionspecific_objectcolours_withG_Work_Around
EHM3_factionObj_and_Sect_Colours
EHM3_factionObj_and_Sect_Colours_withG_Work_Around
Although the descriptive sentences clearly differ in the with and without, I agree this might be improved.
Then, I agree with you that including the API is maybe not the best course of action and I agree that it would be best to refer to an API independently. However, the API is not "officially" released on the Nexusmods (or anywhere else afaik) and I do not really wish to force people to the Egosoft Forums to download this mod in this early stage of X4 modding. First it is not very user-friendly, second, although Morbideth is doing great and I like these forums, some people might not understand completely what is happening when coming to this specific, highly technical, post on an unknown forum for the first time. Although the download link is at the bottom of the post, it is not as clear as the Nexusmod layout, it also does not download the file, it actually is a mirror to a mediafire archive which does not download automatically either (luckily you do not need an forum account to use the download link). Thirdly, this topic is a development topic/guide with a not so (imo) user-friendly title and will change rapidly over time, for instance, the UI api is now updated to v0.2 and although this is great, it could have been uncompatible with my current files on the Nexusmods. So currently, I prefered to package them together as it can guarantee it will at least work when downloading and installing it fresh. Of course I will try to keep updating it as the UI api is updated and keep up with this great workaround, but I also have to work and cannot update the nexusmods page every hour, especially if the UI api itself is rapidly changing and not "officially" released. Once the API is a bit more stable and widely used (or officially user released), or once the modding scene has gone forward, I will remove the UI API packaged with the mod, but for now I wished to include it. If you still feel you do not like my way of releasing the mod or if you have other problems, feel free to provide additional feedback in Discord, with a PM or on the Nexusmod page.

Finally, the reported problem is actually not specific to my personal Nexusmod release or preference for releasing files. The problem reported by Forleyor, as i see it, is a general compatibility problem that would have happened with every lua script out there. Although i am not a professional scripter, I also feel it is my duty to help and suppport this api's development as much as i can as one of its users. I fail to see how this is making things more complicated than necessary in general.

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 13:16
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by LegionOfOne » Tue, 8. Jan 19, 18:09

Does anyone among you fine folks know how to create our own ffi functions, and where the original ones are located ?

I'm trying to add a new functionnality, specifically to allow us to choose the volume of every buy or sell offer on our stations.
Since that simply doesn't exist yet, I believe I need to create my own ffi functions to handle the logic.

If you can think of another path to the same result, that would also be very helpful.

Thanks in advance !

morbideth
Posts: 391
Joined: Sun, 9. Nov 08, 03:07
x3tc

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by morbideth » Tue, 8. Jan 19, 19:26

LegionOfOne wrote:
Tue, 8. Jan 19, 18:09
Does anyone among you fine folks know how to create our own ffi functions, and where the original ones are located ?

I'm trying to add a new functionnality, specifically to allow us to choose the volume of every buy or sell offer on our stations.
Since that simply doesn't exist yet, I believe I need to create my own ffi functions to handle the logic.

If you can think of another path to the same result, that would also be very helpful.

Thanks in advance !
Um, yeah, you can't write your own ffi function... those are defined by the devs. You would need access to the source code to add them (or I guess you could try decompling the game or modding in assembly :gruebel:)


I think cargo.{$ware}.target might do what you wish. See scriptproperties.html for more info.

SetContainerWareIsBuyable, SetContainerWareIsSellable, SetContainerStockLimitOverride, and SetContainerWarePriceOverride are also options. The UI documentation is here

MD scripts may also be an option.

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 13:16
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by LegionOfOne » Tue, 8. Jan 19, 19:35

Thanks a lot for your answer, that was very helpful and detailed.
I won't waste any more time on trying to mod my ffi functions thanks to you, and I have a few good hints to follow. It's exactly what I needed.

DeadMor0z
Posts: 26
Joined: Fri, 29. Nov 13, 18:20
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by DeadMor0z » Tue, 8. Jan 19, 21:55

What do you think about using (md) <raise_lua_event name="'EventName'" param="'path_to_lua_file'"/> and (lua) RegisterEvent("EventName", eventCallBack) variant, rather than open_menu?
Events should be queued by default, so there will be no problem with multiple extensions raising event. And it will make workaround code much simplier.

morbideth
Posts: 391
Joined: Sun, 9. Nov 08, 03:07
x3tc

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by morbideth » Wed, 9. Jan 19, 03:18

DeadMor0z wrote:
Tue, 8. Jan 19, 21:55
What do you think about using (md) <raise_lua_event name="'EventName'" param="'path_to_lua_file'"/> and (lua) RegisterEvent("EventName", eventCallBack) variant, rather than open_menu?
Events should be queued by default, so there will be no problem with multiple extensions raising event. And it will make workaround code much simplier.
It's an idea. I haven't looked at those yet. If it works it would get rid of all of the debug spam from opening an empty menu, which would make it worth doing. Going to be a while before I can look into this, unless someone else already has.

DeadMor0z
Posts: 26
Joined: Fri, 29. Nov 13, 18:20
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by DeadMor0z » Wed, 9. Jan 19, 04:39

morbideth wrote:
Wed, 9. Jan 19, 03:18
DeadMor0z wrote:
Tue, 8. Jan 19, 21:55
What do you think about using (md) <raise_lua_event name="'EventName'" param="'path_to_lua_file'"/> and (lua) RegisterEvent("EventName", eventCallBack) variant, rather than open_menu?
Events should be queued by default, so there will be no problem with multiple extensions raising event. And it will make workaround code much simplier.
It's an idea. I haven't looked at those yet. If it works it would get rid of all of the debug spam from opening an empty menu, which would make it worth doing. Going to be a while before I can look into this, unless someone else already has.
I used this technique to pass data back to lua code from md script in my mod - works great.

EDIT: Rewrited latest API to use raise_lua_event and RegisterEvent, checked - all done well. You can get and check it from here:

MUST BE INSTALLED IN YOUR %installdir%\extensions folder! REQUIRES: basic literacy to install.
v0.30 Right Click API, _G Work Around, Modding Templet, and Debugging Tools
[ external image ]

LegionOfOne
Posts: 122
Joined: Sun, 16. Dec 18, 13:16
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by LegionOfOne » Wed, 9. Jan 19, 23:15

DeadMor0z wrote:
Wed, 9. Jan 19, 04:39
Rewrited latest API to use raise_lua_event and RegisterEvent
I don't know enough to say if the code itself is better or worse than before.
But I tested it with my UI mod, and it worked just fine, while clearing up the debug log a bit. It's still full, but of unrelated things.
So from a purely empirical view, it seems like an interesting improvement.

Thanks for sharing it !

In that spirit, I have code that adds a 'Create/Remove Buy Offer' button to the station logical overview UI, and allows buy offers to display for 'trade goods', not used in production or produced by the station.
It is mostly useful if you use the Warehouse mod, but anyone who wants to see how it works, or wants to integrate it in a mod can request it via PM.

Unclejack
Posts: 24
Joined: Wed, 5. Dec 18, 01:50
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by Unclejack » Thu, 10. Jan 19, 04:13

Hello again.

Thanks for keeping this updated and working hard on it everyone. I just downloaded the latest package from DeadMorOz (v0.3) and it works with my lua scripts.
@DeadMorOz, I just wanted to let you know the version in the content.xml file is still 0.2 (but I guess this is for the dependency?)

Also, more importantly maybe, a nexusmods user (Te1waz) has reported that when playing the game in a borderless window and pressing alt+enter, which kind of minimizes the window, the mod's lua script he is using fails to work.
I tried repeating his problem and it is reproducible with the v0.1, 0.2 and 0.3 API and two of my lua scripts (one is the Enhanced Holomap 3). In addition, I also tried the command /reloadui in game, which also fails to reinitiliaze the modded lua script/G_Work_Around (maybe as expected). As Alt+Enter from a borderless window seems to fully reset the UI, I tried to call /refreshmd (edit) before using alt+enter or the /reloadui command. However, this failed to reinitialize the lua script/G_Work_Around as well. I guess it would be difficult to fix and the md scripts would need to keep running. Maybe we can think of something to recall the lua script from the md file when the UI is just reloaded for a given game, although I do not really know. I know the debugging tools "allow" for reloadui, but as Alt+enter in a borderless window is user input that any player (not debugging or using the chat console) might use, it would be nice to find a way to prevent the problem.

Then, when I downloaded the current 0.2 version of the API, I noticed that in every subfolder a hidden folder called: ".tmp.drivedownload" is present.This folder was not present in 0.1 AFAIK and is also not present in the "0.3" version. Is this an oversight when packaging your folder?

Finally, I just saw the updated "user" terms and will update my mod accordingly when possible. However, do you think it would be possible to include a user friendly download link at the top which does not include the hidden folder and the debugging tools and template? It would increase the user friendlyness of the post and would make it easier to send people here, where they find the download link at the top without the extra baggage. Also, maybe it would be possible to include a basic readme within the downloaded package, this will help people that just want to download it quickly without scrolling through the forum post. I understand if you would prefer not to do this, it is just a suggestion.. although having multiple download links is not nice either.
Last edited by Unclejack on Thu, 10. Jan 19, 15:41, edited 1 time in total.

DeadMor0z
Posts: 26
Joined: Fri, 29. Nov 13, 18:20
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by DeadMor0z » Thu, 10. Jan 19, 10:07

Unclejack wrote:
Thu, 10. Jan 19, 04:13
Hello again.

Thanks for keeping this updated and working hard on it everyone. I just downloaded the latest package from DeadMorOz (v0.3) and it works with my lua scripts.
@DeadMorOz, I just wanted to let you know the version in the content.xml file is still 0.2 (but I guess this is for the dependency?)

Also, more importantly maybe, a nexusmods user (Te1waz) has reported that when playing the game in a borderless window and pressing alt+enter, which kind of minimizes the window, the mod's lua script he is using fails to work.
I tried repeating his problem and it is reproducible with the v0.1, 0.2 and 0.3 API and two of my lua scripts (one is the Enhanced Holomap 3). In addition, I also tried the command /reloadui in game, which also fails to reinitiliaze the modded lua script/G_Work_Around (maybe as expected). As Alt+Enter from a borderless window seems to fully reset the UI, I tried to call /resetmd before using alt+enter or the /reloadui command. However, this failed to reinitialize the lua script/G_Work_Around as well. I guess it would be difficult to fix and the md scripts would need to keep running. Maybe we can think of something to recall the lua script from the md file when the UI is just reloaded for a given game, although I do not really know. I know the debugging tools "allow" for reloadui, but as Alt+enter in a borderless window is user input that any player (not debugging or using the chat console) might use, it would be nice to find a way to prevent the problem.

Then, when I downloaded the current 0.2 version of the API, I noticed that in every subfolder a hidden folder called: ".tmp.drivedownload" is present.This folder was not present in 0.1 AFAIK and is also not present in the "0.3" version. Is this an oversight when packaging your folder?

Finally, I just saw the updated "user" terms and will update my mod accordingly when possible. However, do you think it would be possible to include a user friendly download link at the top which does not include the hidden folder and the debugging tools and template? It would increase the user friendlyness of the post and would make it easier to send people here, where they find the download link at the top without the extra baggage. Also, maybe it would be possible to include a basic readme within the downloaded package, this will help people that just want to download it quickly without scrolling through the forum post. I understand if you would prefer not to do this, it is just a suggestion.. although having multiple download links is not nice either.
About version - I just forgot to update it.
As of Alt-Enter/reloadui - there is a problem:
when ui is reloaded, lua stack will be cleared and all lua scripts will be initialized. since md scripts already triggered at savegame load/game start - lua workraround script will know nothing about lua addons and won't load them.
I don't see a way now how to track which lua addons were loaded and autoreload them when ui is reloaded. I can recommend to call /refreshmd (not /resetmd - there is no such command) AFTER UI reset (after Alt_Enter or /reloadui) - this should help. Also, reloading savegame will refresh md and ui and should help.

Romz
Posts: 21
Joined: Sat, 24. Dec 16, 19:39
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by Romz » Fri, 11. Jan 19, 00:53

DeadMor0z wrote:
Wed, 9. Jan 19, 04:39
EDIT: Rewrited latest API to use raise_lua_event and RegisterEvent, checked - all done well. You can get and check it from here:

MUST BE INSTALLED IN YOUR %installdir%\extensions folder! REQUIRES: basic literacy to install.
v0.30 Right Click API, _G Work Around, Modding Templet, and Debugging Tools
[ external image ]
That broke all my mods, that use _G Workaround

DeadMor0z
Posts: 26
Joined: Fri, 29. Nov 13, 18:20
x4

Re: [API/GUIDE] How to mod the UI (_G Workaround and Right Click API) v0.20

Post by DeadMor0z » Fri, 11. Jan 19, 01:59

Romz wrote:
Fri, 11. Jan 19, 00:53
DeadMor0z wrote:
Wed, 9. Jan 19, 04:39
EDIT: Rewrited latest API to use raise_lua_event and RegisterEvent, checked - all done well. You can get and check it from here:

MUST BE INSTALLED IN YOUR %installdir%\extensions folder! REQUIRES: basic literacy to install.
v0.30 Right Click API, _G Work Around, Modding Templet, and Debugging Tools
[ external image ]
That broke all my mods, that use _G Workaround
That was not intended to use with mods, which were made for v0.20 of this mod, as it uses different load technique. if you need all your mods to work with this new version - you need to change their md script in accordance with new template.
Also, I'm not the author of this mod - I made changes for him (or anyone interested) to CHECK how it works.

Post Reply

Return to “X4: Foundations - Scripts and Modding”