Page 1 of 1

Confused about a macro error

Posted: Tue Nov 14, 2017 2:45 am
by TheIneffableCheese
I am sorry to be back with another question that's probably going to end up being something basic, but I've been over this macro code multiple times and I cannot figure out where I'm screwing things up.

The macro is for applying a Readied Action. I have a Ready Action state to mark tokens that are readied.

The intent of the macro is to first check to make sure the token is in the initiative. Second it checks if the token has the Readied Action state. If the state is not turned on, the macro turns the state on.

If the state is already turned on, then intent is to have the program turn off the "Readied Action" state, prompt for the new Initiative value for the token, apply the new value token, then sort the initiative to put the tokens into the correct order.

The first two steps seem to be working fine - the macro stops if the token isn't in the initiative, and it will turn on the Readied Action state. However, if I apply it to a token that already has the Readied Action state, then I get the following error in the chat:

java.lang.NullPointerException error executing expression .

Here is my code for reference. Please let me know if I need to clarify anything:
CODE

Code: Select all


<!--- confirm Token is in Initiative --->

[h: init = getInitiative()]
[h: newInit = ""]
[h: assert(isNumber(init),"Ready Action only applies to tokens in Initiative", 0)]

<!--- Check for current State value --->

[h: stateName = "ReadyAction"]
[h: currentStatus = getState(stateName)]

<!--- set State and adjust Initiative order --->

[h, if(currentStatus == 0): setState(stateName, 1);]

[h, if(currentStatus == 1), CODE:
{
	[h: setState(stateName, 0)]
	[h: input("newInit|"+init+"|Enter New Initiative",)]
	[h: setInitiative(newInit)]
	[h: sortInitiative()]
	
}]


Re: Confused about a macro error

Posted: Tue Nov 14, 2017 4:58 am
by bobifle
I'm absolutely no expert but there seems to be an extra comma on the "input" line.

Re: Confused about a macro error

Posted: Tue Nov 14, 2017 7:47 am
by wolph42
bobifle wrote:I'm absolutely no expert but there's seems to be an extra comma on the "input" line.
nice catch, its always hard to scan for syntax errors.

Re: Confused about a macro error

Posted: Tue Nov 14, 2017 7:51 am
by aliasmask
You have an extra ";". This may not cause an error, but is incorrect if you don't have a false condition statement. Also, when dealing with true/false conditions where your value is 0 or 1, then you don't need the == 0 part. if(0) where currentStatus equal 0 is perfectly valid. In that case, it's better to have a variable name that describes the true/false nature like "isStateOn".

There is an extra "," in your input line at the end. The parser is probably expecting another line for input and found null instead. I find using 'text'+var+'text' to get a bit messy sometimes and prefer to use strformat("text %{var} text") formatting instead. I find it easier to read.

I also changed the order of getting your input and setting a state. You don't want to set the state if the user aborts the input.

Code: Select all

<!--- confirm Token is in Initiative --->

[h: init = getInitiative()]
[h: newInit = ""]
[h: assert(isNumber(init),"Ready Action only applies to tokens in Initiative", 0)]

<!--- Check for current State value --->

[h: stateName = "ReadyAction"]
[h: isStateOn = getState(stateName)]

<!--- set State and adjust Initiative order --->

[h, if(isStateOn), code: {
   [h: abort(input(strformat("newInit|%{init}|Enter New Initiative|TEXT")))]
   [h: setState(stateName, 0)]
   [h: setInitiative(newInit)]
   [h: sortInitiative()]   
};{
   [H: setState(stateName, 1)]
}]

Re: Confused about a macro error

Posted: Tue Nov 14, 2017 5:41 pm
by TheIneffableCheese
Thanks to all for the help. Aliasmask's code works perfectly, and is definitely more elegant than my first attempt.

Hopefully one of these days I will be able to do some of these simple things without having to ask so many questions. :D