Jump to content
  • Advertisement
Sign in to follow this  
Bismuth

Registering data pointed to by a level N pointer

This topic is 3170 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a quick question here. Suppose you have a level N pointer in c/c++ that points to some data type, let that be a char, an integer, a class, text, whatever. Example:
class A {
	int * var1;
	int * var2;
	int * var3;
};

class B {
	A * a;
};

class C {
	B * b;
}

C * c;
How do you register i.e. c->b->a->var2 in Angelscript, provided you only have the address of variable c and you know that the variable var2 exists in memory at the time of script execution? Bismuth

Share this post


Link to post
Share on other sites
Advertisement
You indeed have to write a function to access it.

With the new getters and setters you can register the function as a class member, and yet have the script access the variable as a member variable.

Example wrapper function and registration:


int get_var2(const C *c)
{
return *c->b->a->var2;
}

engine->RegisterObjectMethod("C", "int get_var2() const", asFUNCTION(get_var2), asCALL_CDECL_OBJLAST);


Example script accessing the member:


void func()
{
C c;
print(c.var2);
}


Regards,
Andreas

Share this post


Link to post
Share on other sites
Nice. And if I would like to register c->b->a->var2 as a global property, not as a class member? Does this method with getters and setters work for globals too?

Previously I used to register a level-1 pointer like this:
r = engine->RegisterGlobalProperty("int @var2", var2_pointer); assert( r >= 0 );

Then I tried to do the same with a higher level pointer:
r = engine->RegisterGlobalProperty("int @@@var2", var2_pointer_3); assert( r >= 0 );
And I got some configuration errors here. Not sure, if I was doing something wrong, but getters/setters should probably work just as fine.

Biz

Share this post


Link to post
Share on other sites
Getters/Setters for global properties are not yet supported, but I have plans to implement that too. The scripts would have to access the properties through the actual functions for now.

On the other hand, if the script is supposed to access the global property through the multi-level pointer directly, why not register the variable that the pointer actually points to? It would obviously only work if the pointer stays the same throughout the lifetime of the script engine.


r = engine->RegisterGlobalProperty("int var2", ***var2); assert( r >= 0 );


The script language doesn't have multi-level pointers, so you can't register those directly. Handles are only available for reference types, i.e. types that are allocated on the heap and has reference counting. A handle to a handle cannot be used, since the handle itself is not a reference type.

In a distant future I have plans to add support for pointers to the language again, but because these wouldn't be safe in a scripted environment they would be turned-off by default and should only be used for applications where the developer has full control over the code that is written (just as in a C++ application). For that reason this is of extremely low priority at the moment.

Regards,
Andreas

Share this post


Link to post
Share on other sites
Quote:
Original post by WitchLord
Getters/Setters for global properties are not yet supported, but I have plans to implement that too.
Do you have an estimated version number that will add support for this feature?

Quote:
Original post by WitchLordOn the other hand, if the script is supposed to access the global property through the multi-level pointer directly, why not register the variable that the pointer actually points to? It would obviously only work if the pointer stays the same throughout the lifetime of the script engine.
Well that's exactly the problem. It does change during runtime. I take it I'll have to use the approach of registering a function for now... not that I dislike this approach though, it actually gives me a chance to do some extra sanity checks.

Biz

Share this post


Link to post
Share on other sites
I can't make any promises, but I believe it would be a rather small change, and maybe I can fit it into the 2.18.0 release. That depends on how much time it takes to implement the other changes I have planned for 2.18.0.

Share this post


Link to post
Share on other sites
Could I ask for a bit more detailed example on registering getters and setters from C++? Particularly the proxy function part i.e. what parameters should the functions take, and registrations. I can't find any such info in the manual, or the test code, and I'm getting data corruption with my code that's based on your sample.

Share this post


Link to post
Share on other sites
The getters and setters are normal class methods, and should follow the same naming convention as that described here: Property accessors.

The get method should return the same type as the set method takes as parameter, otherwise the compiler won't consider the pair of methods as getter and setter for the same property.

The registration of the getters and setters should be done the same way as other methods. You can use either asCALL_THISCALL, asCALL_CDECL_OBJLAST, asCALL_CDECL_OBJFIRST, or asCALL_GENERIC for the calling convention.

It is a new feature, so there is the possibility that you've come across some bug in the library. Can you show me how you tried to do it?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!