The premise of your mod is that you create "clones" of the player's character. However data.raw.character.character
is not necessarily the only character available. My mod miniMAXIme includes a character selector mode that allows players to change their appearance if they play with third-party character mods (I guess there are about 40 different characters that players could choose from currently.)
A naive way to get the current character is to look for player.character.name
. However, my mod creates different versions of each character if Jetpack or Bob's Character Classes are also active. Now if a player was flying with a jetpack, creating a clone of the flying character would be cheaty because it wouldn't hit obstacles and would go in a straight line to the end of the map. (That's not only cheaty, but would waste UPS and inflate save files because so many chunks would get charted.)
I've just added a new remote function for the next version of my mod. If you call it, it will return either the base name of the player's current character, or the name of the last character used by the player before switching to god mode (this may also work for editor mode iff the player has changed to it from god mode), or nil if the player is not valid or no character has been stored for it. Here's a modified version of your function create_new_character_behind_player
that will try to get the name of the character you create from my mod if it's active:
function create_new_character_behind_player(player, direction)
local player_position = player.position
local offset = {x = -1, y = 0} -- You can adjust the offset as needed
local new_position = {x = player_position.x + offset.x, y = player_position.y + offset.y}
surface = player.surface
local char_name
-- If minime is active, get the base version (ignoring Bob's classes and Jetpack) of the player's
-- current character, or the last character the player had before turning on god mode.
if remote.interfaces.minime and remote.interfaces.minime.get_character_basename then
char_name = remote.call("minime", "get_character_basename", player)
end
-- Got no name from "minime"
if not char_name then
-- Get name from player.character?
if player.character and player.character.valid then
char_name = player.character.name
-- Fall back to default character!
else
char_name = "character"
end
end
-- Spawn a new character
local new_character = surface.create_entity{
name = char_name,
position = new_position,
force = player.force
}
if new_character and new_character.valid then
new_character.walking_state = {walking = true, direction = direction}
new_character.character_running_speed_modifier = settings.global["clone_speed"].value
end
return new_character
end