ToME: the Tales of Maj'Eyal
http://forums.te4.org/

Call the "rest" action from code
http://forums.te4.org/viewtopic.php?f=36&t=44914
Page 1 of 1

Author:  Charidan [ Sat Sep 19, 2015 9:37 pm ]
Post subject:  Call the "rest" action from code

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:
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:
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

Author:  HousePet [ Sat Sep 19, 2015 11:28 pm ]
Post subject:  Re: Call the "rest" action from code

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! :D

Author:  Charidan [ Mon Sep 21, 2015 8:45 pm ]
Post subject:  Re: Call the "rest" action from code

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.

Author:  HousePet [ Tue Sep 22, 2015 1:09 am ]
Post subject:  Re: Call the "rest" action from code

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'.

Author:  Charidan [ Tue Sep 22, 2015 1:27 am ]
Post subject:  Re: Call the "rest" action from code

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.

Author:  HousePet [ Tue Sep 22, 2015 1:49 am ]
Post subject:  Re: Call the "rest" action from code

Self is always defined.
Calling a function via : eg. game.player:initRest() means that 'self' inside initRest() is game.player.

Author:  Charidan [ Tue Sep 22, 2015 3:19 am ]
Post subject:  Re: Call the "rest" action from code

I have an order of execution problem now. I call restInit with
Code:
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.

Author:  HousePet [ Tue Sep 22, 2015 8:53 am ]
Post subject:  Re: Call the "rest" action from code

Remove the () from player_ai_after_rest(). You want to pass the function, not call it.

Author:  Charidan [ Tue Sep 22, 2015 7:26 pm ]
Post subject:  Re: Call the "rest" action from code

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.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/