Magical Monkey Adventure Devlog #4: Maps

This post is a twofer. First: maps. Second: some code cleanup I did.

Maps

Maps are working now! Scripts can be executed whenever you walk onto a space (which is where the lovely flavor text is coming from) and whenever you try to enter one. These scripts haven’t been set up yet, but that it can be done at all is what matters for now. Since everything’s connected to the scripting system, it’ll be relatively easy to put all the pieces together — a node on the map can send you to a battle or refill your health or give you more worms or whatever else the scripting system will be able to do.

Refactoring

Last week, I mentioned that I’d do some code cleanup, and I did indeed clean up my code. Specifically, I messed with the scripting system, which was basically the ugliest thing in my code. Inside a for loop that iterated through the script, there was a giant switch statement, with a case for each command. In addition to looking ugly, it felt like the wrong way of doing things. Wasn’t there a better way?

Yes, there was! Instead of running through the switch statement, the functions are now stored in an object. The for loop is still there, but it just calls whatever function the command points to, passing in the arguments directly. While I haven’t run the numbers to see if it’s faster, it’s a heck of a lot cleaner and easier to parse, so I feel confident in calling it the better way.

There was one issue with this, though. When the code behind a command was called from inside the for loop, it had access to the current index. This was how I did branching code: through gotos and labels. goto just set the index to the first instance of the matching label in the script array. This wouldn’t work if the code wasn’t in the loop, though, barring making the array index global. (Yuck.)

So I did what I should’ve done a while ago: made a proper if command, based on how conditionals work in Shenzhen I/O. (It might be how things work in actual real-life assembly, too, but I wouldn’t know.) The if command just sets a variable in the scripting system to be either true or false. Lines preceded by plus signs only run if the condition is true; the opposite is true for lines preceded by minus signs.

Here’s some example code using the old method:

jumpif a a==b
log A is not B.
jump end
label a
log A is B.
label end

And here’s some code with the new method that does the same thing:

if a==b
+log A is B.
-log A is not B.

I really should’ve done things this way from the start, but a late improvement is better than continuing to do things the wrong way.

So! That’s what I accomplished this week. Next week: a map of a different sort. (And maybe some worms.)

Magical Monkey Adventure Devlog #1: The Basics

I’ve been reading a fair amount of Shamus Young content lately. (His stuff is up there with The Digital Antiquarian for me. Amazing comfort food reading.) I particularly enjoy his programming project stuff, and since I’ve been working on a game, I got to thinking.

I’m a programmer. I write. I could do that. So I am. Welcome to the Magical Monkey Adventure devlog.

Continue reading

In All Honesty, I Don’t Know Why I Thought The Other Workflow Was Really That Viable

Welcome to the new blog! After years away from WordPress, I’ve finally come back to it. It’s just so much easier; I’m a college student now, and messing around with my FTP stuff whenever I want to post to my blog is a bit much. I’ve got classes to go to!

I’m leaving the old blog up for posterity’s sake. (The post on why I abandoned WordPress for static HTML didn’t age well, did it? I almost wish I had finished it, just so I could know what I was thinking back then.)

This post is just an announcement – “Hey guys, I’m still here!” – so I don’t have anything else to say. Expect more here in the future – I’m working on a couple things right now, so I’m sure I’ll end up having something to talk about.