Callbacks 101 – ShiVa Engine

Callbacks 101

Nav Gupta: “This tutorial is similar to my previous hooks and callbacks tutorial for Android as some parts are identical so we will be starting there. This is an Intermediate/Advanced tutorial for those more intermediate/advanced users who want to expand ShiVa apps to do even more! This will open you up to the world of true customization with ShiVa!”

ShiVa Steps

Follow step 1 and 2 from the previous Hooks and Callbacks tutorial for Android

ShiVa MAC UAT

Instead of opening ShiVa UAT on Windows, you will be copying your STK file to the Mac system and compiling the ShiVa STK using ShiVa UAT for MAC.

xCode Steps

First, make sure LINE NUMBERS are shown in xCode: Click on File->Preferences->Text Editing->Show: Line numbers
Under the Classes folder (On left window pane), select the S3DEngine_Wrapper.cpp file. Go to Line 170 and type in the following code (Change the handler name and function name to your own that you specified in ShiVa 3D Steps above):
Example 1:

S3DClient_InstallCurrentUserEventHook  ( "MainAI", "YOUR_HANDLER_NAME", YOUR_HANDLER_NAMECallback, NULL);

Example 2:

 //Using previous example the hook would be called
 S3DClient_InstallCurrentUserEventHook  ( "MainAI", "onMyTestCallWithVar", onMyTestCallWithVarCallback, NULL);

Go to line 137 above void AdMobSetVisibleCallback and paste the following (Change the handler name again accordingly):
Example 1:

 void YOUR_HANDLER_NAMECallback ( unsigned char _iArgumentCount, const void *_pArguments, void *_pUserData )
 {
     if ( _pArguments && ( _iArgumentCount == 1 ) )
     {
         const S3DX::AIVariable *pVariables = (const S3DX::AIVariable *)_pArguments ;
         if ( pVariables[0].GetType ( ) == S3DX::AIVariable::eTypeString )
         {
             strncpy ( sYOUR_STRING, pVariables[0].GetStringValue ( ), 254 ) ;
         }
     }
 }

Example 2:

 // Once again using the example from the first hooks and callbacks tutorial
 void onMyTestCallWithVarCallback ( unsigned char _iArgumentCount, const void *_pArguments, void *_pUserData )
 {
     if ( _pArguments && ( _iArgumentCount == 1 ) )
     {
         const S3DX::AIVariable *pVariables = (const S3DX::AIVariable *)_pArguments ;
         if ( pVariables[0].GetType ( ) == S3DX::AIVariable::eTypeString )
         {
             strncpy ( sYOUR_STRING, pVariables[0].GetStringValue ( ), 254 ) ;
         }
     }
 }

Go to line 29 and add the following (Once again change the variable to your own but make sure you replace the same variable in the function above as well):

 static char sYOUR_STRING [255] = "" ;

Go to approx. line 356 and type in the following:

 extern "C" const char *S3DEngine_sYOUR_STRING ( )
 {
     return sYOUR_STRING;
 }

Save the file and close it.
Now open up S3DEngine_Wrapper.h and go to line 31 approx and type in the following:

 extern const char *S3DEngine_sYOUR_STRING ( );

Save the file and close it.
Now ANYWHERE you want to get the string information you passed from ShiVa to iOS, you simply write the function name itself as follows:

 S3DEngine_sYOUR_STRING ( );

Most of the work happens in the Classes folder within the various .m files. For example, the main game loop is contained in S3DEngine_EAGLView.m.
If you go to line 258 you will find the function and if you go down inside the function to line 356 you can write an if statement (which will be executed at every clock tick just like an onEnterFrame is) as follows using your

 if ( S3DEngine_sYOUR_STRING ( ) )
 {
     // Show passed information in DEBUG log window
     NSLog ( S3DEngine_sYOUR_STRING ( ) );
 }

Using the same method above, you can pass numbers, strings and booleans from ShiVa to iOS and do various things with it.

Conclusion

This is a very basic overview of hooks and callbacks in iOS from ShiVa 3D. As you start getting into advanced iOS functionality, knowing this will help you quite a bit!




Need more answers?

  • slackBanner