Arrays: Linebreaks and "old" code not working

Twine Version: 2.5.1
Story Format: Sugarcube 2.36.1

I was watching Twine 2.0: Creating a Dungeon Crawler -- Part 1 (SugarCube 1.0) - YouTube Dan Cox’s video on YouTube on how to build a Dungeon Crawler in Sugarcube. I have been trying to replicate his method in my own game, but his “MapArray” code does not function for me the way it does for him.

When I tried to write his exact code, using my own Variables, it came back with a <<set error at first. I chalked it up to me having to have done something wrong and started over. (NOTE: I don’t have the code handy to replicate it for you.)

In his video, he has his $MapArray written like this:

<<set $mapArray to
[[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
...(rest of the map)
]>> /* The "1" represents where the player starts out, or Coordinates ($positionX = 1, $positionY = 1) */

In his video, 0’s create a border, or walls, to the dungeon, and 1’s are valid moves the player can make. In my example above, only the players Starting Location is a 1.

He has the $mapArray in its own passage, and displayed in the header of every passage of his dungeon using the (<>) macro.

When his array is displayed, it Linebreaks after every string and has no " , " at all displayed, but when I use the same code structure, it prints every string consecutively, all separated by commas.

What am I doing wrong? I know that he was using Sugarcube 1, but some codes work between the two, and some don’t. This one just doesn’t work the way it used to.

<<set $trenchMapArray to [
[0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 1, 0]
]>>

I need an array, not a property ($object.property) which will contain multiple numerical strings and will display each string on a newline when printed and without commas.

And please, if you can, explain it to me as if I were a child.

Too often, when searching for answers, I end up with more questions, or just totally overwhelmed and confused, than when I started. Such as “_i”. I have researched plenty of functions and I continually see “_i” in the solution, what does the “_i” variable usually represent??? It’s never explicitly addressed, and I know it means something different in every bit of code, that’s just one example of how the answers are not always so easy to use. It’s difficult to learn how a code works if I am continually introduced to extra concepts and functions that are increasingly more complicated, and the documentation for Sugarcube offers little to no answers for questions like that.

Thank you in advance for any help you can offer.

The Twine Cookbook includes a “Moving through a ‘dungeon’”: SugarCube (v2.0) recipe (that was written by Dan), that includes a Download link within the Example section which you can use to download a Story HTML file.

You can open this Story HTML file within a web-browser to see it work.

That Story HTML file can also be imported into the Twine 2.x application to create a project that will contain the Passages that make up a Dungeon Crawler like project.

In Software Development there are habits that have persisted across the years, one of them being common names given to variables used in specific situations, even though these common variable names are (generally) not required for the code to work correctly.

One such example of a commonly used variable name is using the letter i (short for index or iterator) when naming a variable associated with a for loop, and in situations where you have nested for loop the letters i, j, k are commonly used.

note: Mathematic examples themselves also commonly uses the same three letters for similar purposes, so it isn’t just a programming thing.

In SugarCube’s case the underscore before the letter i indicates that the variable is a Temporary Variable.

It must be stressed that while such common variable names may be used in SugarCube related code/examples they have no special meaning in SugarCube. If you want to name the index variable in your <<for>> macro call _banana you can, as it is more important that the variable’s name has meaning in the situation that it’s being used in than that variable’s name following any particular pre-conceived pattern.

1 Like

You might want to rewatch that video more carefully.

He never displays the map arrays themselves. His Location passage only shows the current coordinates within the map ($positionX and $positionY) and valid directional links.

He does build a map display in the second video in that series, but that also does not simply print the map arrays. Rather he loops over the arrays and prints various characters to depict the map.

Variables named i rarely represent anything other than some kind of an index value. They’re extremely common in loops, denoting the loop index.

Since SugarCube variables bear sigils to denote their type, story or temporary, that would be $i or _i in SugarCube.

No documentation is normally going to describe general variable names and what they could possibly mean. That’s something you’re just going to have to pickup,

That said. A quick search of the documentation for _i would have shown that it’s generally used to hold an index value—as seen in the <<for>> macro entry.


Beyond my replies above.

There are far more recent videos by other authors that would probably be better to start with—no offense to Dan, but he wasn’t following best practices even then.

I’d also suggest starting with videos that attempt to teach you the basics first before any that jump straight into building something. Learn the fundamentals first and you’ll have less questions about the concepts you’re seeing—like what the variable i is normally used for.

[quote=“TheMadExile”]
You might want to rewatch that video more carefully.

He never displays the map arrays themselves. His Location passage only shows the current coordinates within the map ($positionX and $positionY) and valid directional links.

He does build a map display in the second video in that series, but that also does not simply print the map arrays. Rather he loops over the arrays and prints various characters to depict the map.[/quote]

You’re right. I must have watched that video 15 times yesterday alone and just never noticed.

This is where terminology becomes important, I suppose.

The _i example was just that: an example.

The point being: Simply “searching” for a solution doesn’t always give you what you need, especially when the posts on the topics are from 2014 and the forum it is being discussed on is Read Only.

As another example, Searching Google for “Creating a map in Sugarcube” yields 0 results for Dungeon-esque maps and a lot of stuff on Button mapping, Image mapping, and a whole lot of other non-relevant stuff for other platforms. Even when I do find something, it’s not quite what I am looking for. I.e Using HTML tables, styling, creating navigational links in images, embedding Google maps…

I just liked Dan’s system and wanted to start there and work on it as I go.

Searching the documentation IS a great help, it has helped me learn a lot in a short amount of time, IF you know what you’re looking for. If you don’t, then it can be really hard to find what you need, especially if you’re looking something that can be achieved multiple ways.

Where’s the fun in that??

I could create simple ‘click here, click here, click here, you win’ games and move on, but I can already do that and I am not learning much, if anything. I want to (spend the time to) create something I would get enjoyment from playing (no matter how long it takes me to get it right). Surely, as a creator yourself, you understand.

The way I like to learn is very hands on. I can see something done a million times(exaggeration), but unless I use it and see the results first hand, I won’t learn anything about it (also an exaggeration).

And, c’mon… It’s very rewarding to get outside your comfort zone and create something from nothing and watching it perform as intended. Every little bit of progress I have made has been through hours of practice, and :musical_note::notes: getting by with a little help from my friends. :notes::musical_note:

I’ve watched the YouTube webinars, Vegetarian Zombie’s Beginner Series, although his ‘intermediate’ series that he alluded to in episode 16(?) doesn’t seem to exist anymore if it ever did.

I have spent nearly 3 days reading various forums, the sub-reddit, the Sugarcube documentation and the Twine cookbook.

And trust me when I say I am searching everything I can before asking here, although, I’m clearly missing a few things.

I just need to know how to make it work, lol. All of that useless stuff out of the way, I do greatly appreciate you taking the time to address this and I’m sure I’ll have more questions in the coming days.