Jump to content

register for mcm event?


Recommended Posts

Does your mod already have an MCM? If so, you can just reference a script attached to your trigger box directly in the MCM.

 

Let's say you have this script attached to your trigger box:

 

Scriptname TM_TriggerBoxScript extends ObjectReference 

Function ConfigOpen() 
    ;do something
EndFunction 

Function ConfigClose() 
    ;do something
EndFunction

Then in your MCM script you can do this:

 

TM_TriggerBoxScript Property TriggerBoxScript Auto 
;in the CK, fill the property with the reference the TM_TriggerBoxScript is attached to.

Event OnConfigOpen() 
    TriggerBoxScript.ConfigOpen() 
EndEvent 

Event OnConfigClose() 
    TriggerBoxScript.ConfigClose() 
EndEvent

This will only work for when you open and close your MCM menu. If you want to detect when any MCM menu is opened, that might be more difficult to do.

Link to comment
Share on other sites

IMO there are better ways than OnConfigOpen and Close. Its not when menus are closed, but rather when that MCM is exited. You may trigger things to happen repeatedly if they go back into it, etc

 

For simple stuff its easy enough to watch for an deal I suppose. Your trigger box could use States or a Bool Property that the MCM tells it to change I guess

Link to comment
Share on other sites

I have multiple scripts attached to different triggerboxs in different patches. I want any of them to be able to know when a toggle in my MCM menu has been set. The toggle is for the scripts to know that debugging code is to be used.

I'm not sure about:

TM_TriggerBoxScript Property TriggerBoxScript Auto
;in the CK, fill the property with the reference the TM_TriggerBoxScript is attached to.

for 8 different scripts when in any patch only one of them might be used. The MCM menu is from a script shared by all my patches.

At the moment each script queries the MCM toggle value using OnLoad() but that is not guaranteed to catch a change in the MCM menu when made.

 

diziet

Link to comment
Share on other sites

You cannot register for a mod event on one script and that event be running on a separate script. The events registered for are for events to be ran on the script doing the registering. Thus you can use SendModEvent on the MCM to send a trigger for a custom event that your eight other scripts have registered for.

 

Here is an example:

 

 

MCM Script

Event OnConfigClose()
  RegisterForSingleUpdate(0.1) ;causes the game to wait till the menu is fully closed before processing
EndEvent
 
Event OnUpdate()
  ;do some stuff
  SendModEvent("HeyRunSomeDebugStuff")
  ;do other stuff maybe
EndEvent

Other scripts

Event OnInit()
  RegisterForSingleUpdate(0.25) 
  ;OnInit can sometimes run twice especially on quests
  ;this helps reduce running code twice unnecessarily
  ;use this update trick only if needed
  ;otherwise skip and call the function directly
EndEvent

Event OnUpdate()
  MaintRoutine()
EndEvent
 
Function MaintRoutine()
  RegisterForModEvent("HeyRunSomeDebugStuff","OnMCMMenuClosed")
EndFunction
 
Function OnMCMMenuClosed()
  ;do debug stuff
EndFunction

If you want to update the eight scripts mid-game after their OnInit event may have ran. Use a player alias script to call the MaintRoutine function during the OnPlayerLoadGame event similar to dybill's suggestion. You can use GetFormFromFile to obtain the trigger boxes instead of direct properties.

 

 

 

EDIT: slight adjustment to the example

Edited by IsharaMeradin
Link to comment
Share on other sites

If you want to update the eight scripts mid-game after their OnInit event may have ran. Use a player alias script to call the MaintRoutine function during the OnPlayerLoadGame event similar to dybill's suggestion. You can use GetFormFromFile to obtain the trigger boxes instead of direct properties.

 

So your suggestion is that the player alias script uses:

GetFormFromFile(trigger_formid,"my_mod_esp").Do_stuff()

one for each script that may be running?

Is it considered ok to do this knowing in advance that maybe 7 out 8 of possible scripts will not be running?

I have over 200 very simple patches, most of which use my own base object triggerboxes with scripts already attached. This I assume would use GetFormFromFile on my esp that has the activators I made. But some of them are have copies of these scripts attached to existing trigger boxes in other mods, I would need to add those trigger boxes to the player alias script?

In the long run this might not be workable.

 

diziet

Link to comment
Share on other sites

You can use it knowing that some may not be in use. If you are already using SKSE, you can use GetModByName to determine if a plugin is present before running code that relies on something from that mod. i.e. If Game.GetModByName("Hearthfires.esm") != 255

 

GetFormFromFile will also harmlessly fail (will put a printout in the papyrus log) if the plugin is not present so GetModByName is not strictly necessary. Just nice for removing "spam" from the papyrus log.

 

You can always come up with some other way to trigger the registration for the mod event. There just won't be any guarantee that it will happen in a timely manner. Any situation where the player can trigger the mod event before one or more triggerboxes can register to receive the mod event is not ideal.

Link to comment
Share on other sites

Im rather fond of GetFormEx. If you number your IDs in a sequential manner you can fetch IDs relative to a base ID like say... a MCM quest

 

Int MyOwnHex = (Self as Quest).GetFormID()
Form NextA = Game.GetFormEx(MyOwnHex + 1)
Form NextB = Game.GetFormEx(MyOwnHex + 2)
Form NextC = Game.GetFormEx(MyOwnHex + 3)
Etc

 

This takes the file name out of the equation aka you can rename your mod and it wont matter plus its useful too especially if you get carried away and want to do this stuff in an array

 

Prereq is you set FormIDs in XEdit accordingly. So the quest can end in 00 for example, and your first trigger would end with 01, and the next 02, and so on. You can declare these as properties instead
ofc but depending on the project at hand you might be better benefitted using an approach like this. You also can cleanly swap to another approach if you arent storing anything. Just another approach

 

Other objects/refs can be instructed to listen for mod events so long as they have a script ready to listen. Anything can send a mod event though, no script needed, but something does have to invoke that

 

Game.GetFormEx(MyOwnHex + 3).RegisterForModEvent("Whatever","OnThisOrThat")

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...