Page 1 of 1

Using "Player" from load.lua crashes the mod on game start

Posted: Wed Jul 22, 2015 8:34 pm
by Charidan
I made the following simplistic load hook:

Code: Select all

local KeyBind = require "engine.KeyBind"
local Player = require "mod.class.Player"

class:bindHook("ToME:run", function(self, data)
	KeyBind:load("toggle-player-ai")
	game.key:addBinds {
		TOGGLE_PLAYER_AI = function()
		    game.log("#GOLD#Player AI Toggle requested!")
			game.log("#GOLD#Player AI is: %s", Player:ai_active and "#LIGHT_GREEN#enabled" or "#LIGHT_RED#disabled")
			Player:player_ai_start()
		end
	}
end)
If I don't comment out the "require Player" line, the mod crashes between character selection and loading the first level. It gets to 100% and then just doesn't start the game. I have otherwise tested the mod and I can get the keybind to toggle a local boolean, but I'd like to have access to the Player class so I can activate a sort of "autoexplore"-like function.

The Player:ai_active boolean and the Player:player_ai_start() functions are things I added in a superload. Is that confusing the load order and trying to parse functions that don't exist yet? How can I work around that?

I can post the superload/mod/class/Player.lua too if that's relevant.

Re: Using "Player" from load.lua crashes the mod on game sta

Posted: Thu Jul 23, 2015 8:15 am
by stinkstink
If you look in the te4_log.txt file in the same directory as your executable, you should see a detailed crash reason at the end. If none shows up/the log is incomplete, try loading ToME with the --flush-stdout argument.

Re: Using "Player" from load.lua crashes the mod on game sta

Posted: Fri Jul 24, 2015 1:43 am
by HousePet
Probably an error in your superload.

Re: Using "Player" from load.lua crashes the mod on game sta

Posted: Fri Jul 24, 2015 4:35 am
by Charidan
I found this error:

Code: Select all

Lua Error: /mod/class/Actor.lua:258: table index is nil
	At [C]:-1 __newindex
	At /mod/class/Actor.lua:258 init
	At /mod/class/Player.lua:84 init
	At /engine/class.lua:104 new
	At /mod/class/Game.lua:192 newGame
	At /mod/class/Game.lua:136 runReal
	At /mod/class/Game.lua:93 run
	At /engine/Module.lua:990 instanciate
	At /engine/utils.lua:2197 showMainMenu
	At /engine/init.lua:162 
	At [C]:-1 dofile
	At /loader/init.lua:204 
I then tried to add a require for Actor, which added this error (without resolving the above one):

Code: Select all

Lua Error: /mod/class/Game.lua:2206: attempt to index field 'player' (a nil value)
	At [C]:-1 __index
	At /mod/class/Game.lua:2206
Any idea what's going wrong?

Re: Using "Player" from load.lua crashes the mod on game sta

Posted: Fri Jul 24, 2015 10:15 am
by HousePet
At a guess, your require Player line in load.lua is causing Player to initialise too early.
Try moving it into the function where you need it.

Re: Using "Player" from load.lua crashes the mod on game sta

Posted: Fri Jul 24, 2015 9:10 pm
by Charidan
Thanks! I was trying to treat "require" like "include" from C, never would have thought of putting it in a function. Now it's actually running and giving me errors when I try to run the function (function is nil), which I assume is because I didn't define it right.

It tells me that the player_ai_start function is nil, so I need to figure out how to define/call it properly. I tried calling it with Player:player_ai_start() and Player.player_ai_start() to no avail. I tried defining the function as function _M:player_ai_start() and function player_ai_start() without effect.

Disclaimer: I've never used Lua before, so help with the basic syntax may be required.

EDIT: I now know that the colon is for function declaration and it adds a self argument, so I should really be calling things with dot. This hasn't solved my problem, but it has made me less dumb.

Re: Using "Player" from load.lua crashes the mod on game sta

Posted: Thu Sep 17, 2015 9:07 pm
by Charidan
It was an even stupider error. I forgot to add

Code: Select all

superload = true
to my init.lua file.

Issue is now resolved.