Create account / Log in

Imported Class trait question

Discussion area for the development team.

Moderators: uckelman, Tim M

Imported Class trait question

Postby m3tan » November 19th, 2020, 3:40 am

I'm making a custom trait as a java class (using these instructions here: http://www.vassalengine.org/wiki/Programming_Tutorial). I want to be able to access a property on the trait - it looks like the methods I would override are:

public List<String> getPropertyNames()
public Object getProperty(Object key)

I'm just not sure how to interpret the key. For example - I would like a Property named "FromLocation" - to access that I would be like this: $FromLocation$

So here's the code I tried to use for that:

@Override
public List<String> getPropertyNames() {
List<String> myProps = new ArrayList<String>();
myProps.add("FromLocation");
return myProps;
}
@Override
public Object getProperty(Object key) {
Object myValue = null;
if (key.equals("FromLocation")) {
myValue = this.getState();
}
else {
myValue = super.getProperty(key);
}
return myValue;
}

But it's not working. I suspect it is the "key.equals" condition in "getProperty". Does anyone have an example of an Imported Class where they have exposed properties?
User avatar
m3tan
 
Posts: 209
Joined: August 12th, 2018, 11:49 pm

Re: Imported Class trait question

Postby Brent Easton » November 19th, 2020, 5:24 am

That code looks OK, but where are you trying to reference $FromLocation$ from?
User avatar
Brent Easton
 
Posts: 3226
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: Imported Class trait question

Postby m3tan » November 19th, 2020, 5:34 am

I'm trying to expose the FromLocation, ToLocation, and Range from the LoS Thread so they can be evaluated in Beanshell expressions. I have a custom version of the LoS Thread button working and added a new trait in the editor as attached. I'm stuck at how to get those to be properties that Vassal can see at the piece or global level...
Attachments
trait.png
trait.png (17.47 KiB) Viewed 209 times
User avatar
m3tan
 
Posts: 209
Joined: August 12th, 2018, 11:49 pm

Re: Imported Class trait question

Postby Brent Easton » November 19th, 2020, 5:50 am

Ah, OK. The LOS thread is not a trait (Decorator), it is a Component (Buildable/Configurable). Traits (counters) cannot directly access any information held by components themselves, the components need to publish that information into Map or Module Global Variables that can then be seen by traits.

Have a look at what DiceButton does to publish it's result. It creates a property

Code: Select all
protected final MutableProperty.Impl property = new Impl("", this);


gives it a name

Code: Select all
property.setPropertyName(getConfigureName() + "_result");


and then adds it the Global properties maintained by it's parent in addTo().

Code: Select all
  @Override
  public void addTo(final Buildable parent) {
    super.addTo(parent);
    ran = GameModule.getGameModule().getRNG();
    property.setPropertyValue("1"); // Initialize with a numeric value //$NON-NLS-1$
    property.addTo((MutablePropertiesContainer)parent);
  }


After rolling a Die, it set the value into the property

Code: Select all
property.setPropertyValue(result.toString())


The Turn tracker does a similar thing.

Regards.
User avatar
Brent Easton
 
Posts: 3226
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: Imported Class trait question

Postby m3tan » November 19th, 2020, 10:22 pm

Thanks much. I got it working in my module! :D
User avatar
m3tan
 
Posts: 209
Joined: August 12th, 2018, 11:49 pm

Re: Imported Class trait question

Postby Cattlesquat » November 27th, 2020, 3:44 am

You somehow managed that using the ancient-and-out-of-date programming tutorial - yay for you!

Note that you may wish to point a git clone at https://github.com/vassalengine/vassal-module-template -- it has MUCH more up-to-date examples (including both a "component" and a "trait"), and includes the maven stuff to get things building much more reliably.

Brian
User avatar
Cattlesquat
 
Posts: 947
Joined: December 2nd, 2019, 4:57 pm
Location: Baltimore, Maryland, USA


Return to Developers

Who is online

Users browsing this forum: Rindis and 5 guests