Page 1 of 1

I think I made an obvious mistake in control.lua

Posted: Mon May 29, 2017 9:22 pm
by Sigor

Code: Select all

--control.lua

require "util"

script.on_event({defines.events.on_tick},
   function (e)
	if game.player.character.vehicle.name ~= nil
	then if game.player.character.vehicle.name == "car"
	and game.player.character.vehicle.grid.avabile_in_batteries == 0
	then game.player.character.vehicle.speed = 0 end end
end)
I want the car to only start if there is any stored energy in it's equipment grid.
When I enter the game, that error occurs:

Code: Select all

Error while running event EGMOT::on_tick (ID 0)
__EGMOT__/control.lua:7: attempt to index field 'player' (a nil value)
EGMOT is the name of my mod (Equipment Grids for Means Of Transport... such a bad name...)
Can you plase help me and tell me what's wrong?
Should I include any logs?

Re: I think I made an obvious mistake in control.lua

Posted: Mon May 29, 2017 9:35 pm
by Ranakastrasz
Player doesnt exist anymore I think. Its a table called players instead.

Re: I think I made an obvious mistake in control.lua

Posted: Mon May 29, 2017 9:40 pm
by Pandemoneus
You should only really use game.player when the mod is meant for single player only.
Generally, you want to iterate over all players with

Code: Select all

for index, player in pairs(game.players) do
   ...
end
.

Re: I think I made an obvious mistake in control.lua

Posted: Mon May 29, 2017 11:34 pm
by Nexela
Pandemoneus wrote:You should only really use game.player when the mod is meant for single player only.
There is no such thing as a mod that is single player only, only mods that are broken in multiplayer

Also game.player is the player typing at the console. In a mod it will only be available in a very few places.


When iterating players if you don't need offline players use game.connected_players

Code: Select all

script.on_event({defines.events.on_tick},
   function (e)
   for _, player in pairs(game.connected_players) do
     if player.vehicle and player.vehicle.name == "car"  -- This limits it just to "the vanilla car"
     and player.vehicle.grid and player.vehicle.grid.avabile_in_batteries == 0 then 
       player.vehicle.speed = 0 
    end 
  end
end)

Re: I think I made an obvious mistake in control.lua

Posted: Tue May 30, 2017 1:17 pm
by Sigor
Thanks so much!