API Question Reading a name from a scenario file

Hlynkacg

Aspiring rocket scientist
Addon Developer
Tutorial Publisher
Donator
Joined
Dec 27, 2010
Messages
1,868
Reaction score
4
Points
0
Location
San Diego
I've it a minor hiccough in my scenario parser.

I'm trying to read a character string from the scn file and then pass it to "oapiGetVesselByName ()". I the name writes to the scn file just fine but I'm having trouble reading it.

here is the pertinent portion of "clbkLoadStateEx"

Code:
// Find mothership if able			
if (!_strnicmp (cbuf, "MOM", 4))						
{
  char name[256];
  [COLOR="Red"]sprintf (name, "%s", cbuf+4);[/COLOR]
  OBJHANDLE oh = oapiGetVesselByName (name);
  if (oh != NULL) oh_mother = oh;
}

I've used this same basic format in other areas without problem so I'm not sure why it isn't working here. Any idea what I'm doing wrong?
 
What is getting written to `name`?

Did you step through it in debugger?

Additionally, if it isn't a problem with string, are you sure the vessel you want to get from oapiGetVesselByName is already initialized?
 
Why the number is 4? MOM is 3 letters
 
Additionally, if it isn't a problem with string, are you sure the vessel you want to get from oapiGetVesselByName is already initialized?

:facepalm:

That was it.

I know clbkPostCreation gets run after LoadStateEX and prior to the first time step but does it get run after the LoadStateEx of all other vessels' in the scenario? If so I could bypass this problem by making the mothership's name string a class variable and then calling it there.

ETA:
Why the number is 4? MOM is 3 letters

you're forgetting that the 'space' in the scenario file counts as a character.

Code:
MOM <Mom's name>
1234<Read this>
 
ETA:


you're forgetting that the 'space' in the scenario file counts as a character.

Code:
MOM <Mom's name>
1234<Read this>

Ah maybe because you don't add the first character, right.

I use this code for example
Code:
char *line;
 if(!strnicmp(line,"STAGE",5)){
		 sscanf(line+5,"%i",&stage);

	 }else if(!strnicmp(line,"MET",3)){
		 sscanf(line+3,"%lf",&Tminus);

	 }else if(!strnicmp(line,"TGT_APOGEE",10)){
		 sscanf(line+10,"%lf",&tgtapo);

and it works perfectly in my dll

BTW:

I know clbkPostCreation gets run after LoadStateEX and prior to the first time step but does it get run after the LoadStateEx of all other vessels' in the scenario? If so I could bypass this problem by making the mothership's name string a class variable and then calling it there.

I don't think that Post creation runs before load state of the next vessels. I think that the loading is done vessel by vessel, so after the post creation of one vessel you start with the loadstate with the next one. I'm not 100% sure about this anyway.
 
I know clbkPostCreation gets run after LoadStateEX and prior to the first time step but does it get run after the LoadStateEx of all other vessels' in the scenario?
Yes. clbkPostCreation is called after all vessels have gone through clbkLoadStateEx.
 
Ah maybe because you don't add the first character, right.

I use this code for example
Code:
char *line;
 if(!strnicmp(line,"STAGE",5)){
		 sscanf(line+5,"%i",&stage);

	 }else if(!strnicmp(line,"MET",3)){
		 sscanf(line+3,"%lf",&Tminus);

	 }else if(!strnicmp(line,"TGT_APOGEE",10)){
		 sscanf(line+10,"%lf",&tgtapo);

and it works perfectly in my dll

Yes but you are using "sscanf" to scan the string for specific types of variable. "%i" for integers, "%lf" for floats, etc... where as I am trying to copy the string in it's entirety. If I were to copy/paste "OMG the monkeys have gone mad and they have power tools :hunter:" into the scenario file I want the value of my name string to return "OMG the monkeys have gone mad and they have power tools :hunter:"

Yes. clbkPostCreation is called after all vessels have gone through clbkLoadStateEx.

Thank you, got it working now
 
Last edited:
Like orb said, what is being written into 'name'?

I created a quick program
Code:
#include <iostream>
#include <cstdio>

int main()
{
	char * testString = "MOM OMG the monkeys have gone mad and they have power tools";
	char name[256];
	sprintf (name, "%s", testString+4);
	std::cout << name << "\n";
	
	return 0;
}

And, as expected, name contains "OMG the monkeys have gone mad and they have power tools"
 
And, as expected, name contains "OMG the monkeys have gone mad and they have power tools"

As I said above, it works now, the issue was that "oapiGetVesselByName ()" was getting called prior to complete initialization and was thus returning a NULL value and causing various errors.
 
Back
Top