Someone over in Discord asked about some entity drawing code in Hug Arena, so I took a look a stab at explaining it. I realized the code in Hug Arena is similar to a technique I've used quite a bit, so I put together a small example cartridge demonstrating the technique.
The sample is commented code that demonstrates how to draw two types of objects that update the same way each frame, but draw differently.
In "Remains of El Dorado" I used this for most objects drawn in the game, you can check out that cartridge to see how this works on a larger scale.
Thank you for this. It is really well explained, and it taught me. The : syntax will come in especially handy. I think I need to go through more carts like this, they are a great learning resources.
One question - why do you put calls for update_ball and draw_spr/_square as up[i/] and [i]dr variables inside each entity if those vars are not referenced anywhere else in the code?
...also, there is a Discord for Pico8? I was aware only of two Slacks and the IRC.
edit: wouldn't it be better if this cart was in the workshop category?
Putting the draw_* functions inside of each entity allows us to mix all kinds of entities in one list and call each entity's dr function.
We could have separate lists of boxes and circles. We'd loop over all the boxes and call draw_rect on each one. Then we would loop over each circle and call draw_spr for each one. But when you add more and more different types of entities to your game this method will result in a lot of code to call each entity's draw function.
Instead we put the draw function in the entity's dr variable. Then calling e:dr() will draw the entity with the appropriate function. So now instead of having many loops to draw each type of entity we have one loop that does the correct drawing depending on what's stored in the entity itself.
A cool thing too is that you can mix and match update and draw functions. In El Dorado, I have a few different types of particle effects that all use the same draw function but different update functions. And then there are other entities that have the same update but draw differently, like in this example cart.
[Please log in to post a comment]