Skip to content

Suggestion: Call a function of a differen script

prwoprwo Member Posts: 69
Currently, ExecuteScript always and only invokes the main function of the provided script. It would be very useful if the function to be actually executed could be provided as well. This would allow to call an arbirary function of another script.

E.g.
a_script.nss: void MyFunction1 () { // Do something here } another_script.nss: ... ExecuteScript("a_script", oObject, "MyFunction");

This would allow to group related functions together in a script instead of making own files for each and every one of them.

Further advances would be to allow for the scripts to return values (int, object, ...) to the caller.

(Maybe this idea was posted in another sub-thread alredy, but I couldn't find it anymore.)

Comments

  • ValgavValgav Member Posts: 25
    Why just not use include?
  • prwoprwo Member Posts: 69
    Valgav said:

    Why just not use include?

    That is possible, but it is cumbersome mainly for two reasons:
    • Before NWN:EE 74.8164 by using massive includes, the identifier list would be filled up fast and you'd run into the nasty IDENTIFIER LIST FULL problem pretty soon. I don't know how many hours / days I spent on splitting up include files into smaller portions... Luckily the identifier list has been doubled in size in patch 8164, so that is hopefully mitigated.
    • When you have your main code in an include file and when you include that in many other files, you will have to recompile all depending files if you change something in the included files. Otherwise you are asking for incorrect behavior. The safest way is to recompile just everything, but that is often time consuming. Programming languages try to avoid this if possible.
  • ValgavValgav Member Posts: 25
    My approach is to define two types of files main and helpers this way I can use the same helper in many mains without risk of including mains. I would say for now safest approach is probably to switch to java and use magic of this horrible language ;)
  • ProlericProleric Member Posts: 1,285
    @prwo for now the workaround is to pass a local variable to the second script (as you said in your thread on executing conditional scripts).
  • prwoprwo Member Posts: 69
    @Proleric Exactly.
    Note: The mentioned thread describing a workaround to pass "return values" back from a script to the calling script is: Suggestion: Call a conditional script from within another script and evaluate its return value

    The same technique can be used to pass parameters from a script to the called script. Such a parameter could contain a reference to function to be executed by the called script. The main() of the called script would evaluete the parameter an execute the corresponding function.

    By the way: the latest version of AuroraExt contains an NSS preprocessor that can automatically generate such proc/func/event dispatcher scripts automatically.

    ... if the Aurora Toolset would separate the build system from the editing part such language extenders could be integrated rather easily by everyone.
  • squattingmonksquattingmonk Member Posts: 59
    edited April 2018
    prwo said:

    The same technique can be used to pass parameters from a script to the called script. Such a parameter could contain a reference to function to be executed by the called script. The main() of the called script would evaluete the parameter an execute the corresponding function.

    I do this in my scripts using some functions adapted from Memetic AI (see util_i_libraries.nss and util_i_library.nss).

Sign In or Register to comment.