Jump to content
  • Advertisement
Sign in to follow this  
Jedive

Script classes in arrays

This topic is 2037 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

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.

Share this post


Link to post
Share on other sites
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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!