Sign in to follow this  
Serpens

Possible to add a new prefab based upon an existing, without copy/paste the script?

Recommended Posts

Serpens    565

Is it possible to eg. add a new pig with new prefab based on the prefab file pigman, without copy pasting and renaming that prefab file?
And then make your changes via modmain, so the new prefab acts different from the original.

The goal here is, to also get updates from the game when the devs do changes to the pigman. If I would copy/paste the fie, I would need to update the mod everytime the game updates the files.

Share this post


Link to post
Share on other sites
YakumoYukari    150

I'll say there are three popular ways to override prefabs(or components if you want). I've been seen some mod files that modify existing files, and all these ways have their properties. If you enough understand with these options, all three methods are legit.

The first one is to use post init functions in the mod environment. It is written in modutil.lua, InsertPostInitFunction

Quote

Every PostInit functions are inserted by table.insert() into an environment table, named postinitfns. 
It is executed in runtime, including the main loads(for the client mods mostly), worldgen and the world loading. 
Since every element in the game environment(prefabs, stategraphs, actions...) is being variable and separated, it has its own time to appear. And if any post functions exist on that element, the function runs, in the load order.

I have explained this in the last post(Mod Load Order Priority).

Because of the post init functions run in the order listed in postinit table, It is the best-easy, and I think you will probably use this one.
For prefabs, there are AddPrefabPostInitAny, AddPlayerPostInit, AddPrefabPostInit.

AddPrefabPostInitAny and AddPlayerPostInit are used when you need to override multiple prefabs at once. But you need to think about some performance issues as its comment.

And if you want pigmans overridden, do something like this.

AddPrefabPostInit("pigman", function(inst)
	-- do something with inst
end)

And for the pigman prefab specifically, your PIF will run after the local function, normal.
However, you can't override local function in there, like if you're trying to tweak some lines in SetNormalPig which you can't access in PIF scope, you need to re-define the whole local function that works correctly(or copy-paste it then tweaks) or use another method.
 

The second one is to override the existing file, that the way you don't want to do though. 
If a mod file is at the directory where the original file and the name are the exact same, you can override the whole file.
It removes the information of the original file which means the game update will no longer affect that file.
So if you don't update your mod frequently, it might cause the crash more easily and soon your mod will be deprecated.
But this can be useful when you do a huge scale mod project(that may have unique dependency) or when you need to patch a lot of things.
Since prefab files are easily bound to local functions, this can be better than you may concern.
But not for components, as it avoids using local functions.
 

Lastly, you can use UpvalueHacker made by Rezecib.
This is a tool to access the local variable using Lua's debug functions.
It is very powerful but also the toughest to use. Because you need to get a reference by going through the function

Edited by YakumoYukari

Share this post


Link to post
Share on other sites
Ultroman    727
1 hour ago, YakumoYukari said:

stuff

A lot of great information there, but I think you misunderstood his question.

He wants to make his own new prefab based on the original pigman code, and is asking whether he can do that by loading in the original pigman.lua from the game into a new prefab, instead of copying the pigman.lua and renaming the prefab, so when Klei updates pigman.lua his mod will automatically be up-to-date, because it loads the original pigman.lua code into his custom prefab and then alters the new renamed prefab using postinits.

Share this post


Link to post
Share on other sites
SpoonyBardIV    857
3 hours ago, Ultroman said:

A lot of great information there, but I think you misunderstood his question.

He wants to make his own new prefab based on the original pigman code, and is asking whether he can do that by loading in the original pigman.lua from the game into a new prefab, instead of copying the pigman.lua and renaming the prefab, so when Klei updates pigman.lua his mod will automatically be up-to-date, because it loads the original pigman.lua code into his custom prefab and then alters the new renamed prefab using postinits.

Speaking of custom prefabs, how do I actually create one? I want to make a wheat plant based on the gorge assets that spawns in the savannah.

Share this post


Link to post
Share on other sites
Serpens    565

thank you, Ultroman is right, that is what I meant.

But still thank you YakumoYukari for all this information. But I fear, if I would be a newbie, I would not understand much :D UpValueHacker is more for experts, although I used it some times already some years ago.

If no one knows a solution, I will do a workaround. In fact I'm not working on pigman, but on the shadow minions from maxwell, and I "deleted" the lumber/digger/chopper anyway and am creating a new one "shadowworker" which can do all of the stuff. So instead of making a new prefab shadowworker, I will then instead use the shadowlumber and change it within modmain with prefabpostinit.

SpoonBardIV:
please make a new thread with your question :)

My shadowmaxwell mod beta here:
https://forums.kleientertainment.com/files/file/1968-beta-ultimate-maxwell-shadows-better-maxwell-shadows/

 

Edited by Serpens

Share this post


Link to post
Share on other sites
Ultroman    727
18 minutes ago, SpoonyBardIV said:

Speaking of custom prefabs, how do I actually create one? I want to make a wheat plant based on the gorge assets that spawns in the savannah.

If you're talking about taking an existing asset and copying it to, e.g., make it available outside gorge or make a new version of an existing asset, then that's what this topic is about. Usually, you'd copy the prefab lua, animations, etc., rename the files and the prefab to avoid clashing with the original prefab, do whatever changes you want, and create a modinfo.lua and modmain.lua with the needed code. There are plenty of mods you can investigate to see how this can be done. There are some examples in the newcomer post and in this thread.

Share this post


Link to post
Share on other sites
SpoonyBardIV    857
22 hours ago, Ultroman said:

create a modinfo.lua and modmain.lua with the needed code.

I looked through all the mods in both your links, and I still don't have the foggiest idea of what to do. Do you know of any good guides on creating prefabs using existing assets?

Share this post


Link to post
Share on other sites
Ultroman    727
11 minutes ago, SpoonyBardIV said:

I looked through all the mods in both your links, and I still don't have the foggiest idea of what to do. Do you know of any good guides on creating prefabs using existing assets?

Not really. Try making a new prefab without using existing assets first, just to get the hang of everything. Then you can look into extracting and repackaging existing animation assets, and replace the ones you used for your new prefab. There aren't many tutorials to find, but there are quite a lot of very specific tutorials, e.g., Klei's tool guideKlei's old sample mods, my modinfo walkthrough, and the example mods I posted earlier. Klei's stuff is 6 years old, but almost holds up. You can ask when you've followed those and your thing still doesn't work. There's no easy way here.You gotta study existing items and mods to understand how to do these things, and ask when you're out of hair to pull out :)

We're working on getting some updated tutorials done.

Edited by Ultroman

Share this post


Link to post
Share on other sites
SpoonyBardIV    857
14 minutes ago, Ultroman said:

Try making a new prefab without using existing assets first, just to get the hang of everything.

You know of any tutorials on that? Sorry if I seem a bit pushy, I just don't really know a lot about this kind of stuff...

Share this post


Link to post
Share on other sites
Ultroman    727
1 hour ago, SpoonyBardIV said:

You know of any tutorials on that? Sorry if I seem a bit pushy, I just don't really know a lot about this kind of stuff...

Nope. There aren't any. There's this video giving an intro to scripting and making other mods, but not one for items. Look at the Cactus Armor mod in the newcomer post. There are a lot of comments in that, and it should give you a good idea of how things are connected. For prefab code, you can go look at the simplest item you can think of, like twigs or something.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this