How can I define an empty datamap?

If you are requesting technical assistance with Twine, please specify:
Twine Version: 2.3.14
Story Format: harlowe 3.3.2

How can I define an empty datamap?
for example, I made an empty datamap

(set:$inv to (dm:))

then I try to use it with a (if:) marco,

to see if it is empty,
I tried below:

(if:  (datanames:$inv) is not 0)[(link:'haha')[]]

(if:  $inv is not empty)[(link:'haha')[]]

(if:  (datanames:$inv) is not empty)[(link:'haha')[]]

(if:  (datanames:$inv) is not 0)[(link:'haha')[]]


it all fails,
in short: I tried to hide the link when the datamap is empty, but I don’t how to define an empty datamap in a if marco.

Can someone helps me, thanks!

(if (datanames: $inv)'s length is not 0)) should work: (datanames:) gives you an array, and 's length gives you the number of entries, then you compare against zero.

thanks very much, it seems to work!

In this specific use-case you may find the (unless:) macro a better option.

eg. Show the link unless the Array returned by the (datanames:) macro is empty.

(set: $inv to (dm: "name", "jane"))

(unless: (datanames: $inv)'s length is 0)[
	(link: 'haha')[]
1 Like


But I dont get it why (unless:) is better than (if:) in this situation ?

It seems (unless:) is less frequently used, and I wonder in which circumstance, we have to use (unless:), not (if:)

Could you elaborated a little bit? Thanks!!! :slight_smile:

It’s mostly style or personal preference: if you’re only using the “not” case (no (else:)), it might read better: to say (unless: thing) than to say (if: not thing)

i see thanks!