[Scripting] Intelligent script loading and addressing development issues
renotoaster
Join Date: 2012-11-26 Member: 173309Members
in Modding
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.)
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
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...
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.
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.
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!
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.
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.