Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Simon Geilfus

Member Since 16 Feb 2014
Offline Last Active Feb 11 2015 05:13 AM

Topics I've Started

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

18 December 2014 - 03:25 PM

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) )


Casting value objects.

14 December 2014 - 12:45 PM

Hi Andreas,

 

I have a quick question about polymorphic types.

 

I'm trying to register a series of functions that accepts a const reference to an abstract class and I'd love to find a way to make it work like in c++, being able to implicitly cast a Derived class to a const reference of the base class. Something like this :

 

class Base { // abstract with no public constructors
};
 
class DerivedA : public Base {
};
class DerivedB : public Base {
};
class DerivedC : public Base {
};
class DerivedD : public Base {
};
class DerivedE : public Base {
};
 
void someFunction( const DerivedA &obj );
 

My objects are registered as Values making it difficult I guess to find a solution to my problem. The first issue of course is that the Base class usually doesn't have a public default constructor, which trigger an error message when trying to register the type. I managed to make it run by registering the base type as POD (this is probably totally wrong). Secondly I added to the Derived classes an opImplConv operator. Everything compiles fine without any error messages, but somehow when converted and then passed to the function the object seems to be empty and usually make the application crash. 

 

I don't know if I've been really clear but if you have any advices other than re-implementing everything as Ref types I'd be really grateful.

 

Thanks in advance,

Simon.


Enum names collisions

03 December 2014 - 12:17 PM

Hi all,

 

I'm having some issue trying to register two enums that have the same name. One is in the global namespace and the other lives inside a class. I thought that I could register the second one by using a namespace but apparently I still got some name collision errors.

 

I just stumbled upon this thread and though this was fixed a long time ago, but I think the issue I'm having might still be related because I still have errors when one of the two enums is living in the global namespace. Here's an example.

 

The c++ enums I'd like to register:

 

enum {
    VALUE_A,
    VALUE_B
} Enum;
 
namespace enum_ns {
    enum {
        VALUE_A,
        VALUE_B
    } Enum;
}
 
And here's the code I'm trying to use to register the two enums.
 
engine->RegisterEnum( "Enum" );
engine->RegisterEnumValue( "Enum", "VALUE_A", Enum::VALUE_A );
engine->RegisterEnumValue( "Enum", "VALUE_B", Enum::VALUE_B );
 
engine->SetDefaultNamespace( "enum_ns" );
engine->RegisterEnum( "Enum" );
engine->RegisterEnumValue( "Enum", "VALUE_A", enum_ns::Enum::VALUE_A );
engine->RegisterEnumValue( "Enum", "VALUE_B", enum_ns::Enum::VALUE_B );
engine->SetDefaultNamespace( "" );
 
If I try to execute this I have this error message; And as Code:-13 suggests things that have already been registered I guess the scope of the Enum can't be solved correcly.
 

 (0, 0) : ERR  : Failed in call to function 'RegisterEnum' with 'Enum' (Code: -1)

 (0, 0) : ERR  : Failed in call to function 'RegisterEnumValue' with 'Enum' and 'VALUE_A' (Code: -13)

 (0, 0) : ERR  : Failed in call to function 'RegisterEnumValue' with 'Enum' and 'VALUE_B' (Code: -13)

 (0, 0) : ERR  : Invalid configuration. Verify the registered application interface.

 

The funny thing is that if I do it the other way around and register the non-global one first, I don't have any error message. Unfortunately in the application I'm working on I can't really predict in what order this is going to be registered, so I'm afraid this doesn't really help!

 

I'm not sure if I can think of an other alternative. Has someone already experienced this before? Am I doing something wrong?

 

Thanks a lot for any suggestions.

 

Simon.


opConv and opImplConv with bool

13 November 2014 - 07:04 AM

Hi,

 

I've got a lot of shared_ptr underneath my script objects and because of the new features of 2.29.2 I wanted to try registering the handy "operator bool" that allow testing shared_ptr like this :

if( myShared ){
} 

For some reason I can't make opConv or opImplConv to work for booleans. To make sure I wasn't doing something stupid on the c++ side, I tried doing the same in the script using the code provided in the documentation but without success.

 

It is an extremely useful feature of shared_ptr in c++ and I'd love to be able to expose the same feature for the classes I register to angelscript.

 

Here's what I tried in c++ :

engine->RegisterObjectMethod( "ObjectRef", "bool opImplConv() const", asMETHODPR(ObjectRef, operator bool, () const, bool ), asCALL_THISCALL ); 

and in Angelscript:

class MyObj
{
    double myValue;
    double opImplConv() const  { return myValue; }
    bool opImplConv() const  { return myValue > 0; }
    // bool opConv() const  { return myValue > 0; }
};

MyObj c;
c.myValue = 0.12345;

double cDouble 	= c; // this works
bool 	cBool	= c; // this doesn't and gives "MainSection (66, 16) : ERR  : Can't implicitly convert from 'MyObj&' to 'bool'."

Is this a limitation of the new conversion operator or am I missing something obvious?

 

Thanks in advance!

Simon.


Global variables and const argument by reference.

27 February 2014 - 04:19 PM

I'm still quite new to Angelscript and I have some trouble understanding what's going here.

 

I'm trying to pass a global variable in my script to a function that accept a const & parameter. Things seems to work as expected when I use a primitive or a class declared in the same script, but as soon as I'm trying to do this with a type I registered from the application I run directly into a failed assertion:

 

Assertion failed: (useVariable), function PerformFunctionCall, file [...]/angelscript/source/as_compiler.cpp, line 12833.

if( descr->returnType.IsObject() && !descr->returnType.IsReference() ){
		int returnOffset = 0;

		if( descr->DoesReturnOnStack() ){
			asASSERT( useVariable );

			// The variable was allocated before the function was called
			returnOffset = varOffset;
			ctx->type.SetVariable(descr->returnType, returnOffset, true);

			// The variable was initialized by the function, so we need to mark it as initialized here
			ctx->bc.ObjInfo(varOffset, asOBJ_INIT);
		}

It really seems to be related to the fact that the variable I'm trying to pass as a parameter is global and from a custom type. I tried doing the same inside a class and I don't have any problem. As explained above I don't have this issue with primitives or types created in the script, so my first guess would be that I'm doing something wrong with the registration. Which is probably the case as this is the first class I'm trying to register to AS.

 

I'm registering my type as asOBJ_VALUE and asOBJ_APP_CLASS_CDAK.

 

Here's an example code to illustrate what I'm doing.

float test1;
Vec2f test2; // Vec2f is the registered type

void main(){ // main is the function called 

    testFloat( test1 ); // works
    testVec2f( test2 ); // crash with "Assertion Failed"
}

void testFloat( const int &param ){}
void testVec2f( const Vec2f &in param ){}

// the two following work without surprise but I would really prefer
// keeping the const correctness of the class I register to AS
void stupidTestVec2f( const Vec2f param ){}
void stupidTest2Vec2f( Vec2f param ){}

I guess it is somehow related to the way I register my Vec2f type, but I can't seem to find the correct way and I've tried to find solution here without success.

 

As a side question, is "const T &in" the closest to what "const T &" means in c++?

 

Thanks!

Simon.


PARTNERS