SDK Question Defining animations in a Array

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,865
Reaction score
4
Points
0
Location
San Diego
So I have a bunch of almost identical but seperate animations (Switches and displays in a VC)

Rather than go through and define 18 MGROUP_TRANSLATEs and 49 MGROUP_ROTATESs individually I thought I'd use an array.

Code:
	for (int i = 0; i < 18; i++)
	{
		static UINT NeedleGroups[i] = VC_GRP_Panel_Needle + i;
		static MGROUP_TRANSLATE Needle[i] (
			4,
			&NeedleGroups[i], 1,					
			NeedlePos[i],						
			_V( 0, sin(RAD*8)*0.03, cos(RAD*8)*0.03),	
		);
		anim_TapeGuage[i] = CreateAnimation(0.00);
		AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, &Needle[i]);
	}

Apperently this isn't allowed

So is there a way to achieve this same effect or am I just going to have to bite the bullet and do it all by hand ?
 
Last edited:
Shouldn't
Code:
AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, &Needle[i]);

Be
Code:
AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, Needle[i]);
 
nope, still won't compile.

It's something to do with having an [] inside a static I think
 
What's the compiler error code / message?
 
Code:
1>c:\users\hlynkacg\documents\visual studio 2010\projects\lunarlanders\lemvessel.cpp(205): error C2057: expected constant expression
1>c:\users\hlynkacg\documents\visual studio 2010\projects\lunarlanders\lemvessel.cpp(205): error C2466: cannot allocate an array of constant size 0
1>c:\users\hlynkacg\documents\visual studio 2010\projects\lunarlanders\lemvessel.cpp(205): error C2440: 'initializing' : cannot convert from 'int' to 'UINT []'

"MGROUP_TRANSLATE Needle" is the line getting flagged.
 
When declaring an array you need to give a constant size like :
MGROUP_TRANSLATE Needle[18]

You can only use a i instead of 18 for the size, when dynamically allocating the array on the heap(IIRC),

MGROUP_TRANSLATE *pNeedle = new MGROUP_TRANSLATE;

MGROUP_TRANSLATE is just a simple C data structure.

Its also important to understand why the MGROUP_TRANSLATE is declared as static. Its to ensure that MGROUP_TRANSLATE stays in memory when the function completes (to put it simply, look up the static keyword). Orbiter needs the animation settings to persist throughout the duration of the sim.
But you can get the same effect by dynamically allocating a bunch of MGROUP_TRANSLATE too :) preferably in the constructor and then getting rid of them in the destructor of your class.

Maybe try this :

Code:
static UINT NeedleGroups[18];
static MGROUP_TRANSLATE Needle[18];

for (int i = 0; i < 18; i++)
{
	NeedleGroups[i] = VC_GRP_Panel_Needle + i;
	Needle[i].abc = ....
        Needle[i].xyz = ....
		
	anim_TapeGuage[i] = CreateAnimation(0.00);
	AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, Needle[i]);
}

Something similar....I havent tested this code :P
 
Last edited:
When declaring an array you need to give a constant size like :
MGROUP_TRANSLATE Needle[18]

You can only use a i instead of 18 for the size, when dynamically allocating the array on the heap(IIRC),

I know this and already have declared as a class variable along side the animation.

but what is
Code:
MGROUP_TRANSLATE *pNeedle = new MGROUP_TRANSLATE[i];
 
but what is
Code:
MGROUP_TRANSLATE *pNeedle = new MGROUP_TRANSLATE[i];
An MGROUP_TRANSLATE array with 'i' elements allocated on heap (you use it when you don't know the number of elements of the array during the compile time, but only on runtime), pointed by pNeedle pointer. You need to 'delete [] pNeedle' to deallocate the array when it's no longer used.

In your case, the array has always 18 elements, so you don't need to use the dynamic allocation.
 
Ok, the VC still isn't compiling.

Can I get anotherset of eyes on this cause I really want to have this working by tomorrow. (tuesday)

Code:
static UINT NeedleGroups[18];
static UINT Needle = new MGROUP_TRANSLATE[i];

	for (int i = 0; i < 18; i++)
	{
		NeedleGroups[i] = VC_GRP_Panel_Needle + i;
		Needle[i] (
			4,
			&NeedleGroups[i], 1,					
			ADI_Center,						
			_V( 0, sin(RAD*8), cos(RAD*8)),	
		);
		anim_TapeGuage[i] = CreateAnimation(0.00);
		AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, &Needle[i]);
	}	
}

Compiler error is...

Code:
1>c:\users\hlynkacg\documents\visual studio 2010\projects\lunarlanders\lemvessel.cpp(204): error C2512: 'MGROUP_TRANSLATE' : no appropriate default constructor available
1>c:\users\hlynkacg\documents\visual studio 2010\projects\lunarlanders\lemvessel.cpp(209): error C2109: subscript requires array or pointer type

same line as before.
 
Code:
static UINT Needle
Did you mean this instead?
Code:
static MGROUP_TRANSLATE Needle [18];
UINT != MGROUP_TRANSLATE, also `new` operator returns a pointer.

Code:
new MGROUP_TRANSLATE[i];
The default constructor is `MGROUP_TRANSLATE (UINT _mesh, UINT *_grp, UINT _ngrp, const VECTOR3 &_shift)`. You need to pass these parameters to it. Also, does `i` contain some defined value at that point?

If you want dynamic allocation and you have an array of pointers to MGROUP_TRANSLATE in the `Needle` variable, you don't call `Needle = new MGROUP_TRANSLATE;` before the loop, but `Needle = new MGROUP_TRANSLATE (mesh, grp, ngrp, shift);` in the loop.
 
Did you mean this instead?
Code:
static MGROUP_TRANSLATE Needle [18];
UINT != MGROUP_TRANSLATE, also `new` operator returns a pointer.

No,

the compiler rejects 'static MGROUP_TRANSLATE' as a constructor.

---------- Post added at 09:08 PM ---------- Previous post was at 08:34 PM ----------

Current code state...

Code:
	static UINT NeedleGroups[18];
	static MGROUP_TRANSLATE Needle [18];

	for (int i = 0; i < 18; i++)
	{
		NeedleGroups[i] = VC_GRP_Panel_Needle + i;
		Needle [i] = new MGROUP_TRANSLATE (
			4,
			&NeedleGroups[i], 1,					
			ADI_Center,						
			_V( 0, sin(RAD*8), cos(RAD*8)),	
			);

		anim_TapeGuage[i] = CreateAnimation(0.00);
		AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, &Needle[i]);
	}

Current compiler error is...

Code:
1>c:\users\hlynkacg\documents\visual studio 2010\projects\lunarlanders\lemvessel.cpp(205): error C2512: 'MGROUP_TRANSLATE' : not a valid constructor
 
Sounds like MGROUP_TRANSLATE doesn't have a default (void) constructor.
 
it didn't work but the old compiler error has been replaced by a new one...

Code:
1>c:\users\hlynkacg\documents\visual studio 2010\projects\lunarlanders\lemvessel.cpp(215): error C2059: syntax error : ')'
1>c:\users\hlynkacg\documents\visual studio 2010\projects\lunarlanders\lemvessel.cpp(218): error C2664: 'VESSEL::AddAnimationComponent' : cannot convert parameter 4 from 'MGROUP_TRANSLATE **' to 'MGROUP_TRANSFORM *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

on the "AddAnimationComponent" line.
 
Remove the ampersand from
Code:
AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, Needle[i]);

also doesn't AddAnimationComponent function take five parameters instead of four ?
Oh I've checked fifth one is default so passing four is ok.

It seems like you'll need to also apply casting casuse you're creating MGROUP_TRANSLATE objects and AddAnimationComponent functions wants a pointer to bas eclass which is MGROUP_TRANSFORM so in the and it should be something like this
Code:
AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, reinterpret_cast<MGROUP_TRANSFORM*>(Needle[i]));
 
Last edited:
Success! :woohoo:

Final iteration looks like...

Code:
static UINT NeedleGroups[18];
static MGROUP_TRANSLATE *Needle[18];

for (int i = 0; i < 18; i++)
{
	double alpha = 8*RAD;
	NeedleGroups[i] = VC_GRP_Panel_Needle + i;
	Needle [i] = new MGROUP_TRANSLATE (
		4,
		&NeedleGroups[i], 1,				
		_V( 0.00, 0.064*cos(alpha), 0.064*sin(alpha))
		);

	anim_TapeGuage[i] = CreateAnimation(0.00);
	AddAnimationComponent ( anim_TapeGuage[i], 0.0f, 1.0f, Needle[i]);
}

Now how do I go about making sure those get statics get cleaned out at the end of the simulation?
 
Now how do I go about making sure those get statics get cleaned out at the end of the simulation?
Code:
for (int i = 0; i < 18; ++i) delete Needle [i];
You can additionally fill the array with NULLs after deleting its elements.

Since storage for the array wasn't allocated dynamically, but only for its elements, you can't simply use delete array, which would look like:
Code:
delete [] Needle;
 
this would go in the "LEM::~LEM ()" function correct?

Also, should I be doing this for all of my statics?
 
Code:
static UINT NeedleGroups[18];

Here memory is not allocated dynamically. NeedleGroups is not an array of pointers to which memory has been allocated using the 'new' operator. The compiler will insert proper code for releasing the memory for you. Its just an array of unsigned ints.

Code:
static MGROUP_TRANSLATE *Needle[18];

For this one you do need to free memory only if you have stored the location of some dynamically allocated memory in the elements of the array, which you have using :

Code:
Needle [i] = new MGROUP_TRANSLATE (
		4,
		&NeedleGroups[i], 1,				
		_V( 0.00, 0.064*cos(alpha), 0.064*sin(alpha))
		);
 
Back
Top