ToME: the Tales of Maj'Eyal

Everything about ToME
It is currently Sat Oct 20, 2018 4:53 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Mon Feb 12, 2018 2:36 am 
Offline
Wayist

Joined: Fri May 16, 2014 6:30 pm
Posts: 29
I was trying to edit one function in mod/resolvers.lua. The function was named as:

function resolvers.calc.moddable_tile(t, e) -- located in line 545 of that file.

So what I did was I make a new file with the same name and directory under superload and did the following:

local _M = loadPrevious(...)

function _M:resolvers.calc.moddable_tile(t, e)

-- Here I copied the whole original function and made some very simple modifications. Just changing some strings, actually.

end

return _M

However this did not work at all. The game launched just as the plugin was not activated. I also checked all possible reasons that I could think of, such as superload = false. But I cannot find out how to solve this problem. Can any of you tell me what's wrong? Thanks a lot.


Top
 Profile  
 
PostPosted: Mon Feb 12, 2018 7:22 am 
Offline
Uruivellas

Joined: Mon Oct 09, 2006 7:47 pm
Posts: 615
Most mod/engines files have the following structure

require(...)
function _M:foo(...)
function _M:bar(...)

In that case, il you use the usual superload mechanism

local _M = loadPrevious(...)
-- it defines the local _M array and includes the original file.
-- As a result, all the _M:whatever function/var will be part of the local _M array
-- you can then do all you want including redefining function already defined
local old_foo = _M.foo
function _M:foo (....)
do something
local ret = old_foo()
etc
end
return _M

At the end, you send the array to the superload mechanism and its puts back the content of _M to its normal scope.

But, if functions in the superloaded file are not defined as part of an array, the following mechanism does not work.

In mod/resolvers.lua functions are
function resolvers.equip(t)
not
function _M:resolvers.equip(t)

So defining a local _M array before (super)loading the file is useless, and defining functions in _M even more.
All your mods will be completely ignored.

I think, what you want could be done

by

local _M = loadPrevious(...) --local _M is useless, but it does not hurt and who knows.... Probably the superload expects an array returned

function resolvers.calc.moddable_tile(t, e)
-- do whatever you want
end

return _M

As resolvers.calc.modable is not defined as _M:resolvers.calc...it will be directly redefined and it should work.

But beware.
A double load of the file may have unexpected results
And this is not good practice to directly redefine a function. If another addon does the same thing, its modifications (or yours) will be lost.


Top
 Profile  
 
PostPosted: Mon Feb 12, 2018 3:11 pm 
Offline
Wayist

Joined: Fri May 16, 2014 6:30 pm
Posts: 29
Thank you for your detailed analysis. However even I removed the _M: just as you said, it still did not work at all.

I may have to use overload instead, though I really hate overload, LOL.

ghostbuster wrote:
Most mod/engines files have the following structure

require(...)
function _M:foo(...)
function _M:bar(...)

In that case, il you use the usual superload mechanism

local _M = loadPrevious(...)
-- it defines the local _M array and includes the original file.
-- As a result, all the _M:whatever function/var will be part of the local _M array
-- you can then do all you want including redefining function already defined
local old_foo = _M.foo
function _M:foo (....)
do something
local ret = old_foo()
etc
end
return _M

At the end, you send the array to the superload mechanism and its puts back the content of _M to its normal scope.

But, if functions in the superloaded file are not defined as part of an array, the following mechanism does not work.

In mod/resolvers.lua functions are
function resolvers.equip(t)
not
function _M:resolvers.equip(t)

So defining a local _M array before (super)loading the file is useless, and defining functions in _M even more.
All your mods will be completely ignored.

I think, what you want could be done

by

local _M = loadPrevious(...) --local _M is useless, but it does not hurt and who knows.... Probably the superload expects an array returned

function resolvers.calc.moddable_tile(t, e)
-- do whatever you want
end

return _M

As resolvers.calc.modable is not defined as _M:resolvers.calc...it will be directly redefined and it should work.

But beware.
A double load of the file may have unexpected results
And this is not good practice to directly redefine a function. If another addon does the same thing, its modifications (or yours) will be lost.


Top
 Profile  
 
PostPosted: Tue Feb 13, 2018 12:55 am 
Offline
Perspiring Physicist

Joined: Sun Sep 09, 2012 7:43 am
Posts: 5759
Unless I'm really confused (which does happen), I think your problem here is that mod/resolvers.lua does not exist.
when superloading via superload/mod/... you are superloading a file which is inside the class folder of the module file. Resolvers.lua is not in that folder.

I think I have edited a resolver before, so I'll try to remember to look in my addons when I'm at home for how to do it.

_________________
My feedback meter decays into coding. Give me feedback and I make mods.


Top
 Profile  
 
PostPosted: Tue Feb 13, 2018 4:36 am 
Offline
Wayist

Joined: Fri May 16, 2014 6:30 pm
Posts: 29
I double checked the tome-1.5.5 module and there is a file tome-1.5.5.team\mod\resolvers.lua, and overloading this file does work so I believe the problem is how to edit the file by superloading it.

HousePet wrote:
Unless I'm really confused (which does happen), I think your problem here is that mod/resolvers.lua does not exist.
when superloading via superload/mod/... you are superloading a file which is inside the class folder of the module file. Resolvers.lua is not in that folder.

I think I have edited a resolver before, so I'll try to remember to look in my addons when I'm at home for how to do it.


Top
 Profile  
 
PostPosted: Tue Feb 13, 2018 6:23 am 
Offline
Perspiring Physicist

Joined: Sun Sep 09, 2012 7:43 am
Posts: 5759
Okay I am confused, and I don't seem to have any code that edits a resolver.

This would be easier if you had pasted your code in this thread, but anyway:

You don't need to superload the file to edit these functions.
You should be able to overwrite them at any time in your code. The best time for "any time" is in your addon's "ToME:load" hook.
By the time that hook is called, all resolvers should have been created but not yet resolved, and they are all stored in a table that can be accessed by just typing resolvers.whichever_on_you_want...

So add in something like:
Code:
resolvers.calc.moddable_tile = function(t, e)

whatever you want...

end


Note that this is basically overwriting the function, so if anyone else does the same thing there is no protections for allowing both of you to do that safely.

_________________
My feedback meter decays into coding. Give me feedback and I make mods.


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 3:55 am 
Offline
Wayist

Joined: Fri May 16, 2014 6:30 pm
Posts: 29
Thank you for your great guidance. Now I'm doing something like:

in hooks/load.lua:

class:bindHook("ToME:load", function(self, data)
function resolvers.calc.moddable_tile(t, e)

--Some modifications

end
end)

This seems to be working fine. But is this exactly what you meant? I just want to make sure that I do not make any mistakes.


HousePet wrote:
Okay I am confused, and I don't seem to have any code that edits a resolver.

This would be easier if you had pasted your code in this thread, but anyway:

You don't need to superload the file to edit these functions.
You should be able to overwrite them at any time in your code. The best time for "any time" is in your addon's "ToME:load" hook.
By the time that hook is called, all resolvers should have been created but not yet resolved, and they are all stored in a table that can be accessed by just typing resolvers.whichever_on_you_want...

So add in something like:
Code:
resolvers.calc.moddable_tile = function(t, e)

whatever you want...

end


Note that this is basically overwriting the function, so if anyone else does the same thing there is no protections for allowing both of you to do that safely.


Top
 Profile  
 
PostPosted: Wed Feb 14, 2018 5:35 am 
Offline
Perspiring Physicist

Joined: Sun Sep 09, 2012 7:43 am
Posts: 5759
I'm not sure on the exact syntax required for this, so if it works I guess its right.

I was thinking about it after I posted last, and I think we can potentially still do the superload wrapper trick by storing the original resolver function and calling it at the start or end of the new function. This will of course depend on how complex your change to the function is, as to whether you want to go to the effort of making sure that any changes to the base code or from other addons are compatible with your addon.

_________________
My feedback meter decays into coding. Give me feedback and I make mods.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group