Reverse callbacks – ShiVa Engine

Reverse callbacks

Nav Gupta: “This tutorial will allow you to call ShiVa functions directly from XCode. This allows us to complete the loop, getting data into our ShiVa app, and pushing data out of our ShiVa app over to xCode.”

Tutorial

For this tutorial, we are going to take a variable in ShiVa, pass it to xCode, then pass it back to ShiVa again with an increment to complete the loop. It’s a simple example of how to do hooks/callbacks forward and reverse.

ShiVa Steps

Create two handlers like these:

onSendNumber ( nNumber )
onReceiveNumber ( nNumber )

Fill onReceiveNumber with this code:

--------------------------------------------------------------------------------
function MainAI.onReceiveNumber ( nNumber )
--------------------------------------------------------------------------------
    log.message ( "Received Number: ", nNumber )
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------

Go into our onInit handler. Make it call the onSendNumber handler with a value of 5:

user.sendEvent ( application.getCurrentUser ( ), "MainAI", "onSendNumber", 5 )

Save and export your STK file for iOS/XCode.

XCode Steps

Open S3DEngine_Wrapper.cpp. Add the following variable (approx. line 29) to the variables list:

static int   nNumber = 0;

Go down to S3DEngine_iPhone_LoadPack (approx. line 200). Add the following event hook to the the EventHooks part at the bottom:

S3DClient_InstallCurrentUserEventHook ( "MainAI", "onSendNumber", onSendNumberCallback, NULL );

Create a new function above the previous one (approx. line 180) by copying and pasting the following function code:

void onSendNumberCallback ( 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::eTypeNumber )
      {
         nNumber = (int)pVariables[0].GetNumberValue() ;
      }
   }
}

Below that, create another function as follows (approx. line 240):

extern "C" bool onReceiveNumber ( int nNumber )
{
   // NOTE: The reverse hook/callback takes 4 inputs only, the last of which is an array of variables
   // Setup a basic array to hold variables (You can specificy whatever amount of variables)
   S3DX::AIVariable variables[1];
   // Define variables (Numbers are in FLOAT format, and so you must cast them!)
   // you may also use SetBooleanValue or SetStringValue
   variables[0].SetNumberValue((float)nNumber);
   // Actual REVERSE Hook/Callback code
   // Format is S3DClient_SendEventToCurrentUser ( AI, Handler, NUMBER_OF_ARGUMENTS, ARRAY_OF_ARGUMENTS )
   S3DClient_SendEventToCurrentUser ( "MainAI", "onReceiveNumber", 1, &variables );
   // Return 0
   return 0;
}

Go down to the bottom of the code and add the following function (approx. line 460):

extern "C" int S3DEngine_ShowNumber ( )
{
   return nNumber;
}

Save and open S3DEngine_Wrapper.h Add the following line of code to the very end (approx line 40):

extern int S3DEngine_ShowNumber ( );
extern bool onReceiveNumber ( );

Save and open S3DEngine_AppDelegate.m. Add the following line of code (approx line 33) to the applicationDidFinishLaunching function :

// Basically grabbing number from handler and passing it over to reverse hook/callback
onReceiveNumber ( S3DEngine_ShowNumber ( ) );

Save and run. You should see the following in your LOG output:

Received Number: 5

Troubleshooting

Always make sure to check for Lowercase/Uppercase and semicolons. Simple things like that can always break your code.




Need more answers?

  • slackBanner