Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Functions are visible to SqPlus, but not to each other?

Last post 02-23-2006, 2:00 PM by John Schultz. 4 replies.
Sort Posts: Previous Next
  •  02-21-2006, 4:03 PM 629

    Hmm [^o)] Functions are visible to SqPlus, but not to each other?

    If anyone knows what's wrong with this setup, please let me know :-)

    I have the .nut file and c++ program below, which produces the output under both of them.

    The problem is that Squirrel functions seem visible to SqPlus always, and visible to other Squirrel functions only sometimes.

    The first SqPlus function execution calls the function 'receiveEffect', which from the output, gets called. Ok so far. Note that receiveEffect is valid.

    The second SqPlus function execution calls the function 'returnFunction', which also seems to get called from the output. Still ok. Note that one function successfully returned another: a function called onInstanceAdded.

    That SquirrelObject is then made into a SqPlus function and called, calling onInstanceAdded. From the output, this function is also called. So far so good.

    But then, it chokes on trying to call receiveEffect, saying that index does not exist.

    I know it does, because I successfully called it a few lines earlier. The scope also shouldn't be a problem because returnFunction was able to see onInstanceAdded.

    Is there something that I'm missing?

    --Ben


    ///////////////////////////////////////////////////////
    NUT FILE
    ///////////////////////////////////////////////////////
    print("GenericPart.nut running");

    function receiveEffect(instance) {
        print("Executing receiveEffect");
        return true;
    }

    function onInstanceAdded(instance) {
        print("Executing onInstanceAdded");
       
        receiveEffect(instance);
       
        return true;
    }

    function returnFunction() {
        print("Executing returnFunction");
        return onInstanceAdded;
    }

    ///////////////////////////////////////////////////////
    CPP PROGRAM
    ///////////////////////////////////////////////////////
        // Load the script from file using Ogre's resource system
        ScriptObjectPtr script = ScriptManager::getSingleton().load("GenericPart.nut");

        // Make a namespace for this script
        SquirrelObject scriptNamespace = SquirrelVM::CreateTable();

        // Run the script into the namespace
        SquirrelVM::RunScript(script->getScript(), &scriptNamespace);

        // Run the function called 'receiveEffect' to show that it works.
        SqPlus::SquirrelFunction(scriptNamespace, "receiveEffect")((int)5);

        // Get a reference to the 'returnFunction' function
        SqPlus::SquirrelFunction scriptFunction(scriptNamespace, "returnFunction");

        // Execute it and catch the return value, which should be the function 'onInstanceAdded'
        SquirrelObject returnObj = scriptFunction();

        // Get a reference to that function
        SqPlus::SquirrelFunction scriptFunction2(scriptNamespace, returnObj);

        // Run it
        scriptFunction2(SE_Part());


    ///////////////////////////////////////////////////////
    OUTPUT
    ///////////////////////////////////////////////////////
    18:44:11: ScriptObject: Loading GenericPart.nut.
    18:44:12: GenericPart.nut running
    18:44:13: Executing receiveEffect
    18:44:13: Executing returnFunction
    18:44:41: Executing onInstanceAdded
    18:44:41:
    AN ERROR HAS OCCURED [the index 'receiveEffect' does not exist]

    18:44:41:
    CALLSTACK

    18:44:41: *FUNCTION [onInstanceAdded()] GenericPart.nut line [61]

    18:44:41:
    LOCALS

    18:44:41: [instance] INSTANCE

    18:44:41: [this] TABLE
  •  02-22-2006, 9:34 AM 630 in reply to 629

    Idea [I] Incorrect error message

    Problem solved :-)

    Aparently SqPlus mangles the error code in this case. The actual error was that Push(SE_Part) was failing in the Squirrel macro DECLARE_INSTANCE_TYPE, but the message displayed was way off from what it should have been.

    Changing the final line of C++ code to:
        // Run it
        scriptFunction2(&SE_Part());

    fixed the problem entirely, all functions are getting called and can all see one another. As an aside, this is because my SE_Part doesn't copy well as it was designed to be an interface class, and makes the macro function fail:
    Push(HSQUIRRELVM v,TYPE & value)
    where the other macro function
    Push(HSQUIRRELVM v,TYPE * value)
    works fine.

    I'm not sure why Squirrel reported "
    the index 'receiveEffect' does not exist" when it should have reported "Push(): could not create INSTANCE copy (check registration name)" like the macro says, but better to report it so other people don't run into the same problem :-)

    --Ben
  •  02-22-2006, 11:45 AM 632 in reply to 630

    Re: Incorrect error message

     Project5 wrote:
    Problem solved :-)
    I'm not sure why Squirrel reported "the index 'receiveEffect' does not exist" when it should have reported "Push(): could not create INSTANCE copy (check registration name)" like the macro says, but better to report it so other people don't run into the same problem :-)


    Good to hear you figured it out. Error propagation between Squirrel and SqPlus could be improved. One of the issues has to do with throwing exceptions in SqPlus and storing errors internally in (lower-level) Squirrel (where no exception will be thrown).

    Until a cleanup/fix can be implemented, you might want to update the Wiki (I just added  an FAQ section for this purpose: http://wiki.squirrel-lang.org/default.aspx/SquirrelWiki/SqPlusFAQ.html ) and add an entry with an appropriate title and an explanation of how you worked around/understood the issue(s). I believe there are comments in the sqplus.h header and/or the testSqPlus2.cpp source file that explains possible issues with passing items by reference (which end up being copied/allocate memory) as opposed to passing their address (as a pointer).
  •  02-23-2006, 1:40 PM 636 in reply to 632

    Re: Incorrect error message

    Once I get a decent html editor I'll post what to look for if you get an invalid index error on the wiki :-)

    --Ben
  •  02-23-2006, 2:00 PM 637 in reply to 636

    Re: Incorrect error message

     Project5 wrote:
    Once I get a decent html editor I'll post what to look for if you get an invalid index error on the wiki :-)

    --Ben


    I'm not sure you need an HTML editor to post in the Wiki (it has its own format/syntax). Just double click the page to start editing (online help is on the right pane).
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems