Foreach problem looping one too many times

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
neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Foreach problem looping one too many times

Post by neofax »

Here is the bit of code that is causing the error:

Show Class

Code: Select all

[R, foreach(classlink,ClassInfo,""),code:{
	[macro("Get Object@Lib:Magic"):classlink]
	[H: CType=macro.return]
	[H: lvl=json.get(CType,"ClassLvl")]
	[H: PrC=json.contains(CType,"ClassPrC")]
	[ pause( "CType", "lvl", "PrC" ) ]

	[H: check=json.contains(CType,"prep")] 
	[H, if(check==1):prepC=json.get(CType,"prep")]
	[ pause( "check", "prepC", "classlink" ) ]

	[H, if(check==1):CL=json.get(CType,"CasterLvl");CL=json.get(CType,"addCL")]
	[ pause( "check", "CL" ) ]
	[H, if (PrC==1):show=classlink+" "+lvl+"/"+CL+"  ";show=macroLink(classlink+" "+lvl+"/"+CL,page+"@Lib:Magic", "none",classlink+","+spelllevel+ ","+prepC,token.name)]
	[R, if(classviewed==classlink):"<B>"+show+"</B>";show+" "]
}]
ClassInfo

Code: Select all

{"Sorcerer_PF":{"control":"Charisma","prep":0,"Channelling":"","CasterType":"Arcane","KnowAll":0,"MSL":5,"CasterLvl":10,"ClassLvl":10,"SpellList":"Wizard","LearnedSpells":[[],[],[],[],[],[],[],[],[],[]],"maxknownspells":[9,5,4,3,2,1,0,0,0,0],"SPD":[6,6,6,6,0,0,0,0,0,0],"SPDleft":[6,6,6,6,0,0,0,0,0,0]}}
CType

Code: Select all

CType
{"control":"Charisma","prep":0,"Channelling":"","CasterType":"Arcane","KnowAll":0,"MSL":5,"CasterLvl":10,"ClassLvl":10,"SpellList":"Wizard","LearnedSpells":[[],[],[],[],[],[],[],[],[],[]],"maxknownspells":[9,5,4,3,2,1,0,0,0,0],"SPD":[6,6,6,6,0,0,0,0,0,0],"SPDleft":[6,6,6,6,0,0,0,0,0,0]}
It is strange as I have only selected Sorcerer_PF as a class, yet it loops(due to the foreach) and causes the JSON.Get error. I do not know why it loops, but only that it does.

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Foreach problem looping one too many times

Post by Azhrei »

Check the "Get Object" macro to see if it uses any of the same variables.

There are only three Wiki: json.get() calls in there. Which one fails?

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Foreach problem looping one too many times

Post by aliasmask »

Now I know foreach will loop though a json array, I don't think it loops through a json object, does it? I think the whole object is just looked at once. I'm just guessing classinfo could be an array of objects which is why it's a loop.

I think you'll get a get error if macro.return is NULL.

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: Foreach problem looping one too many times

Post by CoveredInFish »

I'd say (untested as I'm working) foreach loops through json objects.

prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: Foreach problem looping one too many times

Post by prestidigitator »

From the wiki, I believe it returns each key in the JSON object.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

User avatar
Raoden
Dragon
Posts: 381
Joined: Fri Dec 18, 2009 2:33 am
Location: San Diego

Re: Foreach problem looping one too many times

Post by Raoden »

I just tried it to double-check: foreach does indeed work on JSON Objects, and returns each key in them.
"Fairy tales do not tell children the dragons exist. Children already know that dragons exist. Fairy tales tell children the dragons can be killed."
- G. K. Chesterton

Wonderful HTML/CSS reference * Color Manager * Token Manager 2.0

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: Foreach problem looping one too many times

Post by neofax »

OK, so since it goes thru each item inside ClassInfo, wouldn't it be prudent to rip out just the Class and do a foreach on this so the json.get does not error out as it will never find ClassLvl? As if I am reading the code properly, it is grabbing the Class(s) and making links for them.

@Azhrei. Here is the Get Object code:

Code: Select all

[h:classviewed=macro.args]
[ pause( "classviewed" ) ]

[h:ClassInfo=getProperty("CasterInfo")]
[h, if(string(ClassInfo)=="{}"),CODE:{
	[h:input("tjunk||CasterInfo Property Empty!!|Label|")]
	[CasterBackup=getProperty("CasterBackup")][setProperty("CasterInfo",CasterBackup)]
	[h:abort(0)]
};{}]

[h, if(json.contains(ClassInfo,classviewed)==1),CODE:{
	[Type=json.get(ClassInfo,classviewed)]};
	{[h:input("tjunk||Error no class found!|Label|")]
	[CasterBackup=getProperty("CasterBackup")]
	[setProperty("CasterInfo",CasterBackup)]
	[h:abort(0)]
}]



[h: macro.return=Type]

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Foreach problem looping one too many times

Post by aliasmask »

Raoden wrote:I just tried it to double-check: foreach does indeed work on JSON Objects, and returns each key in them.
Cool, that will save me the json.fields line if I need to loop through each field.

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Foreach problem looping one too many times

Post by Azhrei »

neofax wrote:@Azhrei. Here is the Get Object code:
So is the change to ClassInfo affecting the variable back in the calling macro? I don't know what the data looks like inside the CasterInfo parameter...

User avatar
biodude
Dragon
Posts: 444
Joined: Sun Jun 15, 2008 2:40 pm
Location: Montréal, QC

Re: Foreach problem looping one too many times

Post by biodude »

Where does 'ClassInfo' get its value from? Could there be an error in the JSON syntax creating the illusion of another, empty object key?

I just tested the code you posted, feeding the values you posted and got no errors (other than undefined variables page, lvl, and classviewed at the end), so I'm not sure what the problem could be.

Have you identified where the json.get error comes from? Could it be one of the calls in your code, rather than an extra loop looking for a key that isn't there?
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: Foreach problem looping one too many times

Post by neofax »

ClassInfo comes from a property on the token called CasterInfo which has the following info:

Code: Select all

{"Sorcerer_PF":{"control":"Charisma","prep":0,"Channelling":"","CasterType":"Arcane","KnowAll":0,"MSL":2,"CasterLvl":5,"ClassLvl":5,"SpellList":"Wizard","LearnedSpells":[[],[],[],[],[],[],[],[],[],[]],"maxknownspells":[6,4,2,0,0,0,0,0,0,0],"SPD":[6,7,5,0,0,0,0,0,0,0],"SPDleft":[6,7,5,0,0,0,0,0,0,0]}}
The code is failing on the second loop thru the "Get Object" block of code. Why, I do not know as when I use zEal's pause debugger, the variable Type is being set, but the second time it does not. However "classviewed" and "ClassInfo" are the exact same as the first go round. However, while debugging, I happened to notice that for some reason "classviewed" and "ClassInfo" popup twice when they should only show once. I would appreciate any help!

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: Foreach problem looping one too many times

Post by neofax »

OK, is there a way to strip out the Class from "ClassInfo" and then send this thru the code to make the header? This way (hopefully), I won't get an error. Also, it would streamline the code as you are not doing a foreach on like 15 items per class and only doing say 2 or three classes.

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: Foreach problem looping one too many times

Post by neofax »

Still don't know what was causing the error, but I started fresh with a new campaign and now it works fine.

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Foreach problem looping one too many times

Post by Azhrei »

That's usually a bogus property. One that used to be in the property type and isn't any more, but because it exists on the token it causes problems trying to use it as a variable name in a macro.

I thought I'd fixed that in b66, though. I suppose I could add some kind of warning so that when a variable is used that shadows a token property the user gets a warning.

Post Reply

Return to “Macros”