Monday, May 23, 2016

The randomized enemy spawner is working!

This bunch of circles might not look like much, but they took me over two days to figure out. The issue was not making them have random values, but wrapping my head around the concept of Lua tables. Lua is great at simplifying things to make the programming flow better. Unfortunately, a byproduct of this is making some concepts more difficult to understand by someone who has very little programming experience. This is the case with tables. They combine arrays and matrices (and probably some other things!) into a single object. This is a great thing for someone who understands how those base concepts work...for someone like me, it is a big hurdle. It didn't help that the Lua documentation focuses solely on the differences between tables and the things they replace...


It took a lot of reading, testing, looking up others' source code and a dash of frustration, but, with help from Pico8 community, I managed to (at least partially) wrap my head around it. The moment it finally clicked felt awesome!

To make it easier on myself, I wrote this randomized enemy spawner as a new program, away from my game code.  I used simple placeholders for most of the elements. That way, nothing else would come in the way of grasping this concept. I think it paid off, because I got it to work and even made some pretty clean looking code:

Edit: I was advised to change the table o from the create_bug function to local table to avoid other functions grabbing this data when they don't need to. I changed that in the code below.

--table for enemies
--enemies are bugs
--hence the name
bugs ={} 

function create_bug()
--responsible for assigning
--random values to enemies
 local o={} --make empty table
 --and fill it with below values
 o.x=flr(rnd(110))+10
 o.y=flr(rnd(50))+10
 o.size=flr(rnd(18))+2
 o.col=flr(rnd(14))

 --add this table to bugs table
 --this way, we have all the enemies
 --stored in one place so they can
 --be accessed all at once.
 add(bugs,o)
 return o
end

function draw_bug (o)
--draw an enemy with values
--established in create_bug
  circfill(o.x,o.y,o.size,o.col)
end 

--spawn enemy on button press
--used for testing the code
--yes, I know the name is 
--somewhat misleading
function debug()
 if btnp(4) then
  create_bug()
 end 
end 

--system functions
function _update()
 debug()
end

function _draw()
 rectfill(0,0,127,127,15) --draw background
 foreach (bugs, draw_bug) --draw all the enemies!
--this function goes through all the enemies
--stored inside bugs table and draws
--them one buy one.

 --show current amount of enemies
 print (#bugs,4,4,1)
end

There are still some small things I do not know exactly why they work, but I know how to make them work, so that's good enough for now. I will continue expanding this code to make those enemies move and possibly not overlay each other, then I can transplant it into the game proper. I hope that by doing so I will figure out the bits I don't fully comprehend.

No comments:

Post a Comment