• Please note: In an effort to ensure that all of our users feel welcome on our forums, we’ve updated our forum rules. You can review the updated rules here: http://forums.wolflair.com/showthread.php?t=5528.

    If a fellow Community member is not following the forum rules, please report the post by clicking the Report button (the red yield sign on the left) located on every post. This will notify the moderators directly. If you have any questions about these new rules, please contact support@wolflair.com.

    - The Lone Wolf Development Team

Q: foreach statements, is nesting possible?

technorat

Member
Hi all,

I've got a situation where I'm trying to compare a hero's equipment against the skills they have. For this it seems to be useful to be able to nest foreach statements but I can't tell if it's possible since the "foreach pick" statement gives you an "eachpick" context. What I would like to do is something like this pseudocode:

foreach pick in hero where Equipment
~if equipment matches some parameter
foreach pick in hero where Knowledge
~does user have knowledge for equipment, if so do something
nexteach
nexteach

Any ideas? Thanks :)
 
Looks fine as is. While you're in the first foreach, you build the variable that you place into "Knowledge" that describes the equipment you're searching for.

This seems more like something you'd want to put on the equipment itself, though - "we exist, now, test if the user has a skill that matches us" - that doesn't require nesting foreaches. Or from the skill: "is there any equipment that we're useful for?"
 
Thanks Mathias,

That works perfectly. I'm actually using it to implement a skill specialization feature like in Deluxe Savage Worlds. So so what I'm doing is comparing the skill domain (I replaced my skills with custom setting skills) to the weapon type and applying a penalty if there are no matches. It ends up looking something like this:

Code:
		    foreach pick in hero where "Armory.Melee"
				~Weapon Type
				var type as string
				type = eachpick.tagnames[WeaponType.?,", "]
				~debug "type=" & type
				~does hero have matching skill
				var foundSkill as number
				foundSkill = 0
				~does hero have matching domain
				var foundDom as number
				foundDom = 0				
				foreach pick in hero where "thingid.tlFighting"
					foundSkill = 1
					var dom as string
					dom = eachpick.field[domDomain].text
					if (compare(dom,type) = 0) then
						foundDom = 1
						endif
				nexteach
				if (foundSkill = 1) then
					if (foundDom = 0) then
						~if we have the skill but not the domain give a -2 penalty
						eachpick.field[wpBonus].value -= 2
					endif
				endif
		    nexteach
 
Back
Top