Advertisement

'set' property accessor for components/properties of a value-type object

Started by May 20, 2021 09:48 PM
2 comments, last by ChillPenguin 3 years, 6 months ago

Hi everybody,

I've got an Angelscript class with get/set accessors. All the documentation uses integers to demonstrate this, but I'm using a Vector3 value-type object.

shared abstract class Actor
{
	private Actor_cpp@ cpp_obj;
	
	void Update() {}	
	



	Vector3 local_position
	{
		get const { return cpp_obj.GetLocalPosition(); }
		set { cpp_obj.SetLocalPosition(value); }
	}
}

For the most part it's working fine, it's possible to get the local position, change the position, and then set it like so:

Vector3 pos = local_position;
pos.x = 0.0f;
local_position = pos;

The problem comes with setting the individual components of the vector - for example,

local_position.x = 0.0f;

Will give an error when compiling that script:

ERR  : Expression is not an l-value

Which makes sense to me - I've not given those individual components set functions. The problem is that I don't know how to declare the set function. It can't be a get/set in the Vector3 class since they don't necessarily belong to an Actor, and since the documentation only uses integers to demonstrate get/set accessors I don't know what a setter on the individual components inside the Actor class would look like, if such a thing is possible.

It's not the end of the world if this isn't possible. As I said, it is possible to change the position by creating a Vector3, modifying that and using it for the setter, but writing scripts would be a lot nicer if that wasn't necessary.

None

The local_position get accessor returns the Vector3 object by value, i.e. a copy. Even if it was allowed, any changes to the returned object wouldn't be reflected on the cpp_obj's location.

What you can do is to write accessors like this:

float local_position_x { 
  get const { return cpp_obj.GetLocalPosition().x; } 
  set { Vector3 t = cpp_obj.GetLocalPosition(); t.x = value; cpp_obj.SetLocalPosition(t); } 
}

and then use it as:

local_position_x = 0.0f;

It is obviously not exactly what you want, and it is definitely not efficient when you want to modify all members of the vector, but it should work.

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

Advertisement

It's not exactly what I was thinking of but I'm not picky. This looks like it'll do what I need it to - just be a shorter, less cumbersome way of setting a single member. I'm not concerned with the efficiency of modifying all members as the local_position set accessor already does that. Thanks for the information and advice!

None

This topic is closed to new replies.

Advertisement