Twine 2: referencing a variable within nested data maps

Twine 2

I’m going to use very basic language because I am very new to this, so please respond in kind, I won’t be offended. explain like I’m 5 if you have to.

I’m trying to set up a system for “equipping” different classes of weapons, and weapons having their own damage multipliers that interact with a pc’s “stats”. I’m also wanting to implement an element of randomness to it, affecting the damage output by -20% to +20%.

I set up a datamap within a datamap for a “simple sword” and its stats.

(set: $equip to (dm:"A",(dm:"strm",0.40,"agm",0.60,"1sword",(dm:"wdmg",0.5,"name","simple sword","inv",true))))

So in variable group A, there is the variable group 1sword, and that has the variables wdmg (weapon damage), name, and inv (inventory).

And for the damage output, I got this working, using a surrogate variable “numvar”.

(set:$dmg to ((((random:-2,2)*0.1))*$numvar)+$numvar)

My issue is I can’t figure out how to reference the actual variable “wdmg”, because it’s in two nested datamaps. I know how to reference a variable in ONE datamap, but I don’t know how to do it when it’s A’s 1sword’s variable, not just A’s variable. I can’t find any information and whatever I try returns an error because it’s not reading the variable.

The basic principle is you need to use a 's operator for each level of property reference you are delving into, and a (print:) macro if you want to output the value.

The following simple two level structure has a variable that references a data-map that has a property named “outer”, and that property in turn references a data-map that has a property named “inner”. To access the value of the “inner” property you would do the following.

(set: $variabe to (dm:
	"outer", (dm:
		"inner", 123
	)
))

value: (print: $variabe's outer's inner)

So if we apply the same usage of the 's operator to your own example…

(set: $equip to (dm: 
	"A", (dm: 
		"strm", 0.40,
		"agm", 0.60,
		"1sword", (dm: 
			"wdmg", 0.5, 
			"name", "simple sword", 
			"inv", true
		)
	)
))

1sword's wdmg: (print: $equip's A's 1sword's wdmg)

NOTE: The above tries to answer your question about referencing a property within a nested data-map structure. It doesn’t touch on the potential memory/performance issues of defining and using such structures.

Thank you! ah, I guess I’ll try something else then if that’s an issue with that sort of system.