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

If you have a module that you'd like comments on or would like to know how to create your very own module, post here

Moderator: Moderator

Post Reply
Message
Author
Charidan
Higher
Posts: 64
Joined: Wed Jul 22, 2015 8:12 pm

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

#1 Post 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.
Currently developing the Player AI addon. You can get it from the T-Engine Addon Hub or Steam
You can also view the source code.

stinkstink
Spiderkin
Posts: 543
Joined: Sat Feb 11, 2012 1:12 am

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

#2 Post 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.

HousePet
Perspiring Physicist
Posts: 6215
Joined: Sun Sep 09, 2012 7:43 am

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

#3 Post by HousePet »

Probably an error in your superload.
My feedback meter decays into coding. Give me feedback and I make mods.

Charidan
Higher
Posts: 64
Joined: Wed Jul 22, 2015 8:12 pm

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

#4 Post 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?
Currently developing the Player AI addon. You can get it from the T-Engine Addon Hub or Steam
You can also view the source code.

HousePet
Perspiring Physicist
Posts: 6215
Joined: Sun Sep 09, 2012 7:43 am

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

#5 Post 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.
My feedback meter decays into coding. Give me feedback and I make mods.

Charidan
Higher
Posts: 64
Joined: Wed Jul 22, 2015 8:12 pm

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

#6 Post 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.
Currently developing the Player AI addon. You can get it from the T-Engine Addon Hub or Steam
You can also view the source code.

Charidan
Higher
Posts: 64
Joined: Wed Jul 22, 2015 8:12 pm

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

#7 Post 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.
Currently developing the Player AI addon. You can get it from the T-Engine Addon Hub or Steam
You can also view the source code.

Post Reply