Advertisement

Problem with array

Started by May 30, 2007 07:08 AM
6 comments, last by mckracken 17 years, 6 months ago
Hi there!! I have a problem creating an array of a class that i've defined in the script. Here is an example:

class Shot : Actor {
	Sprite _sprite;

	float _vx;
	float _vy;

	void SetVisible(bool visible) {
		_sprite.SetVisible(visible);
	}

.
.
.
}
Then, I have another class that declares an array of Shot type:

class Ship : Actor {
	Shot[]		_shots;
.
.
.
}
But when I do something like _shots.SetVisible(false); i have an "Out of range" error. So, i've declared the array as Shot[] _shots(10); but I have an "Expected ;" error in that line. Is it an AngelScript bug or is it something i'm doing wrong? By the way, thanks a lot for this fantastic and FAST script engine. ;) See you soon!
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
I've solved it by declaring:
Shot[] _shots;


and then resizing it:
_shots.resize(SHOTS_COUNT);


But here is another question: is it possible to create an object handles array? Something like Shot@[] _shots; so that i could make something like:
Shot@ shot = _shots[10];_shots[10] = _shots[12];_shots[12] = shot;


Thanks!
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
Advertisement
It's not permitted to define the size of the member array in the class declaration. That's why you get the Expected ; when declaring Shot[] _shots(10);

Yes, you can have arrays of handles. You declare them just as you imagined: Shot@[] _shots;

Your code snippet will do object assignments. I suspect you meant:

Shot @shot = @_shots[10];@_shots[10] = @_shots[12];@_shots[12] = @shot;


which will only swap the handles.

Regards,
Andreas

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

Thanks! ;)

Another question: is there a way to declare an instance in a class of the same type of that class?

Something like:

class Asteroid {Asteroid[] _asteroid;}


Because when I do this, I get an error. Is it a bug?

Thanks! :)
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
What error message do you get?

You cannot declare an instance of the class type as a member of the same class type. That would give an infinite recursion when trying to instanciate the class. The same goes for the array of the type, though theoretically an empty array doesn't instanciate the class type, but if your constructor resizes the array it will and again causing the infinite recursion.

You can however declare a handle to the class type as a member of the class type. Example:

class Asteroid {  Asteroid@[] _asteroid;}


Regards,
Andreas

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

Thanks!

But I couldn't fill the array with instances. I did something like:
class Asteroid {Asteorid@[] _asteroid;bool Initialize(int type) {switch (type) {case 1:_asteroid.resize(2);Asteroid _child1, _child2;_asteroid[0] = @_child1;_asteroid[1] = @_child2;break;...}}}


What can I do to work around this problem?

[edit]
The error message is "Expected '('" in "Asteroid _child1, _child2;".
[/edit]
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8
Advertisement
I don't remember how I implemented the switch case, but I believe you need to add the declarations inside a statement block (just like in C++). Example:

switch (type) {  case 1:   {    _asteroid.resize(2);    Asteroid _child1, _child2;    _asteroid[0] = @_child1;    _asteroid[1] = @_child2;  }  break;}

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

Perfect! It works!!

When this example i'm working on is finished, I'll show you. ;)

Thanks a lot!! ;)
=====================================Regards,Juan Pablo (McKrackeN) Bettini Psychoban Official Site:http://www.psychoban.comPsychoban on iTunes App Store:http://itunes.apple.com/us/app/psychoban/id378692853?mt=8

This topic is closed to new replies.

Advertisement