Macro creating new property and not assigning correctly

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Kira
Kobold
Posts: 11
Joined: Tue Jul 13, 2010 1:38 am

Macro creating new property and not assigning correctly

Post by Kira »

I'm working on a BESM framework for a campaign I plan on running. I have decided to use nested json objects to hold the weapon information (Each weapon name is a key in the top level object, with the object containing the weapon information being the value for that key).

I have a pair of macros to add a new weapon to the object and everything is almost working, but when I run the macro two things occur:
1) The macro, instead of assigning the result to the Weapons property already present on the token, creates a new invisible property and assigns it to that.
2) The macro assigns the default weapon information correctly, but fails to add the new weapon's information.

Expected result:
The macro adds the default weapon ("Unarmed") if the property is empty, then adds the new weapon's information to the property already present on the token

The first macro (present in the Campaign macro set):
Spoiler

Code: Select all

<!-- Take input from the user to add a weapon to the selected character using the AddWeapons library macro
-->
[h: wEntry = "{}"]
[h: newName = ""]
[h: newDamage = 0]
[h: newBonus = 0]
[h: newVariables = "{}"]
[h: newAutoDmg = "{}"]
[h: newAuto = 0]
[h: input(
"newName|''|Enter a name for the new weapon|TEXT",
"newLevel|0|Enter a level for the new weapon|TEXT",
"newAttack|0|Enter an attack bonus for the new weapon|TEXT",
"newVariables|''|Enter variables of the weapon, sepperated by commas and no spaces|TEXT",
"newAutoBool|1|Does this weapon automatically deal ACV?|CHECK",
"newAuto|0|Enter automatic damage of the weapon|TEXT"
)]
[h: wEntry = json.set(wEntry, "newName", newName)]
[h: wEntry = json.set(wEntry, "newLevel", newLevel)]
[h: wEntry = json.set(wEntry, "newAttack", newAttack)]
[h: wVarArr = json.fromList(newVariables)]
[h: wEntry = json.set(wEntry, "newVariables", wVarArr)]
[h, if(newAutoBool == 1), code: {
	[h: newAutoDmg = json.set(newAutoDmg, "1", "AttackCombatValue")]
	};
	{[h: newAutoDmg = json.set(newAutoDmg, "1", "None")]}
]
[h: newAutoDmg = json.set(newAutoDmg, "2", newAuto)]
[h: wEntry = json.set(wEntry, "newAuto", newAutoDmg)]
[h, macro("NewWeapon@Lib:CharacterCreation"): wEntry]
The second macro (present on a library token named "Lib:CharacterCreation"):
Spoiler

Code: Select all

<!-- initialize variables to use for weapon creation.
	weaponEntry is used to combine the objects into one entry in the property
	weaponProps is used to store all of the properties of the weapon together
	weaponVariables is used to store the different variables of a weapon as a nested object
	weaponAutoDmg is used to sepparate the ACV value, if present, from bonus damage
	-->
[h:weaponEntry = "{}"]
[h:weaponProps = "{}"]
[h:weaponVariables = "{}"]
[h:weaponAutoDmg = "{}"]
<!-- if the Weapons property is empty, create a new object as its value and create an unarmed attack as the default value -->
[h, if(json.isEmpty(Weapons)), code: {
	[h: weaponProps = json.set(weaponProps, "Level", 0)]
	[h: weaponProps = json.set(weaponProps, "Attack", 0)]
	[h: weaponVariables = json.set(weaponVariables, "1", "stun")]
	[h: weaponVariables = json.set(weaponVariables, "2", "nonpenetrating")]
	[h: weaponProps = json.set(weaponProps, "Variables", weaponVariables)]
	[h: weaponAutoDmg = json.set(weaponAutoDmg, "1", "AttackCombatValue")]
	[h: weaponAutoDmg = json.set(weaponAutoDmg, "2", 0)]
	[h: weaponProps = json.set(weaponProps, "AutoDmg", weaponAutoDmg)]
	[h: weaponEntry = json.set(weaponEntry, "Unarmed", weaponProps)]
	[h: setProperty(Weapons, weaponEntry)]
	};{}]
<!-- retrieve the individual entries from the calling macro -->
[h: wName = json.get(macro.args, "newName")]
[h: wLevel = json.get(macro.args, "newLevel")]
[h: wAttack = json.get(macro.args, "newAttack")]
[h: wVariables = json.get(macro.args, "newVariables")]
[h: wAutoDmg = json.get(macro.args, "newAuto")]
<!-- create a new object to append to the property -->
[h: weaponProps = json.set(weaponProps, "Level", wLevel)]
[h: weaponProps = json.set(weaponProps, "Attack", wAttack)]
[h: weaponProps = json.set(weaponProps, "Variables", wVariables)]
[h: weaponProps = json.set(weaponProps, "AutoDmg", wAutoDmg)]
<!-- append the new weapon to the Weapon property -->
[h: wList = "{}"]
[h: wList = getProperty(Weapons)]
[h: json.set(wList, wName, weaponProps)]
[h: setProperty(Weapons, wList)]

Craig
Great Wyrm
Posts: 2107
Joined: Sun Jun 22, 2008 7:53 pm
Location: Melbourne, Australia

Re: Macro creating new property and not assigning correctly

Post by Craig »

Kira wrote:I'm working on a BESM framework for a campaign I plan on running. I have decided to use nested json objects to hold the weapon information (Each weapon name is a

The second macro (present on a library token named "Lib:CharacterCreation"):
Spoiler

Code: Select all

<!-- initialize variables to use for weapon creation.
	weaponEntry is used to combine the objects into one entry in the property
	weaponProps is used to store all of the properties of the weapon together
	weaponVariables is used to store the different variables of a weapon as a nested object
	weaponAutoDmg is used to sepparate the ACV value, if present, from bonus damage
	-->
[h:weaponEntry = "{}"]
[h:weaponProps = "{}"]
[h:weaponVariables = "{}"]
[h:weaponAutoDmg = "{}"]
<!-- if the Weapons property is empty, create a new object as its value and create an unarmed attack as the default value -->
[h, if(json.isEmpty(Weapons)), code: {
	[h: weaponProps = json.set(weaponProps, "Level", 0)]
	[h: weaponProps = json.set(weaponProps, "Attack", 0)]
	[h: weaponVariables = json.set(weaponVariables, "1", "stun")]
	[h: weaponVariables = json.set(weaponVariables, "2", "nonpenetrating")]
	[h: weaponProps = json.set(weaponProps, "Variables", weaponVariables)]
	[h: weaponAutoDmg = json.set(weaponAutoDmg, "1", "AttackCombatValue")]
	[h: weaponAutoDmg = json.set(weaponAutoDmg, "2", 0)]
	[h: weaponProps = json.set(weaponProps, "AutoDmg", weaponAutoDmg)]
	[h: weaponEntry = json.set(weaponEntry, "Unarmed", weaponProps)]
	[h: setProperty(Weapons, weaponEntry)]
	};{}]
<!-- retrieve the individual entries from the calling macro -->
[h: wName = json.get(macro.args, "newName")]
[h: wLevel = json.get(macro.args, "newLevel")]
[h: wAttack = json.get(macro.args, "newAttack")]
[h: wVariables = json.get(macro.args, "newVariables")]
[h: wAutoDmg = json.get(macro.args, "newAuto")]
<!-- create a new object to append to the property -->
[h: weaponProps = json.set(weaponProps, "Level", wLevel)]
[h: weaponProps = json.set(weaponProps, "Attack", wAttack)]
[h: weaponProps = json.set(weaponProps, "Variables", wVariables)]
[h: weaponProps = json.set(weaponProps, "AutoDmg", wAutoDmg)]
<!-- append the new weapon to the Weapon property -->
[h: wList = "{}"]
[h: wList = getProperty(Weapons)]
[h: json.set(wList, wName, weaponProps)]
[h: setProperty(Weapons, wList)]
The second last line

Code: Select all

[h: json.set(wList, wName, weaponProps)]
Should be

Code: Select all

[h: wList = json.set(wList, wName, weaponProps)]
json.set() doesn't modify the JSON object it returns a new one. JSON object in macro script are immutable and even though it can some times be a pain in situations like this if they were not then the bugs that occur would be even harder to track down (because they would alter properties when you didnt expect them too etc).

Kira
Kobold
Posts: 11
Joined: Tue Jul 13, 2010 1:38 am

Re: Macro creating new property and not assigning correctly

Post by Kira »

Ah! Thank you! - not sure how I missed that one. However I still have the issue that it creates a new invisible property instead of editing the existing one... As if it can't find the existing property.

Edit:
If I run the Campaign macro a second time, it overwrites the first entry... For example, if I run the macro once and create a "Dummy" weapon then run the macro a second time creating a "Test" weapon and check the newly created Weapons property with a macro: The only entries are "Unarmed" and "Test" with no "Dummy" weapon.

Craig
Great Wyrm
Posts: 2107
Joined: Sun Jun 22, 2008 7:53 pm
Location: Melbourne, Australia

Re: Macro creating new property and not assigning correctly

Post by Craig »

setProperty() takes a string as first variable.
I see many calls to setProperty(Weapons,...) but no where that the variable Weapons is set, should this be setProperty("Weapons", ...) or is the code that sets the variable not shown?

Kira
Kobold
Posts: 11
Joined: Tue Jul 13, 2010 1:38 am

Re: Macro creating new property and not assigning correctly

Post by Kira »

Craig wrote:setProperty() takes a string as first variable.
I see many calls to setProperty(Weapons,...) but no where that the variable Weapons is set, should this be setProperty("Weapons", ...) or is the code that sets the variable not shown?
Oh. So it probably should... "Weapons" is a token property

Post Reply

Return to “Macros”