Sending a pointer to a Primitive from a script to the application

Started by
2 comments, last by WitchLord 9 years, 4 months ago

Hi all,

I'm looking for a way to register some c++ functions and objects that use pointers to manipulate data over time (think UI or animation code). Something along this lines:


void createToggle( const string &name, bool *ptr );
void createSlider( const string &name, float *ptr );

void animateValue( float *ptr, float start, float end, float time );

I've been exploring the generic convention api hoping to find something to help me and had a look on the forum, but I can't figure out how to do it.

So let's say that I have this script:


float someGlobalValue;

void main(){
   addSlider( "value", someGlobalValue );
}

Is there any way I can register the function and obtain the right pointer from the c++ side? Obviously getting the address using asIScriptGeneric isn't going to work (or maybe I'm missing something). And as far as I know the only way to get this variable address would be to use something like this:


int index = module->GetGlobalVarIndexByName( name.c_str() );
if( index >= 0 ){
   return mod->GetAddressOfGlobalVar( index );
}

I'm totally fine with limiting the system to work only with global variables, if it makes the whole thing easier, but I can't find a way to recover the name of the argument either (from what I can tell asIScriptFunction::GetParam returns the name of the argument as it is in the declaration, not the one passed to the function).

Any advice would be greatly appreciated! (and a solution that could work regardless of the type of the object would be totally amazing (primitive, values and references) )

Advertisement

You could use the variable parameter type: http://www.angelcode.com/angelscript/sdk/docs/manual/doc_adv_var_type.html

You'll get references to the given variables, you can check the type of the argument, though figuring out whether it's a global or not can be tricky.

Wouldn't this return an address pointing to the reference and not to the object itself?

It seems that if I print the void* ref from the variable parameter type function it gives me a different address than if I query directly the address using module->GetAddressOfGlobalVar( index );

I might be wrong but it looks like it's doing pretty much the same thing which is giving me a pointer that will not be valid as soon as we are out of the scope of the function. What I'm looking for is a pointer that can outlive that scope, so I can change the value later on.

You cannot send a pointer to the actual variable to a function like you wanted above. It goes against the principles of safe references (AngelScript is not like C++ that can rely on the programmer to do the right thing. This is why pointers doesn't exist in AngelScript).

Instead I recommend you refer to the variable by name, and then use the SDK interface to look up the correct variable to bind the GUI control to. For example:

In the script:


float someGlobalValue;

void main(){
   addSlider( "value", "someGlobalValue" );
}
In the application:


extern asIScriptEngine *engine;
void addSlider(const string &label, const string &variable)
{
  // Find the global variable that should be bound to the control using the name
  asIScriptModule *mod = engine->GetModule("module");
  int varIdx = mod->GetGlobalVarIndexByName(variable.c_str());
  if( varIdx < 0 )
  {
    // The variable doesn't exist
    return;
  }

  // Determine if the type of the variable is compatible with the the slider control
  int typeId = 0;
  bool isConst = false;
  mod->GetGlobalVar(varIdx, 0, 0, &typeId, &isConst);
  if( isConst || typeId != asTYPEID_INT32 )
  {
    // The slider cannot be created for this variable, as the variable is not compatible
    return;
  }
  
  // Get the address of the global variable, so the slider can be bound to it for updates
  void *addr = mod->GetAddressOfGlobalVar(varIdx);

  // Create the GUI control and bind the variable to it
  ...
}

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement