[Scripting] Intelligent script loading and addressing development issues

renotoasterrenotoaster Join Date: 2012-11-26 Member: 173309Members
So I'm trying to make a simple (client side, for now) mod for NS2.
I've gotten up to the point I can do simple stuff (log text, run functions etc).

Question: Is there a way to load my custom scripts "intelligently"?
The meager modding FAQ (http://www.unknownworlds.com/ns2/wiki/index.php/Modding) uses Client.lua to as the "entry point" to load scripts. I added a Script.Load to my own custom script (simple, working simple text logging). The problem comes when I change something in my custom script, NS2 flips (most likely because of "double loading" of the Client.lua?) and starts to spam the console with errors.

Using a custom Client.lua (the only has like "Script.Load('lua/MyCustomScript.lua')") overrides the default one -> stuff that needs to get loaded, doesn't get loaded.
Is there a way to load my custom scripts intelligently, like (in essence) "appending" to the original Client.lua (for example, and still keeping my custom script in my mod folder)?
Can't I just say "just load MyCustomScript.lua in my mod folder"?

It becomes a bother the reload the map / restart the game every time I need to make a change. (Hotloading DOES work, but the aforementioned double loading(?) becomes an issue.)

Comments

  • MCMLXXXIVMCMLXXXIV Join Date: 2010-04-14 Member: 71400Members
    edited November 2012
    This problem is far from solved but a few good solutions exist. One effective way to do it (as we've used in Combat) is to make use of fsfod's hooks structure, which allows you to make changes ('hooks') at the beginning or end of functions, or replace them entirely, as well as creating your own classes and functions that aren't hooks of existing ones. This strategy takes a little effort to get right but once you get it right you'll find that you can consistently write code that augments NS2's gameplay while overriding or changing just the parts you need, rather than bringing in a whole load of unrelated code into your mod and having it break on every NS2 update. If you want to see how we did it, take a look at Combat_Client.lua in NS2Combat's <a href="https://github.com/AlexHayton/NS2Combat" target="_blank">source tree</a>.

    I'm working on a simpler example at the moment, which is much easier to use as a basis for simple gameplay mods. I'm adding a new 'magno boots' item into the game, which allows marines to walk on the walls and ceilings (a cheaper complement to the jetpack), using all the techniques we learned during the development of NS2Combat. I'll post it on here when the code is polished enough to be used as the basis for a 'how-to' guide on creating new functionality on top of NS2, but it might take a day or so before it's ready...
  • renotoasterrenotoaster Join Date: 2012-11-26 Member: 173309Members
    Thanks for the example, I'll have a look more thoroughly when I have the time.

    Maybe it's blindingly obvious, but I don't quite understand how it works? Standalone scripts (those not named like the original scripts, like Main.lua, Client.lua etc.) don't get autoloaded by the game, so the "entry point" must still be one of the "original files"? Which brings the case back to the double loading issue -> error spam in console.

    In a perfect NS2 scripting world (that doesn't seem to be here yet) I could just have a ModMain.lua or something that gets autoloaded first, and I could go on from there. But I'll have to make do for now.

    Not familiar with fsfod, but I take it that changing code in these hooks don't trigger the double loading issue, or something similar?
    Could you provide a bare bones demo/explanation that I could use to get it working? So that changing code with hotloading would not blow up the console with errors.
    Or should I just do some checks if everything is loaded, and just, well, not double load it again?

    Very informative with the github sources, maybe I'll learn something.
  • MCMLXXXIVMCMLXXXIV Join Date: 2010-04-14 Member: 71400Members
    edited November 2012
    The point of first contact with your mod is actually your game_setup.xml file. It's in there that you define the entry points for the Client, Server and Prediction VMs that run the game lua code (you only have to include the ones you want to define entry points for). If you notice, we have made ours point to combat_Client, combat_Server and combat_Predict respectively. We then define all our function hooks (which will be applied once the main script has loaded), load the main NS2's Client.lua, Server.lua or Predict.lua inside our code, and then override any of the variables and values that we want to override that are defined by the main NS2 code.

    NS2's main code has been designed so that it can be 'loaded' multiple times without causing errors, just replacing the defined variables and functions each time. This means it doesn't matter if multiple mods call e.g. Client.lua or Server.lua, but sometimes they won't interact well with one another as a result. You should write your script in the same way, so that it can be hotloaded and not blow up. One way of doing this is checking whether objects have already been defined and then skipping the second instantiation of them if it causes errors.
  • renotoasterrenotoaster Join Date: 2012-11-26 Member: 173309Members
    Ah yes, so the trick is to load your scripts before the defaults, it seems.

    Wondering why I got the errors before if it is supposed to handle them. Might be because of the copied Client.lua like in the tutorial.
    Have to experiment a little when I get home. It should get me started then, thanks!
  • VitdomVitdom Join Date: 2012-04-30 Member: 151345Members, Reinforced - Supporter, Reinforced - Silver, Reinforced - Gold, Reinforced - Diamond, Reinforced - Shadow, WC 2013 - Shadow
    Is there any current way of loading several different mods and not make anything break? I also want an intelligent load mechanism, this would solve all issues.
  • WilsonWilson Join Date: 2010-07-26 Member: 72867Members
    edited November 2012
    How do you know whether to load the hook classes in client or server? I tried a simple mod to test it and it wouldn't work at first when I had it loading in client but when I changed it to server it worked fine.

    Also, I couldn't get hotloading to work using this method. No matter what I changed in my replaced function it didn't change until I reloaded the game.
  • renotoasterrenotoaster Join Date: 2012-11-26 Member: 173309Members
    Vitdom, there's the NS2Modloader. Haven't tried it myself, but guessing by the name it might do the trick.

    Wilson, educated guessing I'd think? The documentation on scripting is nearly non-existent and the behavior seems erratic at best. The client-side script I made still suffer from the console spamming errors. I wish there would just be a way to append your own stuff to the currently run scripts (with your mod), and not resort having to the hope for the best that it doesn't blow up.

    On a brigter side, server-side script ran (and edited on the fly) without errors.
  • WilsonWilson Join Date: 2010-07-26 Member: 72867Members
    Bump, anyone know how to get hotloading working with this method?
Sign In or Register to comment.