Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(2 edits) (+2)

Not actually a question, but a thing that tripped me up and was confusing me. I wrote a bit of code to iterate over an array of strings and slow-print them to a canvas, terminal style. But I was finding some bizarre results when I went to test it: the first line would draw fine, but subsequent lines would go bananas, because somehow the Y coord was changing within the inner loop.

Some simplified code looks like this:

each line y in script
 each c x in line
  x*6,y*13
 end
end
> (((0,0),(6,0),(12,0),(18,0),(24,0),(30,0),(36,0),(42,0),(48,0),(54,0),(60,0),(66,0),(72,0),(78,0),(84,0),(90,0)),
   ((0,0),(6,13),(12,26),(18,39),(24,52),(30,65),(36,78),(42,91),(48,104),(54,117),(60,130),(66,143),(72,156),(78,169),(84,182),(90,195),(96,208),(102,221),(108,234),(114,247)),
   ((0,0),(6,26),(12,52)))

Notice how the Y coord keeps going up where it's not supposed to? 

At first I thought it was something weird with how canvas.text[] works, or that i had my offset or loop logic wrong, but then I realized after comparing this to a version without the multiplication:

each line y in script  
 each c x in line   
  x,y    
 end 
end  
> (((0,0),(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0),(8,0),(9,0),(10,0),(11,0),(12,0),(13,0),(14,0),(15,0)),
   ((0,1),(1,1),(2,1),(3,1),(4,1),(5,1),(6,1),(7,1),(8,1),(9,1),(10,1),(11,1),(12,1),(13,1),(14,1),(15,1),(16,1),(17,1),(18,1),(19,1)),
   ((0,2),(1,2),(2,2)))

That looks like what you'd expect, right? 

Turns out the problem is operator precedence. Since Lil can do matrix math, what's happening is it's actually multiplying x by a vector of (6,y), then multiplying that by 13, so you get nonsense. Wrapping the two multiplication operations in parens fixes the problem. It didn't occur to me at first 'cause in most languages, `,` isn't an operator, it's just syntax.

This was enough of a weird surprise I thought I would share with folks, in case y'all run into similar.