There seems to be something I am just

There seems to be something I am just not understanding about saving tables and loading them. When I log in or first fire up a profile, I'm loading my tables from their save state, and defining the tables ahead of time. The variable data all loads just fine, but none of my functions do. I have to manually go through and click every script file to get them to load. I've been banging my head against this for a couple days, and I know there has to be something simple I'm missing, I just don't know what that is.
13 Replies
NatalieZorah
NatalieZorah5mo ago
It's happening with both function fields and methods, which ruled out what I thought might be happening where it's just arguing with me for using methods. I thought functions were all called on profile load, and from what I've read, that's true, but something I'm doing seems to wipe that away for some reason.
atari2600tim
atari2600tim5mo ago
Whenever it loads a profile, it takes all the scripts and puts them together and runs the whole set. When you edit a script in middle of a session, it runs that piece. So a common thing that people run into is they have everything ran, they edit a new thing, it works, then next session it does not work. Their new thing gets ran before something it relies on, but when they initially wrote it, it was already prepared. So like I might have a trigger that saves my hit points and a different piece that draws a bar showing hit points compared to maximum. Next session it might try to draw before it knows what my hit points are.
NatalieZorah
NatalieZorah5mo ago
I have gating if statements in the functions that rely on instance data like that, but the issue I'm running into is that things like my whitelist is saved, but the function getVitals() returns as nil.
NatalieZorah
NatalieZorah5mo ago
No description
NatalieZorah
NatalieZorah5mo ago
It's only with the Methods and function fields though.
demonnic
demonnic5mo ago
You cannot save functions from tables to disk using table.save, those you should define in the script itself or a separate .lua file great for saving configuration, but can't put your functions in there. they won't load back up, as you noted.
atari2600tim
atari2600tim5mo ago
Oh, they are creating the functions, but then since they are in a table, they get lost upon loading the table from disk. Loading table from disk makes new table and doesn't append onto existing one.
NatalieZorah
NatalieZorah5mo ago
So even though they're defined in script files, because they exist on the tables, when I load, it wipes them? Is there a way around that, or am I going to need to rewrite a lot of this? It's weird, because my save functions mirror other's I've seen, that don't have that issue, which is why I'm confused.
demonnic
demonnic5mo ago
when you load the table, it overwrites it I believe. So if you load it before defining the functions it's fine. But really, I would recommend keeping state in a subtable and loading that if you want to use table.save. Or using a function and table.update IE load it into a temp local table and then use table.update to replace values in your main table with the saved ones
NatalieZorah
NatalieZorah5mo ago
Given how many subtables I have to parse, the update thing may be the best option. I didn't even know that was a thing I could do.
demonnic
demonnic5mo ago
https://wiki.mudlet.org/w/Manual:Lua_Functions#table.update it's not a base Lua thing, but something we added in Mudlet
NatalieZorah
NatalieZorah5mo ago
Oh... Yeah I'm definitely gonna try that first. You are a legend and have saved me so many hours. Much appreciated!
demonnic
demonnic5mo ago
I'm always happy to help =)
Want results from more Discord servers?
Add your server