Callbacks (sample) – ShiVa Engine

Callbacks (sample)

Nav Gupta: “Note that this tutorials draws heavily from the previos one on Hooks and Callbacks, so if you are not familiar with those concepts, click here and start reading. I’m also assuming that some of the basic ShiVa things like creating a HUD button and image are already things you can do, so I won’t go into detail on those points. Also I am assuming that your main user AI is called “MainAI” and that you’ve already setup the basics to get a program working (Ex. Dragging user AI to Game Editor, creating a basic HUD, loading an instance of a HUD via onInit, etc.). As I said, this tutorial is for more Intermediate/Advanced ShiVa devs so if you dont know how to do the basic setup, please refer to the wiki or various other tutorials available at this time.”

ShiVa Steps

ShiVa Code

  1. Create a HUD item that holds some image just for testing. Let’s call it lbl_testimage. Inside this image, put any image you may have on file from your computer.
  2. Create a HUD button and connect it via code using CopyTagToRegister and SendEventToUser.
  3. Inside the button code put the following:
    application.saveCurrentUserViewportToTexture ( "email.jpg", 1024, 600 )
    cache.sendFile ( "email.jpg", "file://"..system.getHomeDirectory ( ).."/Documents/email.jpg" )
    local sFileLocation = "file://"..system.getHomeDirectory ( ).."/Documents/email.jpg"
    user.sendEvent ( this.getUser ( ), "MainAI", "onSendScreenshot", sFileLocation )
    log.message ( " Button Pressed " )
  4. Create an empty handler called onSendScreenshot which takes in a STRING argument. This will have no code in it but will be used when we go to eclipse as a hook. It should look like this:
    --  Handler.......... : onSendScreenshot
    --  Author........... :
    --  Description...... :
    function MainAI.onSendScreenshot ( sFileLocation )
       -- Write your code here, using 'this' as current AI instance.
  5. If you save and run the program now, you should see in your log reporter “Button Pressed” whenever you click on the HUD button you made. If you do, then all is well. EXPORT your program as an STK file from the Game Editor and we will move on to the next step which is the UAT.


  1. Select Android as your target
  2. Do all the basic setup for step 1 (icons, stk file, etc.)
  3. In Step 2, select Project instead of APK file and then enter the rest of the details in.
  4. In Step 3, select Development and select the options you need (such as Network, etc.). We will be exporting to C: to make all things easy. You can however export it to whatever folder you wish, just make sure there are NO spaces in the location (ex. “C:\Users\My Name Is Game Scorpion Inc\My Documents” is bad because there are spaces in the folder names. Android will fail when building if you have spaces. To keep it simple for that reason I work directly in the C:
  5. Once completed, hit build and you should have a Zip file built for you and placed in the folder location (In our example, C:)

Eclipse Hooks and Callbacks

  1. Open Eclipse and create a new Java file (New->Java Project)
  2. Name your project and hit finish
  3. Import the external files for the jni directory (As per the hooks/callbacks tutorial)
  4. In the C++ file, create the following hook (Approx. line 660):
    S3DClient_InstallCurrentUserEventHook ( "MainAI", "onSendScreenshot", onSendEmailImageCPP, NULL );

    and then the following callback function for onSendEmailImageCPP hook (approx. line 65) (NOTE: MAKE SURE TO EDIT THE LOCATION OF THE JAVA FILE IN THE FUNCTION BELOW!):

    void onSendEmailImageCPP ( unsigned char _iArgumentCount, const void *_pArguments, void *_pUserData )
        // Get input filelocation as a string
        if ( _pArguments && ( _iArgumentCount == 1 ) )
            const S3DX::AIVariable *pVariables = (const S3DX::AIVariable *)_pArguments ;
            if ( pVariables[0].GetType ( ) == S3DX::AIVariable::eTypeString )
                strncpy ( YOURSTRING, pVariables[0].GetStringValue ( ), 254 ) ;
        if ( pJavaVM )
            JNIEnv *pEnv ;
            if    ( pJavaVM->GetEnv ( (void**) &pEnv, JNI_VERSION_1_4 ) >= 0 )
                jclass pClass  = pEnv->FindClass ( "com/yourcompany/yourappnamealllowercase/YourAppNameCaseSensitive" );
                if   ( pClass != 0 )
                    jmethodID pMethod = pEnv->GetStaticMethodID ( pClass, "onSendEmailWithImageJAVA", "(Ljava/lang/String;)I");
                    if ( pMethod )
                        int dummy = pEnv->CallStaticIntMethod ( pClass, pMethod, pEnv->NewStringUTF ( YOURSTRING ) ) ;

    And finally make sure we create the variable called YOURSTRING which is a 255 length string at the top of the CPP file (approx. line 46) before the static bool bVibrate global var:

    static char        YOURSTRING [255]             = "" ;

    And save the file.

  5. Once you have setup your calls, go to your Java file and we will place the JAVA code. In the Java File, before the function public void onLowMemory ( ) (approx. line 500) simply paste the following code:
    public static int onSendEmailWithImageJAVA ( String sBody )
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse( sBody ) );
        oThis.startActivity(Intent.createChooser(emailIntent, "Share:"));
        return 0;

    The above code is the actual major code that calls android and tells it to bring up a chooser box. The chooser box displays a list of apps and programs available to share the selected image with. For example, the chooser may display EMAIL, Facebook, Twitter, Digg, etc. The user can then select what their preference is and use it. NOT ALL APPS WILL FUNCTION WITH IMAGES CORRECTLY! Each app handles images differently IF AT ALL. So if its just plain text, you should be fine (Such as wanting to have a tweet sent). But if it’s an image, as is our case in this tutorial, each app MAY or MAY NOT accept an image so you will just end up having the other program open with an empty message.

  6. Save the file and do an ANT build and compile and you should be able to test it out.

Need more answers?

  • slackBanner