Topic: [scripting suggestion]Passing variables to objects

The idea:
In traditional programming, functions can be programmed in such a way that you can pass variables to them via their function call.  For example, if you said:

var global = 1;

function foo(var_1, var_2) {
   global = var_1*var_2;
}

function main() {
   x = 1;
   y = 2;

   foo(x, y);
   alert("Global = "+global);
}

In the above Javascript example, the variables x and y were passed to the function foo where they were multiplied together and stored in the global variable global.  I would like to be able to structure objects in a similar fashion.

Why it is needed:
Global variables are great for passing information, but this can get tricky because it requires very careful coding to ensure that access to certain globals is restricted to very specific objects and it prevents quick calling of an object using different values because they read the same global and altering it mid-execution can cause incorrect object behavior.  Passing variables using the above method would reduce the need for globals in many situations where they are currently being used, and would allow for a more library like code base where the object will only accept data in a certain form, as it is outlined in it's header.

How it would work:
There are two ways to impliment this, either have it so that each object requires a form layout, for example your object would change from this:

object .foobar

Into this:

object .foobar()

Or make it so that if there are no () it is automatically assumed that there are no arguments, this method would make it backward compatible with the current script code base.

What I am willing to contribute to make this a reality:
I am more than willing to write the Sunshine Paradise boss as specified in the wiki, and to document how I did it in the 'Making a Custom Boss' section so that others can repeat it and make bosses of their own.

Thank you for considering my idea, have a great day smile.

Last edited by lunarrush (2012-05-11 21:07:20)

If I knew then what I know now I'd tell you that the story's true.  Cause whatever you do, it comes back to you.  -Slaughter, Burning Bridges

Re: [scripting suggestion]Passing variables to objects

hey lunarrush,

I completely understand the importance of what you're asking. In fact, a similar feature is already implemented for quite some time now - and we already use it in some of the scripts from Open Surge. Whenever you call change_closest_object_state, change_parent_state or change_child_state, internally, the code from the called object will be executed immediately, and therefore before the code from the callee is resumed. That's similar to a subroutine call. You don't need to bother with global naming at all.

object "test"
{
    ...
    state "foo" {
        let $_fun_param1=2
        let $_fun_param2=5
        change_closest_object_state "Calculator" "@add"
        textout default 0 0 $_fun_value // this will show 7
    }
}

object "Calculator"
{
    requires 0.2.0
    always_active // so that the object becomes responsive at all times
    ...
    state "@add" {
        let $x=$_fun_param1
        let $y=$_fun_param2
        let $_fun_value=$x+$y
        return_to_previous_state // so that this state behaves like a function
    }
}

The code from the calculator will be executed BEFORE the code from textout. After the 'subroutine' returns, $_fun_param1 and $_fun_param2 have no use. The name of the globals are irrelevant, but it's a good idea to adopt a convention such as this one.

For further studies, please take a look at this page: http://opensnc.sourceforge.net/wiki/ind … Components .