I’m making a game with two players. I have created a game object for the first player. They are almost exactly the same with a few differences.
First, they have identical animations but one player will be red and the other will be blue.
Second, they have different keys that they should be listening for input from.
Lastly they are facing opposite directions. (at each other)
I’m wondering how the game objects should be set up. Would it be possible for me to use the same game object twice or should I create two similar game objects per player?
I would set both gameobjects under a main game object with a script attached (so both players can just be a copy paste with diffrent names to tell them apart in script).
In the script you could controll the color, rotation and position . Then you don’t have to worry about inputs because it is all in one script. Thats just my opinion, I like to make it as compact as possible. 
Yes, you can use the same game object (with script) for both. The way I would do it:
-
Have a Script Property for the ‘player index’, a number. You can change this in the editor or at spawn-time to 1 for player 1, 2 for player 2, etc.
-
In your script, have a table of all the data that will be different for each player, and key the data sets to each player index. (data[1] = { color=..., facing=..., etc }; data[2] = {color=..., etc.}
)
-
On init, grab the data for it’s ‘player index’ and use it to set the player’s color, rotation, position, and so on.
-
Include a table of input action names in the player data, with hash keys, something like this:
data[1].input = {
[hash("p1_left")] = "left",
[hash("p1_right")] = "right",
[hash("p1_fire")] = "fire"
}
- In your script’s on_input(), instead of comparing
action_id
directly to hashed strings, use the input table, like so:
function on_input(self, action_id, action)
local input_map = data[self.player_index].input
if input_map[action_id] == "left" then
-- handle "left" key input...
elseif input_map[action_id] == "right" then
-- handle "right" key input...
elseif input_map[action_id] == "fire" then
-- handle "fire" key input...
end
end
2 Likes
To clarify, you’re saying there is one game object with one script. The differences in the two players are accounted for by one index variable used to access a table of data.
Would I still use the collection to spawn in the players, or should I use a factory to create both?
Yes, exactly. Each player would be an instance of the same Game Object with the same script. I’m sure there are other ways to do it of course.
Either way would work fine. If you will always have exactly two players and they will persist throughout a ‘match’, it’s probably easier to just place them in the collection. If you might have a variable number of players, or if they might die and respawn, then spawning them dynamically with a factory is probably the way to go.
The method I’ve outlined is only about how the player script would work. Exactly where you put this script or how you add it to the game doesn’t matter so much. Your players can be a single game object or a collection of game objects, etc.
2 Likes