Error Help with Beacons

Frogisis

innn spaaaaace...ace...ace...!
Addon Developer
Joined
Aug 13, 2008
Messages
185
Reaction score
0
Points
31
Location
Chicago
Website
www.frogisis.com
I'm currently working again on my second add-on, a funky take on the "realistic interplanetary spaceship" shape we're all familiar with, to teach myself C++, something I've never used before.
Some beacons, I thought, would really set off the design and make it more interesting to look at, so I scoured the internet and after a reading a lot of tutorials and posts on this and other forums, I finally figured out some beacon code that compiles:

BEACONLIGHTSPEC winglight;
static VECTOR3 winglightpos = {0,10,0};
static VECTOR3 winglightcol = {1,0.5,0.5};
winglight.shape = BEACONSHAPE_DIFFUSE;
winglight.pos = &winglightpos;
winglight.col = &winglightcol;
winglight.size = 2;
winglight.falloff = 0.4;
winglight.period = 0;
winglight.active = true;
AddBeacon (&winglight);

(this is just a test to get the beacon to actually appear in the sim, not the final beacon I want.)
The only problem is that when I load up my new ship in my (clean installation!) of Orbiter, I get a CTD immediately, but if I prune out this code, and this code alone, everything works fine again. I did another little search for this problem, but didn't find anything and I think this is beyond my current skill level to solve on my own.
The added wrinkle is that the Orbiter log does not show anything particularly suspicious: It spools along fine and simply ends with:

Finished initialising world
Module Lodestone.dll [API v.060425] (the ship)
Finished initialising status
Finished initialising camera
Finished initialising panels
Finished setting up render state

....And then a CTD. Can anyone point out to me what I'm missing or not doing correctly here? The .ccp file for the ship is the only thing I've modified, but I read somewhere that for beacons I would have to add something to the .h file, but the post was incredibly vague about it and the example it gave was something specific to that other person's project, so I thought it wasn't relevant (and I wanted to let sleeping .h files lie.)

If someone could help me out with this, I think I'd consider the project done. I made some cargo containers to attach to the ship, and maybe I'll give coding that system a try, but if it turns out to be too hard I'll just go with the ol' Universal Cargo Deck. Thanks for taking the time to read.
 
That problem is that two of the BEACONLIGHTSPEC structure members are not initialized:

Code:
double duration;   //   strobe duration
double tofs;       //   strobe time offset

Since those two fields are not initialized they will have undefined (i.e., indeterminate) values in them. For a working example of beacon code, take a look at lines 2287-2301 of Orbitersdk\samples\DeltaGlider\DeltaGlider.cpp:

Code:
 // ********************* beacon lights **********************
 static VECTOR3 beaconpos[8] = {{-8.6,0,-3.3}, {8.6,0,-3.3}, {0,0.5,-7.5}, {0,2.2,2}, {0,-1.4,2}, {-8.9,2.5,-5.4}, {8.9,2.5,-5.4}, {0,-1.8,2}};
 static VECTOR3 beaconcol[7] = {{1.0,0.5,0.5}, {0.5,1.0,0.5}, {1,1,1}, {1,0.6,0.6}, {1,0.6,0.6}, {1,1,1}, {1,1,1}};
 for (i = 0; i < 7; i++) {
  beacon[i].shape = (i < 3 ? BEACONSHAPE_DIFFUSE : BEACONSHAPE_STAR);
  beacon[i].pos = beaconpos+i;
  beacon[i].col = beaconcol+i;
  beacon[i].size = (i < 3 ? 0.3 : 0.55);
  beacon[i].falloff = (i < 3 ? 0.4 : 0.6);
  beacon[i].period = (i < 3 ? 0 : i < 5 ? 2 : 1.13);
  [COLOR=red]beacon[i].duration = (i < 5 ? 0.1 : 0.05);[/COLOR]
[COLOR=red]  beacon[i].tofs = (6-i)*0.2;[/COLOR]
  beacon[i].active = false;
  AddBeacon (beacon+i);
 }

Be sure to initialize all fields in the structure and it should work fine.
 
Thanks for the advice, although I still couldn't get it to work...I had those lines in there earlier, but since the API reference manual said those two parameters are ignored if the period is set to "0", I took them out to keep things as simple as possible. ...But putting them back in doesn't seem to make a difference.

I have this now:

BEACONLIGHTSPEC winglight;
static VECTOR3 winglightpos = {0,20,0};
static VECTOR3 winglightcol = {1,0.5,0.5};
winglight.shape = BEACONSHAPE_DIFFUSE;
winglight.pos = &winglightpos;
winglight.col = &winglightcol;
winglight.size = 2;
winglight.falloff = 0.4;
winglight.period = 2;
winglight.duration = 1;
winglight.tofs = 0;
winglight.active = true;
AddBeacon (&winglight);

But still get a CTD when I try to use it. I had earlier tried copying the code from the Delta Glider and from some other vessel code I found online (http://www.orbitersim.com/forum/default.aspx?g=posts&t=7134), and I can get them to compile if I add an appropriate "BEACONLIGHTSPEC" line and add another "int i;" line to re-enter the variable "i" (otherwise both the name of the beacon and "i" are spat back as "undeclared identifiers." I wonder if my compiler [Visual 2008] isn't set right or something... On the other hand, everything works just fine without this particular code.)

Orbiter diagnostic comes up clean, too, for what it's worth.

I've fiddled with all the parameters and numbers, but nothing works. They all compile just fine, but I get a CTD when I try to use any of them. The closest I've gotten is being able to add the ship separately in the scenario editor (still can't see the beacon, though) but if I then switch to the ship using F3 and try to switch between the internal and external views, I get a crash. I have absolutely no idea what could be causing this.
 
Make winglight static (or global static). You're passing a pointer to a local, and then as soon as that function exits, that memory is no longer valid since that stack frame no longer exists. If orbiter tries to access that memory, it can die.

As a general rule, you should never ever ever pass a pointer to a local variable as an input unless the documentation explicitly states that it's safe to do so.
 
Or move the declaration of the BEACONLIGHTSPEC to the private section of the class declaration. The disadvantage of having a static is that if you turn the lights off in one instance of the vessel, they will also turn off in all other instances.

PS. Global static - how dare you suggest such a thing! ;)
 
Or move the declaration of the BEACONLIGHTSPEC to the private section of the class declaration. The disadvantage of having a static is that if you turn the lights off in one instance of the vessel, they will also turn off in all other instances.

PS. Global static - how dare you suggest such a thing! ;)

I meant class member. It's late and I've been sitting here working on code for like twelve hours. Give me a break. :P
 
Make winglight static (or global static). You're passing a pointer to a local, and then as soon as that function exits, that memory is no longer valid since that stack frame no longer exists. If orbiter tries to access that memory, it can die.

As a general rule, you should never ever ever pass a pointer to a local variable as an input unless the documentation explicitly states that it's safe to do so.

Thank you so much! That did it! I put "static" in front of "BEACONLIGHTSPEC," and now I'm watching the little guy flash on and off as I write this. I think I understand what you're saying about memory allocation, but my understanding of the nuts and bolts of computers doesn't get quite that fine-grained - Like my understanding of rocketry doesn't make it down to the level of turbopumps and peeing on bus tires. I'll try those other ways people mentioned, since they seem more elegant and professional, but in the mean time at least I can get the beacon to show up.

Like I said, I'm teaching myself this stuff as I go along, so thanks for your patience - The ultimate goal is being able to build an interactive art installation I told some gallery people I could do. I need to do some more homework, though, since C++ might not even be the best way to get little LEDs or motors to turn on in sequence when you press buttons.

In the mean time, however, thanks for helping me add something to my ship that makes it look less like a toy.
 
Thank you so much! That did it! I put "static" in front of "BEACONLIGHTSPEC," and now I'm watching the little guy flash on and off as I write this. I think I understand what you're saying about memory allocation, but my understanding of the nuts and bolts of computers doesn't get quite that fine-grained - Like my understanding of rocketry doesn't make it down to the level of turbopumps and peeing on bus tires. I'll try those other ways people mentioned, since they seem more elegant and professional, but in the mean time at least I can get the beacon to show up.

Like I said, I'm teaching myself this stuff as I go along, so thanks for your patience - The ultimate goal is being able to build an interactive art installation I told some gallery people I could do. I need to do some more homework, though, since C++ might not even be the best way to get little LEDs or motors to turn on in sequence when you press buttons.

In the mean time, however, thanks for helping me add something to my ship that makes it look less like a toy.

Hehe, no problem. Keep in mind though what tblaxland said: with that element listed as static, all of your ships will be blinking on/off at the same time. If that's not a problem then it's fine that way, but if it is a problem you'll have to move it to be a class member variable.
 
Back
Top