Call the "rest" action from code

All development conversation and discussion takes place here

Moderator: Moderator

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

Call the "rest" action from code

#1 Post 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
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: Call the "rest" action from code

#2 Post 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! :D
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: Call the "rest" action from code

#3 Post 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.
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: Call the "rest" action from code

#4 Post 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'.
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: Call the "rest" action from code

#5 Post 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.
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: Call the "rest" action from code

#6 Post by HousePet »

Self is always defined.
Calling a function via : eg. game.player:initRest() means that 'self' inside initRest() is game.player.
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: Call the "rest" action from code

#7 Post 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.
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: Call the "rest" action from code

#8 Post by HousePet »

Remove the () from player_ai_after_rest(). You want to pass the function, not call 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: Call the "rest" action from code

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