Jump to content

  • Log In with Google      Sign In   
  • Create Account


Paril101

Member Since 25 Dec 2012
Offline Last Active Dec 27 2012 05:56 AM
-----

Topics I've Started

Remove global namespace accessor?

26 December 2012 - 09:06 AM

Hey there.

 

Another simple question I figure I'd place in another thread.

I have some scripts which interface highly out to C++. Now, I added a lot of types which the scripts need to use - however, the code is littered with the global namespace accessor. For instance:

 

namespace Default
{
	class DefaultRenderer : ::IBlockRenderer
	{
		void Render(::Block block, ::Point3i position)
		{
			::renderer.Draw(::Box().SetAll(::Point2i(0, 0), ::Colorb(255, 0, 255, 255)));
		}
	}
}

 

 

Is there any way I can get rid of these? I know it's because these are in a namespace, but, I use them to keep scripts separate and enforce that they be in their own namespace. With them it can't seem to resolve the global scope automatically.

 

Was this just a design choice?

 

-P


Member function chaining?

25 December 2012 - 03:10 PM

Hey there.
 
I'm just beginning to use AngelScript for my own project, and I must say it is brilliant. This is the first thing I've needed to get help on, as the documentation is extremely helpful.
 
Okay, so basically, here's my question: is it possible via AngelScript API to register a type that utilizes the well-known member function chaining method? An example of such a class is below:
 
 

class ChainMe
{
public:
	int X;

	ChainMe() :
		X(0)
	{
	}

	ChainMe &Increase(const int &v)
	{
		X += v;
		return *this;
	}
};

 
How would one register such a type in AngelScript so that you may use it from AngelScript as you would from C++, like:

 

ChainMe().Increase(5).Increase(15).Increase(25)

 

My first (failed) attempt at doing this involved this very basic implementation:

 

 

 

engine->RegisterObjectType("ChainMe", sizeof(ChainMe), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_C);
engine->RegisterObjectMethod("ChainMe", "ChainMe &Increase(const int &in)", asMETHOD(ChainMe, Increase), asCALL_THISCALL);

(This also included a constructor but they aren't important for this sample)

The engine runs the constructor, as expected, then runs Increase, as expected.. however, following that, it calls the constructor again, then errors with "GC cannot free an object of type '_builtin_objecttype_', it is kept alive by the application".

 

EDIT: I managed to get the error to go away by adding asOBJ_NOHANDLE to the type flags. I think that this was the problem. I'm still getting a two-constructor call when I try passing the return value of the functions to another function via a reference (ie, "const ChainMe &in")

 

EDIT2: Via adding one extra function (which I love about AngelScript), I have figured out that the "extra constructor" being called is the copy constructor. Fair enough, but, why is it copying when it only needs to pass a reference? Is it because it is an r-value reference?

 

Thanks in advance,

-P


PARTNERS