Page 1 of 1
Call the "rest" action from code
Posted: Sat Sep 19, 2015 9:37 pm
by Charidan
I am trying to get the player to rest as part of a larger action (specifically I'm trying to make a player AI). I clearly don't understand the resting pathway in the code.
Actual question: How do you initiate player resting from the code?
So far I found engine.interface.PlayerRest, which seems to be what I want. However, if I call PlayerRest.initRest() it calls PlayerRest.checkRest(), which is defined as:
Code: Select all
function _M:restCheck()
return false, "player:restCheck() method not defined"
end
Which is clearly wrong (and provides a misleading error message). I am in the process of trying to superload it with a call to mod.class.Player:restCheck(), but I can't seem to effect PlayerRest at all. Is it impossible to superload/overload engine files?
Actual question: How do you initiate player resting from the code?
If it helps, here is the current contents of my superload/engine/interface/PlayerRest.lua:
Code: Select all
local Player = require "mod.class.Player"
local _M = loadPrevious(...)
function _M:restCheck()
game.log("#RED#overload functional") -- Just so I can see an effect in the console
return Player.restCheck()
end
return _M
Re: Call the "rest" action from code
Posted: Sat Sep 19, 2015 11:28 pm
by HousePet
The game initiates resting via: self.player:restInit(), which means call restInit on the player.
PlayerRest.initRest() calls initRest on nothing, which since it doesn't have any resting conditions defined on it, doesn't work.
Also: Hooray you got the right forum this time!

Re: Call the "rest" action from code
Posted: Mon Sep 21, 2015 8:45 pm
by Charidan
Thanks! So if I'm doing this from inside the Player.lua file, do I call _M:restInit()? I tried that, and it gave me an error that ActorTalents.getTalentLevelRaw() indexed a nil "talents" value.
And yeah, I was having trouble seeing the different forum categories. I didn't realize the version separation existed, and I hadn't found the definition of module vs addon yet.
EDIT: Upon further consideration, I may be putting my player AI functions in the wrong file. I really want the instantiation of the player object to work with, and the stuff it calls to tie into the game, not the player class. Of course, this whole experience has been learning how ToME works internally from scratch, so discovering that my very first step was wrong is pretty unsurprising.
Re: Call the "rest" action from code
Posted: Tue Sep 22, 2015 1:09 am
by HousePet
Assuming you want the player to rest, you need to call <the player actor entity>:initRest().
Calling _M:initRest() inside Player.lua is like telling the file to rest.
The player entity can be referenced from anywhere via 'game.player', but it is also frequently 'self'.
Re: Call the "rest" action from code
Posted: Tue Sep 22, 2015 1:27 am
by Charidan
Huh, I thought I tried that, but apparently not.
Thanks again. For some reason "self" isn't defined in my superload of Player.lua, but calling it from game.player works.
Re: Call the "rest" action from code
Posted: Tue Sep 22, 2015 1:49 am
by HousePet
Self is always defined.
Calling a function via : eg. game.player:initRest() means that 'self' inside initRest() is game.player.
Re: Call the "rest" action from code
Posted: Tue Sep 22, 2015 3:19 am
by Charidan
I have an order of execution problem now. I call restInit with
Code: Select all
game.player:restInit(nil,nil,nil,nil,player_ai_after_rest())
which *should* call my player_ai_after_rest() function AFTER the rest has ended, but it turns out not to? I have it printing out log messages, and the log from my after function prints BEFORE the console message "Resting starts...".
How can I check to make sure resting has finished before I execute without blocking or making an infinite loop?
EDIT: Response about "self" stuff: Apparently not, because if i use "self" in my superload/Player.lua directly it gives the error "attempt to index the global value 'self' (a nil value)". Calling things with the colon obviously sets the self value for those function executions, but my local functions don't seem to have a self. It's probably because they're local functions and not _M:functions.
Re: Call the "rest" action from code
Posted: Tue Sep 22, 2015 8:53 am
by HousePet
Remove the () from player_ai_after_rest(). You want to pass the function, not call it.
Re: Call the "rest" action from code
Posted: Tue Sep 22, 2015 7:26 pm
by Charidan
XD XD XD I CAN'T BELIEVE I DID THAT. NOTHING TO SEE HERE NOT A BASIC SYNTAX ERROR OR ANYTHING.
Strange execution bug explained by stupidity. Just. Wow.