Imported Class trait question

I’m making a custom trait as a java class (using these instructions here: 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 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 getPropertyNames() {
List myProps = new ArrayList();
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?

That code looks OK, but where are you trying to reference $FromLocation$ from?

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…

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

protected final MutableProperty.Impl property = new Impl("", this);

gives it a name

property.setPropertyName(getConfigureName() + "_result");

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

@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

property.setPropertyValue(result.toString())

The Turn tracker does a similar thing.

Regards.

Thanks much. I got it working in my module! :smiley:

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 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