Somnei Posted September 1, 2022 Share Posted September 1, 2022 (edited) Hi again! I am trying to change the build of a royal guard, but I am unsure how to get my assets to properly load for the prefab. After searching extensively for other threads on this subject, I wrote this code which I felt pretty hopeful about: -- add hurt base to guards AddClassPostConstruct("prefabs/pigman_city", function() table.insert(GLOBAL.Prefabs["pigman_city"].assets, Asset("ANIM", "anim/personal_guard_hurt.zip")) end) AddClassPostConstruct("prefabs/pigman_city", function() table.insert(GLOBAL.Prefabs["pigman_royalguard"].assets, Asset("ANIM", "anim/personal_guard_hurt.zip")) end) and then simply tried to override the build: inst.AnimState:AddOverrideBuild("personal_guard_hurt_build") But that turns them invisible with the following message in the log: [00:03:06]: Could not find anim build personal_guard_hurt_build To verify that my build files are working properly, I simply put my assets into the game's anim folder directly, and added the Asset line manually to the prefab file. When I do this the build is overrided properly in the game. Clearly something about the way I directly input the Asset into the assets table isn't working, but I'm not having any luck finding more information on how to actually add assets to existing prefabs. I would be extremely grateful if anyone here knew how it is done, or even where the assets are stored within the files so I can read up on it myself. I'm hoping it is as easy as just inserting something into a table, but I am afraid that it might not be able to find my assets since they are within my mod's anim folder (though I am hoping that the way the game loads mods might resolve this?). edit: I just checked what happens if I have my zip file in the game's anim folder, but don't include the asset line in the prefab file and confirmed that this doesn't work. So most likely the issue is just how to add the Asset(...) to the prefab's assets, and not something related to it actually finding the file. Regardless, I really need some guidance on this. Edited September 2, 2022 by Somnei Link to comment https://forums.kleientertainment.com/forums/topic/142943-solved-how-do-you-give-a-new-asset-to-an-existing-prefab/ Share on other sites More sharing options...
Somnei Posted September 2, 2022 Author Share Posted September 2, 2022 (edited) edit: None of this is actually necessary, see edit at the bottom of the post - but I'll leave all of this up as a cautionary tale haha Wanted to let anyone interested know that I've finally solved it, what the problem was, and how I figured it out! So this is what I did: 1) Like my previous post's edit says, I verified that my recompiled files properly worked by putting them directly within the game's anim files and adding the Asset directly in the game's pigman_city.lua: Asset("ANIM", "anim/townspig_attacks.zip"), Asset("ANIM", "anim/townspig_actions.zip"), Asset("ANIM", "anim/pig_royalguard_2.zip"), Asset("ANIM", "anim/townspig_shop_wip.zip"), Asset("ANIM", "anim/personal_guard_hurt.zip"), } At this stage I also noticed that you should use AddOverrideBuild instead of SetBuild for overriding a prefab's build temporarily, so this is what I did. This all worked perfectly in the game, and my custom build was displayed properly. 2) Now that I had verified that the files worked, I knew there was something with the way they were inserted into the assets table of the prefab. My code looked like this: AddClassPostConstruct("prefabs/pigman_city", function() table.insert(GLOBAL.Prefabs["pigman_royalguard"].assets, Asset("ANIM", "anim/personal_guard_hurt.zip")) end) But I tried many variations of this with no luck: AddClassPostConstruct("prefabs/pigman_city", function() table.insert(GLOBAL.Prefabs["pigman_city"].assets, Asset("ANIM", "anim/personal_guard_hurt.zip")) end) AddClassPostConstruct("prefabs/pigman_city", function() table.insert(GLOBAL.Prefabs["pigman_royalguard"].assets, Asset("ANIM", "anim/personal_guard_hurt.zip")) end) AddClassPostConstruct("prefabs/pigman_city", function() table.insert(GLOBAL.Prefabs["common/objects/pigman_city"].assets, Asset("ANIM", "anim/personal_guard_hurt.zip")) end) AddClassPostConstruct("prefabs/pigman_city", function() table.insert(GLOBAL.Prefabs["common/objects/pigman_royalguard"].assets, Asset("ANIM", "anim/personal_guard_hurt.zip")) end) At this point I was beginning to doubt that GLOBAL.Prefabs was even called that in Don't Starve, as I think I originally found that code snippet in a Don't Starve Together thread. 3) Luckily, you can print pretty much everything in lua. At this point I simply wanted to verify that GLOBAL.Prefabs existed - and if it did, exactly what it contained. I was pretty sure that the table I was inserting into wasn't correct, as my build wasn't added to the prefab's assets. I wrote this to iterate over the table and print the name of every prefab: for i, v in pairs(GLOBAL.Prefabs) do print(i) print(v) print("---***---") end Turns out it did exist - and the prefab was named "pigman_royalguard" as well, so I checked what it contained (and also what its children contained): for ii, vv in pairs(GLOBAL.Prefabs["pigman_royalguard"]) do print(ii) print(vv) print("---***---") end By checking my log I could now see clearly what it contained - and turns out that its assets table was called assets, so I printed that instead: for ii, vv in pairs(GLOBAL.Prefabs["pigman_royalguard"].assets) do print(ii) print(vv) for iii, vvv in pairs(vv) do print(iii) print(vvv) print("--..--") end print("---***---") end Now I could see that the assets table contained a lot of tables, each containing a "file" (which was the path) and a "type" (which was "ANIM" for most of them). 4) I went back to the way I inserted into the table and tried this instead: AddClassPostConstruct("prefabs/pigman_city", function() table.insert(GLOBAL.Prefabs["pigman_royalguard"].assets, {type="ANIM", file="anim/personal_guard_hurt.zip"}) end) And it still did not work. At this point I was just very confused, but it felt like I could definitely solve it now that I actually knew what the tables looked like. My theory here was that AddClassPostConstruct didn't work, at least not in this situation. 5) I tried inserting into the table before printing it out instead (all of which I did in one of my function hooks which were added through AddPrefabPostInit). This did not work either, but now I actually saw the Asset in the printed table - so I knew it had been added properly. Since I now knew the table was inserted into in the same way as when I added the line directly into the game file (pigman_city.lua), I figured this absolutely must be due to some additional step that happens when the game registers the prefabs. Now I started really diving into the game files, but noticed that many things were unavailable to read since they happened in "TheSim". For a while I thought that maybe hope was lost and that I couldn't find whatever happened to the Asset when the prefab was created, but two wonderful things happened at once: One: I tried printing out what the table looked like when the line was added manually into pigman_city.lua. Lo and behold, the path did not match the one I got when I inserted into the table afterwards - it was instead a relative path from the game files to the asset within my mod folder. Two: With this knowledge I could track down a function that created this relative path from the one given - appropriately named resolvefilepath. 6) I changed the table.insert into the following: table.insert(GLOBAL.Prefabs["pigman_royalguard"].assets, Asset("ANIM", GLOBAL.resolvefilepath("anim/personal_guard_hurt.zip"))) And now it finally worked!! In the end, the reason why my code didn't work was the following: For some reason, AddClassPostConstruct was useless for inserting stuff into the Prefabs table. Perhaps this was because it happens before the table is filled properly, and that it gets overwritten or something. Could be found out by printing the table within an AddClassPostConstruct - but honesly I'm not that interested. If anyone can confirm why, please let me know though. The path to the file needs to be the relative path from the game files to your asset within the mod folder. I would recommend finding it out by using the built-in function resolvefilepath. I wanted to document all of this in case someone else in the future is ripping their hair out in frustration over the same thing, as well as giving an example of how even a problem that might seem impossible to figure out at first can most likely be tackled with a systematic and thorough approach. Thanks for listening to my TED talk. Here's the result of all my hard work: big edit: I figured out why all of this was giving me such big grief, when it felt like it should be fairly straight-forward - and turns out it was all due to a very small detail that I very stupidly forgot about (like it usually is when it comes to these frustrating kind of errors with programming). I had forgot about the fact that you could declare your own assets in the modmain of your mod. After doing this everything worked as it should have from the start! Still have no idea why the AddClassPostConstruct does nothing, but could have something to do with this specific prefab I guess. Edited September 2, 2022 by Somnei Link to comment https://forums.kleientertainment.com/forums/topic/142943-solved-how-do-you-give-a-new-asset-to-an-existing-prefab/#findComment-1596699 Share on other sites More sharing options...
Leonidas IV Posted September 2, 2022 Share Posted September 2, 2022 10 hours ago, Somnei said: Still have no idea why the AddClassPostConstruct does nothing I don't have much property to speak of AddClassPostConstruct, but you were using the function simply to insert a values into a global table (Prefabs). AddClassPostConstruct is to add something after the construction of a class (usually UI classes and other general ones that don't have their own method), but that doesn't actually need to be used and it just brought me problems. You can simply require the file and change the class directly, as it is a table and tables in lua are referential (the object is always the same). For things where you need something to be built and run first (like the constrution of the Prefabs table, which should never be changed directly) the best way is to use AddGamePostInit. Link to comment https://forums.kleientertainment.com/forums/topic/142943-solved-how-do-you-give-a-new-asset-to-an-existing-prefab/#findComment-1596766 Share on other sites More sharing options...
Somnei Posted September 2, 2022 Author Share Posted September 2, 2022 1 hour ago, Leonidas IV said: I don't have much property to speak of AddClassPostConstruct, but you were using the function simply to insert a values into a global table (Prefabs). AddClassPostConstruct is to add something after the construction of a class (usually UI classes and other general ones that don't have their own method), but that doesn't actually need to be used and it just brought me problems. You can simply require the file and change the class directly, as it is a table and tables in lua are referential (the object is always the same). For things where you need something to be built and run first (like the constrution of the Prefabs table, which should never be changed directly) the best way is to use AddGamePostInit. Ah, I see! The only reason I tried using it in the first place is that the only thread I could find on this topic suggested using that function to insert into the assets table. I think my game crashed when I tried using AddGamePostInit to insert into this table, but I could be wrong. For now I'm inserting the assets once on a AddPrefabPostInit instead. Thanks for the information! Link to comment https://forums.kleientertainment.com/forums/topic/142943-solved-how-do-you-give-a-new-asset-to-an-existing-prefab/#findComment-1596775 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now