Closed racinmat closed 6 years ago
What you need is a Balking Pattern (see Wikipedia for further information). The original purpose of this pattern is to block a thread until another signals it to continue but it can easily be widened for a greater purpose.
internal static class Mediator
{
//...
}
internal bool ConfigIsLoaded {get; set;}
private void LoadConfig()
{
//Code that loads the config...
Mediator.ConfigIsLoaded = true;
}
Code example for Script2
while (!Mediator.ConfigIsLoaded)
return;
// Do stuff which needs to wait until the config is loaded.
The great thing about a static class here is that you don't need an instance. You can call directly into the mediator. CAUTION! You have entered the realm of parallel programming. That needs special attention because it can cause a so called "race condition" and other issues you wouldn't normally think about. Please educate yourself about parallel programming. It would be way too much to explain it all here.
Another example would be the transfer of data like
internal static class Mediator
{
internal static SomeDataContainer MyDataContainer {get; set;}
// Possibly more properties.
}
internal class SomeDataContainer
{
// Add any kind of properties.
internal bool ContentIsValid { get; set; }
}
public class Script1 : Script
{
internal void SetMyDataContainer()
{
Mediator.MyDataContainer.ContentIsValid = false;
// Modify data in the container...
Mediator.MyDataContainer.ContentIsValid = true;
}
}
public class Script2 : Script
{
internal void SomeMethodWhichNeedsTheDataFromTheContainer()
{
/* Note: Since ContentIsValid is false by default
there will be no conflicts at initial start. */
if(!Mediator.MyDataContainer.ContentIsValid)
{
// Do whatever needs to be done in this case.
return;
}
// Do stuff with Mediator.MyDataContainer...
}
}
Thanks much for explaining. I thought something like static class would do. This will do perfectly for my purpose. I just thought scripthook allows me accessing list of loaded scripts, seems like it does not. I think race conditions don't bother me because from one class I will use it only for writing, and from other class, only for reading.
@racinmat 1. Have you considered if your writing script possibly executes before the values are valid? Also consider the possible extension of your mod in the future. A "Dirty" state is just a little property but can be a great help.
What I mean is that someone could write a malicious mod which causes other mods to crash. You know trolls. I have already to deal with users who install all kind of crap and then come complaining because allegedly one of my mods doesn't work. However, I was always able to resolve their issue and it showed it was something else causing trouble (forgot a helper dll, conflicting key binding, installation error, etc.).
Hi, I have a main script, which does many things, some heavy lifting, and has interval 100ms. And I have helper script, with interval=0, which just manages camera and stuff. And I need to get instance of that helper script in the main script, so I couls set som attributes there. Is it possible?