Hi,
I need some help because i am fighting with an ununderstandable problem that systematically crash solarus.
I was trying to build the elements of the hud (rupee, heart...) to understand how to do it and without any problem so far. But doing my test, i realized that when the game restart because the hero died, the hud didn't come up again.
So i look in the script of solarus DX and understood that i have to put something in a "game:on_started" function that is called each time the game restart after the death of the hero, that will reinitialize the hud.
So i try to do so by copying the way it is done in DX and there the problem begins.
So far to include module files in the main.lua file (for example for the hud.lua) i was just using "require(filename)" that works well. But i have seen that to declare the "game:on_started" function, another way of calling a module is used with "load_file" which is an adapted version of lua "loadfile" command for solarus. So i search a lua help to try to understand this command and find this :
-- just load the module without runing it
sol.main.load_file(filename)
-- load the module and run
sol.main.load_file(filename)(some arguments)
if i have well understood the avantage of "require" is that it loads the modules only once (so preserves memory), but it doesn't seems to be able to send arguments to module's function. To be sure I try the following :
require(filename)(some arguments)
but it doesn't pass the arguments. So I then try to use the "load_file" instead as shown in the DX's script and do exactly as it (at least i think so). And then solarus begins to crash all the time.
As i have put some code inside "game:on_started" i try to undertand what part was causing the crash by removing things little by little. But it was complicated because the crash occured or not depending if i add or remove some instructions somewhere in this module or in another one called by "game:on_started", even sometimes on very simple command like "print". So it wasn't easy to localize which part was the problem because it doesn't crash with any commmand but only with some of them.
For example i have seen that i have timer called in one of my module, and i discover that if i comment it, then there's no crash (but i need this timer).
Finally i make a copy of the project and take everything out (all modules and even clean the main). Now i have a mini project with just 2 files: main.lua and a module called play.lua in the data folder. And it continue to crah my solarus (version 1.2 or 1.21).
To be sure there was not a little part of my code hidden somewhere (or in the map definition for example) that gives instability to the program, i have took back the "simple_quest" example proposed in solarus 1.2 zip file you can download on solarus site and that you try at the begining to understand the basics of solarus.
It is just composed of one map inside a house where you almost can't do anything. It was the first example i begun with, and i knew it works well. So i just took this data folder and just change the "main.lua" by mine and add my problematic module "play.lua" and then... it crash solarus. I really didn't changed anything else than this 2 files. Here they are :
-- main.lua
function sol.main:on_started()
game = sol.game.load("save1.dat")
game:set_max_life(12)
sol.main.load_file("play")(game)
game:start()
end
-- play.lua
local game = ...
function game:on_started()
-- this simple timer that does nothing make solarus crash
sol.timer.start(100, function() end)
end
For me, this very simple code crash solarus with nothing log in the error.txt file, but if i comment the call to "sol.main.load_file" the quest start on without problem.
(In my real code the game create process and game:start() are not in the main.lua but in a game_manager.lua, i just put it there to simplify at the maximum).
So probably i'm not using the command sol.main.load_file("play")(game) like it is neccessary because in DX it works without problem. But i don't see what i do wrong by comparing with the use of it in DX.
I must precise that the timer function i use in method "game:on_started" is just an example. It is one of the function that i have localized to cause the crash in some of my modules called by "game:on_started".
But in my code, even a simple "print" function" make a crash (it depends where it is put). This is not the case in this simple version because i have removed all the other modules that "game:on_started" is supposed to call when the game restart.
So i don't think the problem comes from the timer itself as the problem occurs with other functions. If i put this same timer command in the "main.lua" it runs without problem, it is only inside the "play.lua" file that it makes crash. In my real code, the timer is set in another module called by "game:on_started" so that the crash wasn't localized directly in the play module although the problem seems to come from there.
In "main.lua" i also try another variant used in DX to call load_file, but it crash the same :
-- main.lua
function sol.main:on_started()
game = sol.game.load("save1.dat")
game:set_max_life(12)
local mytest = sol.main.load_file("play")
mytest(game)
game:start()
end
I wish to use "require" instead because it never make crash solarus, but apparently it is not possible because it can't parse the argument "game" to the module "game:on_started", although it really needs it.
So could you please help me as i don't know what to do more.
If you don't see a big big stupid error of beginer from my code that will be evident to your expert eyes, could you then try to do like me by taking your "simple_quest" given with the solarus 1.2 engine and put those two files (see attachments) in the data folder to see what happens and if it's normal.
Thanks a lot