What is wrong with my code

If you have searched the other User Creations subforums and an answer to your question has not been found, please post here!

Moderators: dorpond, trevor, Azhrei, Gamerdude

Post Reply
fredjsquirrel
Kobold
Posts: 22
Joined: Thu Jan 28, 2016 9:17 pm

What is wrong with my code

Post by fredjsquirrel »

Maptools Ver: 1.4

What is wrong with the following

Code: Select all

[r, if (result == ""): result = wepstr ; result = result + "," + wepstr ]
I am getting the following error:
  Invalid condition in IF(result == "") roll option.       
Statement options (if any): r, if (result == "")       
Statement Body : result = wepstr ; result = result + "," + wepstr

At this point I am at a complete loss because I just can't figure it out. If I reverse the actions it works fine: if (result == ""): result = result + "," + wepstr ; result = wepstr
But then the output is wrong and isn't usable. If I make it a != again it works but doesn't give a valid result. If I use the != and shift the assignments to be the correct order, then again I get the error. I've include the input data, the entire macro code and a sample of the output to the chat window.

What on earth have I done wrong?

Thanks in advance.


This is the input data:

Code: Select all

["weapon.name=Scimitar;weapon.tohit=4;weapon.damage=1d6+2;weapon.type=slashing;weapon.critdie=d6;weapon.note=","weapon.name=Shortbow;weapon.tohit=4;weapon.damage=1d6+2;weapon.type=piercing;weapon.critdie=1d6;weapon.note="]
This is the Macro Code:

Code: Select all

[h: str = macro.args]

[h: result = ""]
[foreach(w, str), code:
{
	[h: wepstr = ""]
	[foreach(fld, w, "", ";"), code:
	{
		[h: newval = replace(fld, "weapon.", "")]
		[h, if (wepstr != ""): wepstr = wepstr + ',"' + replace(newval, "=", '":"') + '"' ; wepstr = '{"' + replace(newval, "=", '":"') + '"']
	}]

	[h: wepstr = wepstr + ',"range":"5"' + ',"magic":"0"' ]

[h: Output("convert: a: wepstr: " + wepstr, "all", getSelected())]
	
	[h, if ( ! startsWith(wepstr, "{")): wepstr = "{" + wepstr]
	[h, if (! endsWith(wepstr, "}")): wepstr = wepstr + "}"]

[h: Output("convert: b: wepstr: " + wepstr, "all", getSelected())]
[h: Output("convert: a: result " + result, "all", getSelected())]

[b]	[r, if (result == ""): result = wepstr ; result = result + "," + wepstr ]
[/b]

[h: Output("convert: b: result " + result, "all", getSelected())]
}]
[h:result = strformat( "[%s]", result)]
[h: Output("convert: result " + result, "all", getSelected())]

[h: macro.return = result]
Chat window output:
lib:GlobalMacros:
Calling Convert
lib:GlobalMacros:
convert: a: wepstr: {"name":"Scimitar","tohit":"4","damage":"1d6+2","type":"slashing","critdie":"d6","note":"","range":"5","magic":"0"
lib:GlobalMacros:
convert: b: wepstr: {"name":"Scimitar","tohit":"4","damage":"1d6+2","type":"slashing","critdie":"d6","note":"","range":"5","magic":"0"}
lib:GlobalMacros:
convert: a: result
lib:GlobalMacros:
convert: b: result {"name":"Scimitar","tohit":"4","damage":"1d6+2","type":"slashing","critdie":"d6","note":"","range":"5","magic":"0"}
lib:GlobalMacros:
convert: a: wepstr: {"name":"Shortbow","tohit":"4","damage":"1d6+2","type":"piercing","critdie":"1d6","note":"","range":"5","magic":"0"
lib:GlobalMacros:
convert: b: wepstr: {"name":"Shortbow","tohit":"4","damage":"1d6+2","type":"piercing","critdie":"1d6","note":"","range":"5","magic":"0"}
lib:GlobalMacros:
convert: a: result {"name":"Scimitar","tohit":"4","damage":"1d6+2","type":"slashing","critdie":"d6","note":"","range":"5","magic":"0"}
   Invalid condition in IF(result == "") roll option.       Statement options (if any): r, if (result == "")       Statement Body : result = wepstr ; result = result + "," + wepstr

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: What is wrong with my code

Post by aliasmask »

I recommend doing this:

Code: Select all

[r, if(json.isEmpty(result)): result = wepstr ; result = result + "," + wepstr ]
When comparing result to a string, result must also be a string and if not then it may give an error. Using the json.isEmpty allows you to do a check with strings, numbers, jsons and even null. You may want to debug the value of result before the if statement to determine what is wrong.

fredjsquirrel
Kobold
Posts: 22
Joined: Thu Jan 28, 2016 9:17 pm

Re: What is wrong with my code

Post by fredjsquirrel »

That fixed it.

Thank you. I should have thought of that myself.

Post Reply

Return to “Requests for HELLLLP!”