Jump to content

Trigger when Crafted/First Loaded


Recommended Posts

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
Link to comment
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
 Share

×
×
  • Create New...