So, for a jam I'm far too unskilled to have considered, I'm putting together a little game where up to four players can pilot tiny ships and defend a fort. The ships can move about, and (eventually) shoot in the direction of a target that can be rotated around the ship by holding a button. It looks like this in practice:
(Enemies and animations forthcoming.)
Right now, I am attempting to optimize the code, because the player inputs eat a LOT of tokens in their current form. I currently have four different sets of inputs, one for each player, and each also moves a target that is specified as its own object (target1 with .x and .y, target2 with .x and .y, etc.). This is obviously no good, given the immense redundancies.
I think one could probably code a generic input function by making each target a property of its ship, using something like this:
Basically, as long as we specify the controller (c) associated with each player (p), we can use one function to dictate all player inputs. So, for each player, we have to put together a simple table of attributes.
And here is where my problem occurs: those stupid little targets.
Here's what I have tried to use for the first player's attribute table:
It seems PICO-8 doesn't accept putting p1.x, p1.y, and p1.a in those trig functions for p1.targetx and p1.targety. It raises an error, saying that they indicate an attempt to index a nil value. I guess I could just throw in a raw number for them? But I kind of need those variables, I think, since the location of the player's target must depend on the location of the player's ship.
TL;DR: A table isn't working like my noob brain would expect, and I have three questions:
- Why are p1.x, p1.y, and p1.a nil values, if I already provide values for them in the table?
- How else might I define targetx and targety, such that they remain properties of p1?
- If the method I've proposed is stupid, what might you suggest as a workaround?