View previous topic :: View next topic |
Author |
Message |
MaxRock
Joined: 24 Jan 2008 Posts: 196
|
Posted: Fri Sep 03, 2010 20:36 Post subject: Modifying existing, adding new script functions:nwscript.nss |
|
|
I'm wondering if anybody has ever tried to add new script functions by adding them to nwscript.nss directly?
If yes, why was it abandoned? (Can't seem to find any mention of it.) |
|
Back to top |
|
|
axs
Joined: 11 Feb 2005 Posts: 76
|
Posted: Fri Sep 03, 2010 22:49 Post subject: |
|
|
I added additional arguments, new functions? I didn't try. |
|
Back to top |
|
|
ShaDoOoW
Joined: 20 Aug 2005 Posts: 584
|
Posted: Fri Sep 03, 2010 23:51 Post subject: |
|
|
yes i tried but it didnt compiled, hmm new arguments are possible? didnt know/even tested this _________________ Community Patch / NWNX Patch / NWNX Files / NWNX Connect |
|
Back to top |
|
|
MaxRock
Joined: 24 Jan 2008 Posts: 196
|
Posted: Sat Sep 04, 2010 1:35 Post subject: |
|
|
I'm just asking to avoid my usual "foot-in-mouth" situations in case this has already been tried and there's a reason why it can't possibly work.
Anways...
I've been trying to implement 3.5 edition damage reduction and needed 2 new item properties / effects for damage reduction and overcoming damage reduction. Initially I was just going to "hijack" one of the existing itemproperty functions to return the new one I wanted but a closer look at CVirtualmachineCommands::ExecuteCommand revealead that the VM is actually open to new functions.
A new function at the end of nwscript.nss will actually be processed by ExecuteCommand - and of course found invalid unless you hook it - but number and types of arguments and returning values works fine.
It seems a more "elegant" way than going through SetLocalString.
Of course the catch is in how to modularize it:
- Only 1 plugin can hook CVirtualmachineCommands::ExecuteCommand
- Extending the function table
- Adding to nwscript.nss |
|
Back to top |
|
|
virusman
Joined: 30 Jan 2005 Posts: 1020 Location: Russia
|
Posted: Sat Sep 04, 2010 1:38 Post subject: |
|
|
NWNX 2.8 with its new plugin interface can solve this problem. Unfortunately, it's not available on Windows yet..
The nwscript.nss approach hasn't been used for several reasons:
1. This technique requires more effort than just using the existing SetLocalString hook
2. Sharing the hook was impossible before 2.8-dev
3. Editing nwscript.nss is generally unsafe:
* A script compiled with modified nwscript.nss, when run without NWNX, may crash the VM or the server.
* If several plugins add new functions, there is a problem with merging the file and defining their order _________________ In Soviet Russia, NWN plays you! |
|
Back to top |
|
|
MaxRock
Joined: 24 Jan 2008 Posts: 196
|
Posted: Sat Sep 04, 2010 2:13 Post subject: |
|
|
nwscript.nss is definitly the biggest hurdle. (actually it's possible to put an #include "newfunctions" at the end and those new functions will work).
But it seems that at some point the engine must enumerate the functions in nwscript.nss: the first new one is 848, the next one 849, and so on.
Still leaves the problem that nwscript.nss has to be edited somehow...
Well, maybe the idea isn't practical for a general nwnx plugin, but for one tailored to a server I think it's interesting.
EDIT:
Yea, nevermind. The compiler enumerates the functions, duh |
|
Back to top |
|
|
ShaDoOoW
Joined: 20 Aug 2005 Posts: 584
|
Posted: Sat Sep 04, 2010 7:05 Post subject: |
|
|
virusman wrote: |
* A script compiled with modified nwscript.nss, when run without NWNX, may crash the VM or the server.
| Not sure if I understand here, but I do using modified nwmain since two years back when I find the article from Georg Zoeller, without any problems with NWNX both on linux and windows basically with all plugins available...
only functions rename _________________ Community Patch / NWNX Patch / NWNX Files / NWNX Connect |
|
Back to top |
|
|
MaxRock
Joined: 24 Jan 2008 Posts: 196
|
Posted: Sat Sep 04, 2010 8:10 Post subject: |
|
|
ShaDoOoW wrote: | virusman wrote: |
* A script compiled with modified nwscript.nss, when run without NWNX, may crash the VM or the server.
| Not sure if I understand here, but I do using modified nwmain since two years back when I find the article from Georg Zoeller, without any problems with NWNX both on linux and windows basically with all plugins available...
only functions rename |
I'm using that too.
From what I gather the virtual machine doesn't really care about function names; the compiler generates a number from the position in nwscript.nss which the vm uses to determine which function to actually call. EffectHeal(), for example is 78. If you rename it to ExplodeToLittleTinyBits it'll still work cause the compiler still sees it as 78.
The VM has a big switch/case which is used to determine which function to call (I know, it's really a function pointer array, but I find switch/case easier to explain)
Code: |
int CNWVirtualMachine::ExecuteCommand(function_number, ...) {
switch (function_number) {
case 0: ExecuteCommandRandom(...); break;
case 1: ExecuteCommandPrintString(...); break;
.
.
case 847: ExecuteCommandItemPropertyEffect(...); break;
}
}
|
The nice thing is that if you add a new function at the end of nwscript.nss, the compiler will happily assign it the next number - in this case 848 - and if you hook ExecuteCommand to extend the switch/case you can use that function in any script
The problem Virusman is talking about is that bad things might happen if the script is run without the plugin hooking ExecuteCommand. |
|
Back to top |
|
|
ShaDoOoW
Joined: 20 Aug 2005 Posts: 584
|
Posted: Sat Sep 04, 2010 9:05 Post subject: |
|
|
what about constants? I ask because I added one constant (const int SAVING_THROW_TYPE_PARALYSE = 20;) into nwscript.nss for my patch project, yet didnt tested yet. I added it under last saving_throw_type... _________________ Community Patch / NWNX Patch / NWNX Files / NWNX Connect |
|
Back to top |
|
|
MaxRock
Joined: 24 Jan 2008 Posts: 196
|
Posted: Sat Sep 04, 2010 18:10 Post subject: |
|
|
Not quite sure about constants but they shouldn't matter too much, since the compiler simply substitutes them for their value. Adding them anywhere certainly doesn't change the function numbering. |
|
Back to top |
|
|
Sag
Joined: 24 Sep 2009 Posts: 8
|
Posted: Thu Sep 23, 2010 15:18 Post subject: |
|
|
The article can be found here:
http://www.gulbsoft.org/nwn/how_to_override_core_scripting_functions
I renamed GetCasterLevel to GetCasterLevelVoid and made a function in nw_i0_spells named GetCasterLevel which then uses ExecuteScript from which I retrieve the caster's level based on conditions I added in. Granted I had to compile all spell scripts but once I did that, I simply added them to the server's override folder. It works quite well and the "ExecuteScript" function allows me to edit a different script without the need to recompile everything.
I'm sure the same could be done to run sub-functions in this way. It's a bit of a hack, but it works. |
|
Back to top |
|
|
|