Jump to content

How to access a certain Scenario Module


Recommended Posts

I made a scenrio module and it seems to work..

but now I need to gain access to it's data and functions from an other class..

I seem to have lost the overview - no clue how to do that.


var game = HighLogic.CurrentGame;
ProtoScenarioModule psm = game.scenarios.Find(s => s.moduleName == typeof(LCARS_NCI_Scenario).Name);
ScenarioModule scm = psm.moduleRef;

This code delivers me the "ScenarioModule" object..

but it does not know my functions - my class is still not accessible

I tried to make it static, made trouble, I tried to make a new instance, but that istance didn't run though onStart so it had no config data.

How do I get the running instance of my scenario class.

I need to call this function somehow..

*instance_name_of_my_scenario*.addObject(Selected_LCARS_NCI_Object, v.id);

can anyone please hint me to the right direction, so I can get rid of my current brain blockade?

Thanks :-)

Link to comment
Share on other sites

That was the first thing I tried ofcourse..

and also right now it produced this:

[LOG 00:29:26.340] SpawnPart2: ScenarioRef 1 
[LOG 00:29:26.340] SpawnPart2: ScenarioRef 2
[LOG 00:29:26.341] SpawnPart2: ScenarioRef 3
[LOG 00:29:26.341] SpawnPart2: LCARS_NCI_Scenario addObject ex=System.NullReferenceException: Object reference not set to an instance of an object
at Philotical.LCARS_NaturalCauseInc.SpawnPart2 (.AvailablePart avPart, .CelestialBody SelectedBody) [0x00000] in <filename unknown>:0
[LOG 00:29:26.342] SpawnPart2: ScenarioRef 4

here is the code:


try
{
var game = HighLogic.CurrentGame;
ProtoScenarioModule psm = game.scenarios.Find(s => s.moduleName == typeof(LCARS_NCI_Scenario).Name);
UnityEngine.Debug.Log("SpawnPart2: ScenarioRef 2 ");
LCARS_NCI_Scenario ScenarioRef = (LCARS_NCI_Scenario)psm.moduleRef;
UnityEngine.Debug.Log("SpawnPart2: ScenarioRef 3 ");

ScenarioRef.addObject(Selected_LCARS_NCI_Object, v.id);
UnityEngine.Debug.Log("SpawnPart2: LCARS_NCI_Scenario addObject done");
}
catch (Exception ex)
{
UnityEngine.Debug.Log("SpawnPart2: LCARS_NCI_Scenario addObject ex=" + ex);
}
UnityEngine.Debug.Log("SpawnPart2: ScenarioRef 4 ");

Can it be, that I neglected to set some special thing in my scenrio class to make this work?

I'm at a loss..

Link to comment
Share on other sites

That was the first thing I tried ofcourse..

and also right now it produced this:

[LOG 00:29:26.341] SpawnPart2: LCARS_NCI_Scenario addObject ex=System.NullReferenceException: Object reference not set to an instance of an object

at Philotical.LCARS_NaturalCauseInc.SpawnPart2 (.AvailablePart avPart, .CelestialBody SelectedBody) [0x00000] in <filename unknown>:0

[LOG 00:29:26.342] SpawnPart2: ScenarioRef 4

ScenarioModules won't always be initialized when your plugin code runs. I suggest building in a delay with a coroutine. Yours is most likely throwing an exception because moduleRef is null (your code having found an uninitialized ProtoScenarioModule). Here's an example:

[KSPAddon(KSPAddon.Startup.Flight, false)]
class StopAnnoyingAsteroids : MonoBehaviour
{
IEnumerator Start()
{
while (ScenarioRunner.fetch.GetComponent<ScenarioDiscoverableObjects>() == null)
yield return 0;

ScenarioRunner.fetch.GetComponent<ScenarioDiscoverableObjects>().StopAllCoroutines(); // prevent this scenario from spawning more asteroids and spamming my debug log
}
}

Link to comment
Share on other sites

thank's but..

I can't use yield in this situation - I'm in sub function called by "OnGUI" with this and it throughs an exception because of yield (I think)

>>Error 6 The body of 'Philotical.LCARS_NaturalCauseInc.SpawnPart2(AvailablePart, CelestialBody)' cannot be an iterator block because 'Part' is not an iterator interface type

or can I make Part an iterator interface type? (I'll have to google what exactly it means btw.)

what happens:

in a window, I have a button, when it is clicked by the user, it will execute the above code, among others, and tryes to add some data to the scenario that was started on scene load..

it "might" be, that my scenario is failing somewhere else (although the log states it's there and working), therefore I'm not entirely sure this yield-suggestion applies here anyway..

it's user input not plugin start.. - does that make a difference or am I just wrong?

However, I came to the assumption, that the problem must be inside my scenario class - it does write in the persistant - but that does not mean it's not failing somewhere after that..

I guess if all you guys say, this is the way, then it must be in my code..

I'll go hunting - thanks for the help..

finally I will locate the cause of my most likely self made issue issue

Link to comment
Share on other sites

in a window, I have a button, when it is clicked by the user, it will execute the above code, among others, and tryes to add some data to the scenario that was started on scene load..

If you're seeing this error after the user clicks a button, my solution won't work for you. ProtoScenarioModule.moduleRef will only be null for a frame or two at most.

You're sure you've set a value for "v" right? I see the offending call refers to v.id

Link to comment
Share on other sites

yep v is right - however since a few minutes it works..

the problem was somewhere deep in my "wannabe" scenario..

it started, wrote to log and persistant too at game load (main screen to space center)

but in a later call on scene change a sub function failed and ended the class..

so on user click the scenario was gone again.. - I've never seen it, because my dev requires me to go to tracking station at first lol

you both got me loocking in the right direction though - I started to suspect a self made issue..

Thanks for the support guys - it's appreciated

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...