Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: Simulator Enhancements (03/13/24)

Author Topic: How can I pass an Object to an inner class?  (Read 2262 times)

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
How can I pass an Object to an inner class?
« on: November 26, 2013, 02:38:24 AM »

Basically, I need to pass a CampaignFleetAPI Object to a Script.  I don't know until the Script is about to get built what the Object is, so I want to pass that Object reference to the Script as a parameter during an addFleetAssignment() event.  

I'm really confused about this aspect of Java; I've read several articles about this issue but it's unclear how this is supposed to be done.

Code: java
	private Script doSomething(CampaignFleetAPI fleet)
{
return new Script()
{
public void run()
{
//in here, I want to refer to the fleet Object
};
}

Any help on this would be appreciated; I've been banging my head against this one for quite a while tonight  ::)
Logged
Please check out my SS projects :)
Xeno's Mod Pack

silentstormpt

  • Admiral
  • *****
  • Posts: 1060
    • View Profile
Re: How can I pass an Object to an inner class?
« Reply #1 on: November 26, 2013, 05:16:07 AM »

Have you tried extending the CampaignFleetAPI and add the Script to it, i got no idea how to do it any other way
Logged

Talkie Toaster

  • Captain
  • ****
  • Posts: 256
    • View Profile
Re: How can I pass an Object to an inner class?
« Reply #2 on: November 26, 2013, 07:30:15 AM »

I've done it by copying how BaseSpawnPoint is set up- extending Script, and adding a bunch of stuff to the self-named function that I think gets called when you declare a new one. Here's an example of the script:
Code
public class exampleScriptClass implements EveryFrameScript, SpawnPointPlugin {	
private StarSystemAPI scriptSystem;
private SectorEntityToken scriptToken;
private CampaignFleetAPI scriptFleet;

public exampleScriptClass(StarSystemAPI ARG1, SectorEntityToken ARG2, CampaignFleetAPI ARG3) {
this.scriptSystem = ARG1;
this.scriptToken= ARG2;
this.scriptFleet= ARG3;
}
Then it'll take variables as:
Code
exampleScriptClass myScript = new exampleScriptClass ( system, station, fleet );
Logged

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 23986
    • View Profile
Re: How can I pass an Object to an inner class?
« Reply #3 on: November 26, 2013, 08:47:05 AM »

There's a simpler way:

Code: java
	private Script doSomething(final CampaignFleetAPI fleet)
{
return new Script()
{
public void run()
{
//in here, I want to refer to the fleet Object
};
}

If you make a local variable (or parameter) final, it can be accessed from an anonymous class. You don't even need to pass it in, can just use it.
Logged

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: How can I pass an Object to an inner class?
« Reply #4 on: November 26, 2013, 01:09:51 PM »

Well, I got through it (eventually), with Alex's suggestion :)  

I added a valid-instance check like so, just for safety's sake:
Code: java
	private Script destroyAsteroidToken(final CampaignFleetAPI thisFleet)
{
return new Script()
{
public void run()
{
if(thisFleet instanceof CampaignFleetAPI)
{
                                  //do Stuff
      }
    }
  };
}
It turned out to be harder to get what I wanted running than I expected, largely because if anything throws null inside a script, you only get the register back.  But it finally works :)
Logged
Please check out my SS projects :)
Xeno's Mod Pack

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 23986
    • View Profile
Re: How can I pass an Object to an inner class?
« Reply #5 on: November 26, 2013, 02:10:21 PM »

The instance check shouldn't be necessary - rather, it seems like you're just using it instead of a null check. A simple thisFleet != null should be enough there, since if it's not null, instanceof is always going to return true there.
Logged

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: How can I pass an Object to an inner class?
« Reply #6 on: November 26, 2013, 05:23:31 PM »

Ah, OK. 

I'm probably just being paranoid, anyhow; it was pretty hard to get the interior bits working at all and I'm really worried that somehow it'll get bad data passed to it and hose a game; pretty silly, really but it was a pretty epic slog getting it working as planned :)
Logged
Please check out my SS projects :)
Xeno's Mod Pack