Help, game error - no character

Place to get help with not working mods / modding interface.
sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Help, game error - no character

Post by sdgmlj »

I wrote a script to adjust character attributes. but an error will be reported when starting a new game. "No character" I can't understand the rest
script.on_event(defines.events.on_player_created, function(event)
local player = game.players[event.player_index]
player.character_running_speed_modifier = 5
player.character_inventory_slots_bonus = 100
。。。。。。
end)
There is no error when entering the old game, but it cannot be read automatically
What other scripts do I need to add?
Thank you

Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help, game error - no character

Post by Pi-C »

You can have a player that is not connected to a character, for example in god or editor mode. Since Factorio 1.0 the first player created won't be attached to a character until the cutscene has finished. There's also an event for it, on_cutscene_cancelled. While the cutscene is running, player.character will be nil, but there will be player.cutscene_character. Both player.character and player.cutscene_character are exclusive, so only at most one of them will point to an entity.

In miniMAXIme, I use this to delay operations that depend on a character:

Code: Select all

minime_player.on_player_joined_game = function(event)
  local player = game.players[event.player_index]

  local force = player.force
  if force and force.valid and not(global.researched_by[force.name]) then
    minime_player.init_force_data(force)
  end


  -- We're still in a cutscene. Wait until it is finished!
  if player.cutscene_character then
    minime.writeDebug("Player has no character yet. Waiting for cutscene to finish!")
    script.on_event(defines.events.on_cutscene_cancelled, function(event)
      minime.entered_event(event)

      minime_player.init_player(event)

      minime.entered_event(event, "leave")
    end)

  -- Cutscene has finished or wasn't played. Initialize player immediately!
  else
    minime.writeDebug("Initializing player %s%s!",
      {player.index, player.name and " ("..player.name..")" or ""})
    minime_player.init_player(event)

    -- We can stop listening to on_cutscene_cancelled now!
    script.on_event(defines.events.on_cutscene_cancelled, nil)
    minime.writeDebug("Unregistered handler for event \"on_cutscene_cancelled\"!")
  end
end
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

Pi-C wrote:
Sun Mar 13, 2022 3:58 pm
You can have a player that is not connected to a character, for example in god or editor mode. Since Factorio 1.0 the first player created won't be attached to a character until the cutscene has finished. There's also an event for it, on_cutscene_cancelled. While the cutscene is running, player.character will be nil, but there will be player.cutscene_character. Both player.character and player.cutscene_character are exclusive, so only one of them will point to an entity.

In miniMAXIme, I use this to delay operations that depend on a character:

Code: Select all

minime_player.on_player_joined_game = function(event)
  local player = game.players[event.player_index]

  local force = player.force
  if force and force.valid and not(global.researched_by[force.name]) then
    minime_player.init_force_data(force)
  end


  -- We're still in a cutscene. Wait until it is finished!
  if player.cutscene_character then
    minime.writeDebug("Player has no character yet. Waiting for cutscene to finish!")
    script.on_event(defines.events.on_cutscene_cancelled, function(event)
      minime.entered_event(event)

      minime_player.init_player(event)

      minime.entered_event(event, "leave")
    end)

  -- Cutscene has finished or wasn't played. Initialize player immediately!
  else
    minime.writeDebug("Initializing player %s%s!",
      {player.index, player.name and " ("..player.name..")" or ""})
    minime_player.init_player(event)

    -- We can stop listening to on_cutscene_cancelled now!
    script.on_event(defines.events.on_cutscene_cancelled, nil)
    minime.writeDebug("Unregistered handler for event \"on_cutscene_cancelled\"!")
  end
end
Thank you. My first question has been solved, but there is another question:
My module will not be automatically read when running the old archive. If I add the function to "configuration_changed", an error will be reported. How should I change it?
script.on_event(defines.events.on_cutscene_cancelled, function(event)
character_init(event)
end)
--This one is correct, but the following one will report an error
script.on_configuration_changed(function(event)
character_init(event)
end)
How should I change it?

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

bad argument #3 of 3 to '__index' (string expected, got nil)

Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help, game error - no character

Post by Pi-C »

sdgmlj wrote:
Sun Mar 13, 2022 5:34 pm
script.on_event(defines.events.on_cutscene_cancelled, function(event)
character_init(event)
end)
--This one is correct, but the following one will report an error
script.on_configuration_changed(function(event)
character_init(event)
end)
How should I change it?
The arguments passed on to the functions differ for the two events. on_cutscene_cancelled will give you

Code: Select all

{
	player_index:: uint
	name:: defines.events
	tick:: uint
}
on_configuration_changed will pass on

Code: Select all

{
	old_version:: string?
	new_version:: string?
	mod_changes:: dictionary[string → ModChangeData]
	mod_startup_settings_changed:: boolean
	migration_applied:: boolean
}
None of the event data of on_configuration_changed hold references to any player, so you can't use it to call your function. But you could do this:

Code: Select all

script.on_configuration_changed(function(event)
 	for p, player in pairs(game.players) do
 		character_init({player_index = player.index})
 	end
end) 
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

Pi-C wrote:
Sun Mar 13, 2022 6:02 pm
sdgmlj wrote:
Sun Mar 13, 2022 5:34 pm
script.on_event(defines.events.on_cutscene_cancelled, function(event)
character_init(event)
end)
--This one is correct, but the following one will report an error
script.on_configuration_changed(function(event)
character_init(event)
end)
How should I change it?
The arguments passed on to the functions differ for the two events. on_cutscene_cancelled will give you

Code: Select all

{
	player_index:: uint
	name:: defines.events
	tick:: uint
}
on_configuration_changed will pass on

Code: Select all

{
	old_version:: string?
	new_version:: string?
	mod_changes:: dictionary[string → ModChangeData]
	mod_startup_settings_changed:: boolean
	migration_applied:: boolean
}
None of the event data of on_configuration_changed hold references to any player, so you can't use it to call your function. But you could do this:

Code: Select all

script.on_configuration_changed(function(event)
 	for p, player in pairs(game.players) do
 		character_init({player_index = player.index})
 	end
end) 
Thank you. My problem has been solved.
The friends in this forum are very enthusiastic and have helped me solve many problems. I'm really happy!
I wish you a happy life! friend :D :D :D

Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help, game error - no character

Post by Pi-C »

sdgmlj wrote:
Sun Mar 13, 2022 6:21 pm
Thank you. My problem has been solved.
The friends in this forum are very enthusiastic and have helped me solve many problems. I'm really happy!
You're welcome! Glad I could help.
I wish you a happy life! friend :D :D :D
Thanks, same to you!

By the way, if your mod is ready, post a link to it, please! You're dealing with characters there, so I'd like to check if I must make adjustments for it in my mod …
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

Pi-C wrote:
Mon Mar 14, 2022 4:12 pm
sdgmlj wrote:
Sun Mar 13, 2022 6:21 pm
Thank you. My problem has been solved.
The friends in this forum are very enthusiastic and have helped me solve many problems. I'm really happy!
You're welcome! Glad I could help.
I wish you a happy life! friend :D :D :D
Thanks, same to you!

By the way, if your mod is ready, post a link to it, please! You're dealing with characters there, so I'd like to check if I must make adjustments for it in my mod …
Excuse me, can you excuse me again? I have another problem:

When using "jetpack" of "Se", the bonus of some character attributes will disappear. What is the way to reload my configuration when using "jetpack" and what script should be added?

Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help, game error - no character

Post by Pi-C »

sdgmlj wrote:
Thu Mar 17, 2022 1:52 pm
When using "jetpack" of "Se", the bonus of some character attributes will disappear. What is the way to reload my configuration when using "jetpack" and what script should be added?
Your bonuses will disappear because the character will disappear. Jetpack can't magically let a random character fly, so it makes a copy of all prototypes and modifies that copy (data stage). In control, whenever a player activates the jetpack, the character entity used by the player is removed and replaced with a new entity based on the flying version.

Jetpack will inform other mods when it has exchanged a character. The way it works, other mods are required to create a remote interface and add a function called on_character_swapped. So you could use this:

Code: Select all

remote.add_interface("your_interface_name", {
	on_character_swapped = function(event)
	
		new_id = event.new_unit_number
		old_id = event.old_unit_number
		new_char = event.new_character
		old_char = event.old_character
	
		-- Do stuff
	
	end,
})
Make sure to check that new_char and old_char still exist and are valid before trying to do something with them! The safe thing to do is storing properties of a character in the global table, indexed by the character's unit_number (e.g. global.characters[unit_number]). This way, you can apply changes you've made to the old character's to the new one.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 315
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: Help, game error - no character

Post by Stringweasel »

The Jetpack mod also messes with characters. When you enable the jetpack it destroys your character and creates a new one wearing the jetpack. So your mod will have to listen to that remote event. Inside the jetpack script it has this function which calls raise_event

Code: Select all

raise_event(Jetpack.on_character_swapped_event, {
      new_unit_number = new.unit_number,
      old_unit_number = old.unit_number,
      new_character = new,
      old_character = old
})
You can handle it in your mod like this

Code: Select all

remote.add_interface("your-mode-name", {
  on_character_swapped = function(data)
    -- Transfer information from data.old_character to data.new_character
  end
})
I think this is how it works. Never used these interfaces before. Hope it helps! :)
Alt-F4 Author | Factorio Modder
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby

Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help, game error - no character

Post by Pi-C »

Stringweasel wrote:
Thu Mar 17, 2022 2:35 pm
Inside the jetpack script it has this function which calls raise_event
You're mistaken there! The function raise_event() called by jetpack is a local function (see control.lua, lines 6ff.):

Code: Select all

function raise_event(event_name, event_data)
  local responses = {}
  for interface_name, interface_functions in pairs(remote.interfaces) do
      if interface_functions[event_name] then
          responses[interface_name] = remote.call(interface_name, event_name, event_data)
      end
  end
  return responses
end
Unlike vanilla's raise_event(), it doesn't raise an event at all, but checks all remote interfaces for a function on_character_swapped_event.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 315
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: Help, game error - no character

Post by Stringweasel »

Pi-C wrote:
Thu Mar 17, 2022 2:45 pm
Unlike vanilla's raise_event(), it doesn't raise an event at all, but checks all remote interfaces for a function on_character_swapped_event.
Ahh okay. That's kinda cool. So instead of blindly raising an event and not caring if someone is listening, it's rather checking if there is anyone listening for a on_character_swapped_event event. Thanks for pointing that out!

That doesn't change what sdgmlj should do in his mod though :)
Alt-F4 Author | Factorio Modder
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

Pi-C wrote:
Thu Mar 17, 2022 2:31 pm
sdgmlj wrote:
Thu Mar 17, 2022 1:52 pm
When using "jetpack" of "Se", the bonus of some character attributes will disappear. What is the way to reload my configuration when using "jetpack" and what script should be added?
Your bonuses will disappear because the character will disappear. Jetpack can't magically let a random character fly, so it makes a copy of all prototypes and modifies that copy (data stage). In control, whenever a player activates the jetpack, the character entity used by the player is removed and replaced with a new entity based on the flying version.

Jetpack will inform other mods when it has exchanged a character. The way it works, other mods are required to create a remote interface and add a function called on_character_swapped. So you could use this:

Code: Select all

remote.add_interface("your_interface_name", {
	on_character_swapped = function(event)
	
		new_id = event.new_unit_number
		old_id = event.old_unit_number
		new_char = event.new_character
		old_char = event.old_character
	
		-- Do stuff
	
	end,
})
Make sure to check that new_char and old_char still exist and are valid before trying to do something with them! The safe thing to do is storing properties of a character in the global table, indexed by the character's unit_number (e.g. global.characters[unit_number]). This way, you can apply changes you've made to the old character's to the new one.
I just want to restore my previous setting data after using "jetpack" to return to the ground.

therefore
remote.add_interface("your_interface_name", {
on_character_swapped = function(event)

new_id = event.new_unit_number
old_id = event.old_unit_number
new_char = event.new_character
old_char = event.old_character

-- Do stuff

end,
})

应该添加在“control.lua”的什么地方呢?

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

function character_init(event)
local player = game.players[event.player_index]

if player.character then
player.character_running_speed_modifier = running_speed_modifier
.........
end
end

Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help, game error - no character

Post by Pi-C »

sdgmlj wrote:
Thu Mar 17, 2022 3:13 pm
应该添加在“control.lua”的什么地方呢?
Sorry, I don't know what that means. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help, game error - no character

Post by Pi-C »

sdgmlj wrote:
Thu Mar 17, 2022 3:16 pm
function character_init(event)
local player = game.players[event.player_index]

if player.character then
player.character_running_speed_modifier = running_speed_modifier
.........
end
end
Seems like you can keep it as simple as this:

Code: Select all

remote.add_interface("your_interface_name", {
	on_character_swapped = function(event)
	
		local char = event.new_character
		if char and char.valid then
			char.character_running_speed_modifier = running_speed_modifier
		end
	end,
})
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

Pi-C wrote:
Thu Mar 17, 2022 4:23 pm
sdgmlj wrote:
Thu Mar 17, 2022 3:16 pm
function character_init(event)
local player = game.players[event.player_index]

if player.character then
player.character_running_speed_modifier = running_speed_modifier
.........
end
end
Seems like you can keep it as simple as this:

Code: Select all

remote.add_interface("your_interface_name", {
	on_character_swapped = function(event)
	
		local char = event.new_character
		if char and char.valid then
			char.character_running_speed_modifier = running_speed_modifier
		end
	end,
})
ok! The problem has been solved :D :D :D

You are my teacher.

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

Pi-C wrote:
Thu Mar 17, 2022 4:23 pm
sdgmlj wrote:
Thu Mar 17, 2022 3:16 pm
function character_init(event)
local player = game.players[event.player_index]

if player.character then
player.character_running_speed_modifier = running_speed_modifier
.........
end
end
Seems like you can keep it as simple as this:

Code: Select all

remote.add_interface("your_interface_name", {
	on_character_swapped = function(event)
	
		local char = event.new_character
		if char and char.valid then
			char.character_running_speed_modifier = running_speed_modifier
		end
	end,
})
Hello, may I bother you again? I have encountered a new problem. Why is "on_player_repawned" invalid in SE module? My module adjusts character attributes. It has no problem in the original game, but in se, it cannot be read correctly after resurrection
script.on_event(defines.events.on_player_respawned, function(event)
character_init(event)
end)
How can I modify it

Pi-C
Smart Inserter
Smart Inserter
Posts: 1645
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help, game error - no character

Post by Pi-C »

I haven't found yet how SE bypasses on_player_respawned, but I've found this in the changelog:

Code: Select all

---------------------------------------------------------------------------------------------------
Version: 0.4.10
Date: 22. 09. 2020
…

  Compatibility:
    - Added remote interface for get_on_player_respawned_event. on_player_respawned returns the player index after the player has been given a fresh character on the proper surface.
Also, there's this at the top of scripts/respawn.lua:

Code: Select all

--custon event
--/c remote.call("space-exploration", "get_on_player_respawned_event")
--script.on_event(remote.call("space-exploration", "get_on_player_respawned_event"), my_event_handler)
So you must add another function to your remote interface a handler for SE's custom event. But thanks for finding this! This interferes with my mod as well, so now I know about it, I can fix it. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

sdgmlj
Fast Inserter
Fast Inserter
Posts: 127
Joined: Sun Jul 04, 2021 11:12 pm
Contact:

Re: Help, game error - no character

Post by sdgmlj »

Pi-C wrote:
Sun Apr 10, 2022 8:00 pm
I haven't found yet how SE bypasses on_player_respawned, but I've found this in the changelog:

Code: Select all

---------------------------------------------------------------------------------------------------
Version: 0.4.10
Date: 22. 09. 2020
…

  Compatibility:
    - Added remote interface for get_on_player_respawned_event. on_player_respawned returns the player index after the player has been given a fresh character on the proper surface.
Also, there's this at the top of scripts/respawn.lua:

Code: Select all

--custon event
--/c remote.call("space-exploration", "get_on_player_respawned_event")
--script.on_event(remote.call("space-exploration", "get_on_player_respawned_event"), my_event_handler)
So you must add another function to your remote interface a handler for SE's custom event. But thanks for finding this! This interferes with my mod as well, so now I know about it, I can fix it. :-)
I don't quite understand how I can add it
script.on_event(defines.events.on_player_respawned, function(event)
character_init(event)
end)
function character_init(event)
local player = game.players[event.player_index]
player.character_running_speed_modifier = 2

end

Post Reply

Return to “Modding help”