I think I made an obvious mistake in control.lua

Place to get help with not working mods / modding interface.
Post Reply
Sigor
Burner Inserter
Burner Inserter
Posts: 19
Joined: Sun Nov 20, 2016 5:07 pm
Contact:

I think I made an obvious mistake in control.lua

Post 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?

User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2124
Joined: Thu Jun 12, 2014 3:05 am
Contact:

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

Post by Ranakastrasz »

Player doesnt exist anymore I think. Its a table called players instead.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16

Pandemoneus
Fast Inserter
Fast Inserter
Posts: 127
Joined: Fri May 08, 2015 2:25 pm
Contact:

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

Post 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
.
My RSO+Bob's+Angel's modpack: Farlands (outdated)
Mods (current): Resource Labels
Mods (old): Biter Spires

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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)

Sigor
Burner Inserter
Burner Inserter
Posts: 19
Joined: Sun Nov 20, 2016 5:07 pm
Contact:

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

Post by Sigor »

Thanks so much!

Post Reply

Return to “Modding help”