Script classes in arrays

Started by
0 comments, last by WitchLord 10 years ago

Sorry if this has been asked before, I could not find a way to search only in the AngelScript section of GameDev forums. I used AngelScript in a project many years ago, and now I have come back to it, although I am pretty new and unexperienced with it.

I have embeded AngelScript in my application, with the array and stdstring addons and a simple Print function, and I have made one small test:


void Main() {
	string[] arr;
	arr.insertLast("Hello");
	arr.insertLast(" ");
	arr.insertLast("world!");
	arr.insertLast("\n");

	for ( uint32 i = 0; i < arr.length; i++ )
		Print(arr[i]);
}

This example compiles without any issues and prints "Hello world!" as expected. The problem comes when instead of the "string" class, I create an array with a class defined in the scripts. To see this problem, let's change the code to this:


class S {
	S(string s) {
		this.s = s;
	}
	
	string s;
}

void Main() {
	S[] arr;
	arr.insertLast(S("Hello"));
	arr.insertLast(S(" "));
	arr.insertLast(S("world!"));
	arr.insertLast(S("\n"));

	for ( uint32 i = 0; i < arr.length; i++ )
		Print(arr[i].s);
}

The compiler reports the following messages:


hello.as (9, 1) INFO: Compiling void Main()
hello.as (11, 5) WARNING: 'arr' is not initialized.
hello.as (11, 5) ERROR: Illegal operation on '<unknown>'
hello.as (12, 5) ERROR: Illegal operation on '<unknown>'
hello.as (13, 5) ERROR: Illegal operation on '<unknown>'
hello.as (14, 5) ERROR: Illegal operation on '<unknown>'
hello.as (16, 29) ERROR: 'length' is not a member of '<unknown>'
hello.as (17, 12) ERROR: Type '<unknown>' doesn't support the indexing operator

If I change the declaration of the array from "S[] arr;" to "array<S> arr;" it also gives the following error:


hello.as (10, 8) ERROR: Can't instanciate template 'array' with subtype 'S'

May someone with more experience tell me what I am doing wrong?

Thanks in advance, and congratulations Andreas for creating such a nice and powerful scripting language.

Advertisement

The script class needs a default constructor and assignment operator in order for the array to be able instantiate the elements.

class S {
  S() {} // default constructor
  S(string s) {
    this.s = s;
  }
 
  S &opAssign(const S &in other) { s = other.s; } // assignment operator
 
  string s;
}

Of course, the compiler should give better error messages to tell you this so you wouldn't have to guess on what the problem is. I'll look into improving this.

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