Jump to content

Recommended Posts

I am trying to make a prefab do something only when its first crafted, something like `OnNewSpawn` but I can only get that to work for players. Does anyone happen to know a workaround by chance? If not i'll attempt messing with onload and onsave again.

UPDATE: Aquaterion has the answer below for your particular use case (a craftable-only item). I was talking about an item that could exist in the world, as well, without being crafted.

You can't really do that with onsave/onload alone. When an entity is to be loaded, it is first spawned as a fresh instance of that prefab (having its master_postinit called), and then afterwards the saved data is used to put it in the exact state it was in when the game was saved. The problem now lies in the timeline I just described. You cannot know in a prefab's master_postinit whether it is an entity that is being loaded or if it is being spawned for the first time.

Weird knowledge to the rescue! Well, maybe. During world population/loading, this value will be true, and at any other time it will be nil or false.

GLOBAL.POPULATING

You should be able to read this variable in master_postinit (and in onload you already know the prefab is not being spawned for the first time), so if you combine this with a variable on your prefab instance, e.g. inst.hasbeeninitialized, which you save in onsave and can then read (and apply!) in onload, you should be able to get this to work. The complexity of this solution will depend on whether the prefab can be spawned by the world generation (creating a NEW world, not loading a game) or if it can only be created when the world is already running (e.g., by crafting), in which case it you won't have to handle the case where the world spawns the thing.

Edited by Ultroman

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
×
  • Create New...