Zamiel Posted March 16, 2014 Share Posted March 16, 2014 Hello, I am new to coding in lua, and to modding Dont Starve. I have worked for several years with object oriented java as a student, so I grasp a lot of things thrown at me as I learn. Currently, after poking my nose around and test-tweaking a few things in several existing mods and the game code, I decided to embark on several tutorial I found in sticky posts. This is not a problem, just a need for clarification of a certain point. This is code from the custom mob creation tutorial (lesson 4) at the end of the /scripts/prefabs file for the creature. --Here we register our new prefab so that it can be used in game.return Prefab( "monsters/myPrefab", init_prefab, assets, nil) What is ... "monsters/myPrefab"? There is no such "monsters" folder. Can someone explain to me why this is here, why it is so, and how it manages to work with no such folder existing. ps. Another thing I want to ask if you dont mind: does Prefab() essentially act as a big constructor for all various different prefabs out there that creates the correct instance of the prefab which called it and passed in its data? If "technically" is not a constructor let me know, but also say whether it does the exact same thing as a constructor would - even if it really isn't one. pps. Where is this 'Prefab()' function located so I can take a look at it. Link to comment https://forums.kleientertainment.com/forums/topic/33000-newbie-questions-about-prefab-params/ Share on other sites More sharing options...
squeek Posted March 16, 2014 Share Posted March 16, 2014 (edited) The Prefab class definition can be found in scripts/prefabs.lua. It's a very simple Class, and the first parameter gets stored as self.path, and then it takes the part after all the slashes and uses that as self.name (what you'd actually use to spawn it). self.path is not used for anything at all as far as I can tell, so it's either a leftover from some other system or it's just used to classify things into categories (but, again, not sure where those categories are used; as far as I can tell, they aren't). So, yeah, only the final piece of the string after the slashes is actually used, and that's what the prefab will actually be named. It doesn't have anything to do with file paths. Prefab is not really a constructor for the actual prefab, it's more of a constructor for the 'settings' of a prefab. TheSim:SpawnPrefab(name) actually creates the prefab object (see the SpawnPrefab and SpawnPrefabFromSim functions in mainfunctions.lua), and then calls the constructor function (the second parameter of the Prefab class constructor; usually named 'fn' in prefab files) of the associated Prefab object to set it all up. Also, a point of clarification: that return statement at the end of the prefab file doesn't actually register the prefab. The actual registering is done automatically after modmain.lua is run by looking at the PrefabFiles table of the mod environment and registering each prefab specified. If you just add a prefab script without adding it to the PrefabFiles table in modmain.lua, it will never be registered. Edited March 16, 2014 by squeek Link to comment https://forums.kleientertainment.com/forums/topic/33000-newbie-questions-about-prefab-params/#findComment-432549 Share on other sites More sharing options...
Zamiel Posted March 16, 2014 Author Share Posted March 16, 2014 Thank you, you made it very clear. Link to comment https://forums.kleientertainment.com/forums/topic/33000-newbie-questions-about-prefab-params/#findComment-432558 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