GUI mod interface help

Place to get help with not working mods / modding interface.
Post Reply
arc_adan
Manual Inserter
Manual Inserter
Posts: 1
Joined: Thu May 24, 2018 12:31 am
Contact:

GUI mod interface help

Post by arc_adan »

Recently I created my first mod. The goal of the mod is to use a checkbox element to switch you from a player to an enemy and back.

The mod can be found here: https://mods.factorio.com/mod/ArcEnemySwitch

I'm not sure what I'm doing wrong but it appears to be in the control.lua file
The code for my control.lua file is

Code: Select all

script.on_init(function()
	init_gui()
	--game.player.gui.top.add{type="label", name="modinfo", caption="Arc's Switch Mod"}
     --   game.player.gui.top.add{type="checkbox", name="enemyswitch", caption="Enemy Switch", state= "false", enabled="true"}
	--	game.player.gui.top.enemyswitch.caption = "Enemy/Friendly Switch"
end)

script.on_load(function()
	init_gui()
end)

function init_gui()
	--for _, player in pairs(game.players) do
	if not player.gui.top.enemyswitch then
		game.player.gui.top.add{type="label", name="modinfo", caption="Arc's Switch Mod"}
        game.player.gui.top.add{type="checkbox", name="enemyswitch", caption="Enemy Switch", state= "false", enabled="true"}
		game.player.gui.top.enemyswitch.caption = "Enemy/Friendly Switch"
    end
    end


script.on_event({defines.events.on_gui_checked_state_changed},
   function (e)
   local switch_bool = game.player.gui.top.enemyswitch.state
		if e.element == "enemyswitch" then
			if switch_bool == true then
				game.players[e.player_index].force = game.forces["enemy"]
			end
			if switch_bool == false then
				game.players[e.player_index].force = game.forces["player"]
			end
		end
   end)
And when I try to open a map, I get this error:
Error while running on_load: __ArcEnemySwitch__/control.lua:15: attempt to index global 'player' (a nil value)

I'm not sure how to fix this. I'm new to Lua so I'm not the best at it. I know a lot of the commands and what not but I'm not solid on how they work together just yet so I apologize for that.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: GUI mod interface help

Post by darkfrei »

function init_gui()
Here is comment! -- for _, player in pairs(game.players) do
if not player.gui.top.enemyswitch then

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2633
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: GUI mod interface help

Post by steinio »

arc_adan wrote:Recently I created my first mod. The goal of the mod is to use a checkbox element to switch you from a player to an enemy and back.

The mod can be found here: https://mods.factorio.com/mod/ArcEnemySwitch

I'm not sure what I'm doing wrong but it appears to be in the control.lua file
The code for my control.lua file is

Code: Select all

script.on_init(function()
	init_gui()
	--game.player.gui.top.add{type="label", name="modinfo", caption="Arc's Switch Mod"}
     --   game.player.gui.top.add{type="checkbox", name="enemyswitch", caption="Enemy Switch", state= "false", enabled="true"}
	--	game.player.gui.top.enemyswitch.caption = "Enemy/Friendly Switch"
end)

script.on_load(function()
	init_gui()
end)

function init_gui()
	--for _, player in pairs(game.players) do
	if not player.gui.top.enemyswitch then
		game.player.gui.top.add{type="label", name="modinfo", caption="Arc's Switch Mod"}
        game.player.gui.top.add{type="checkbox", name="enemyswitch", caption="Enemy Switch", state= "false", enabled="true"}
		game.player.gui.top.enemyswitch.caption = "Enemy/Friendly Switch"
    end
    end


script.on_event({defines.events.on_gui_checked_state_changed},
   function (e)
   local switch_bool = game.player.gui.top.enemyswitch.state
		if e.element == "enemyswitch" then
			if switch_bool == true then
				game.players[e.player_index].force = game.forces["enemy"]
			end
			if switch_bool == false then
				game.players[e.player_index].force = game.forces["player"]
			end
		end
   end)
And when I try to open a map, I get this error:
Error while running on_load: __ArcEnemySwitch__/control.lua:15: attempt to index global 'player' (a nil value)

I'm not sure how to fix this. I'm new to Lua so I'm not the best at it. I know a lot of the commands and what not but I'm not solid on how they work together just yet so I apologize for that.
Ah there you are.

You sticked out in a negative way due your massive uploads to the mod portal - don't' take it personally, it didn't hurt anyone.
Just want to say you don't need to upload the mod to test it.

player is only useable in console. http://lua-api.factorio.com/latest/LuaG ... ipt.player

You need to use something like game.players[index] and iterate over each player seperatly.
Maybe there is more with your code so i recommend to join the discord #mod-making channel (https://discordapp.com/invite/factorio) and talk with experienced modders (i mean not implicitly me) about it.

Cu, steinio.
Image

Transport Belt Repair Man

View unread Posts

Helfima
Fast Inserter
Fast Inserter
Posts: 199
Joined: Tue Jun 28, 2016 11:40 am
Contact:

Re: GUI mod interface help

Post by Helfima »

I have builded a big interface and to use the player you can make lua modules.
I use adapter pattern when the api changed I need change only my adapter

module for Player
https://github.com/Helfima/helmod/blob/ ... Player.lua
module for Events
https://github.com/Helfima/helmod/blob/ ... /Event.lua

so console file is very simple lol
https://github.com/Helfima/helmod/blob/ ... ontrol.lua

after when i need player properties i juste need

Code: Select all

-- get lua_player
local lua_player = Player.native()
-- get a recipe prototype
local lua_prototype = Player.getRecipe(object_name)

Post Reply

Return to “Modding help”