Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property default and reset js bindings #5156

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

fishface60
Copy link
Contributor

@fishface60 fishface60 commented Jan 30, 2025

Identify the Bug or Feature request

resolves #5152

The linked issue misdiagnoses the issue as Token.setProperty setting a value differently and not being identified as equalling the default to reset.

Actually it's only the Edit Token dialog that resets properties that way.

The lack of functions for resetting properties and determining what the default is however prevents implementing this behaviour yourself, so this patch resolves it by adding required functions.

Description of the Change

This refactors the Token functions slightly to make the property default code and property evaluator reusable, and adds javascript bindings.

Possible Drawbacks

This allows evaluating the defaults even if there is a value set, and an obscure enough campaign might hide logic in there that is usually obscured by a non-default value being present in that property.

Documentation Notes

token.resetProperty(name) Function

• Introduced in version 1.17

Returns true if the property was reset, false if it was not e.g. because you don't own it.

Usage

function fullHeal(token) {
  token.resetProperty("HP");
}
  • name - The name of the property to reset.

See resetProperty() for MTScript equivalent.

token.getPropertyDefault(name) Function

• Introduced in version 1.17

Returns the text of the default value if it exists, null if it didn't or you don't own the token.

Usage

function changeHP(token, value) {
  if (value == token.getPropertyDefault("HP")) {
    token.resetProperty("HP");
  } else {
    token.setProperty("HP", value);
  }
}
  • name - The name of the property to retrieve the default for.

See getPropertyDefault() for MTScript equivalent.

token.getEvaluatedPropertyDefault(name) Function

• Introduced in version 1.17

Returns the value of the default's expression, evaluated in the context of the token if it exists, null if it didn't or you don't own the token.

Usage

function changeHP(token, value) {
  if (value == token.getEvaluatedPropertyDefault("HP")) {
    token.resetProperty("HP");
  } else {
    token.setProperty("HP", value);
  }
}
  • name - The name of the property to reset.

See getPropertyDefault() and evalMacro() for MTScript equivalent.

Release Notes

  • Added Token.resetProperty, Token.getPropertyDefault and Token.getEvaluatedPropertyDefault so JavaScript macros can reset properties if they match their default value.

This change is Reviewable

Token property lookup logic is duplicated and it would be helpful to be
able to evaluate property list values in other contexts.
resetProperty was conspicuously absent
and is necessary to support resetting a token to default
which is convenient for things like a Full Heal or removing expired
temporary HP.

Another reason to reset properties is if they have been restored to
their default and you wish to reduce memory usage and serialization size
but to do so without coupling to the campaign requires functions to get
the default.

getPropertyDefault gets the default value from the properties verbatim
and getEvaluatedPropertDefault evaluates the expression in the context
of the token.
@github-actions github-actions bot added the bug label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Values set by token.setProperty() in GraalVM are of a different type than values set in MTScript
1 participant