Jump to content

Recommended Posts

I'd like to be able to refer to another entity/prefab from my another (my own) entity/prefab and keep this reference intact when saving/loading the game.

 

The first thing that came to mind would be using the ID of the entity - however, I can't find such a thing. The closest I've found is entity.GUID - but that does not persist, so I can't use that.

 

Is there a proper way to do this? If there isn't, I think I can do some magic to assign my own unique ID numbers to the entities/prefabs I'm interested in, but I wanted to see how the proper way is to do it, if there is one.

 

 

 

Also, is there some documentation to the fields and functions that the userdata types have? e.g. entity.AnimState and so on?

 

 

 

Cheers in advance.

I'd like to be able to refer to another entity/prefab from my another (my own) entity/prefab and keep this reference intact when saving/loading the game.

 

The first thing that came to mind would be using the ID of the entity - however, I can't find such a thing. The closest I've found is entity.GUID - but that does not persist, so I can't use that.

 

Is there a proper way to do this? If there isn't, I think I can do some magic to assign my own unique ID numbers to the entities/prefabs I'm interested in, but I wanted to see how the proper way is to do it, if there is one.

 

 

 

Also, is there some documentation to the fields and functions that the userdata types have? e.g. entity.AnimState and so on?

 

 

 

Cheers in advance.

 

You could use onsave/onload and assign an inst.tie (or inst.whatever) value using math.random to generate it and work from there.

The GUID persists if another saved entity keeps a reference to it. Check the OnSave method of the leader component (doing it from the prefab's OnSave is also possible, but a bit different).

 

Thanks a lot! This does indeed work!

 

For anyone else stumbling upon this, I'll make a quick note:

In a component :OnSave, the second return value that you should provide is a table of the GUIDs of the entities you are interested in.

In the component :LoadPostPass(newents, savedata), the first parameter is a table which has a mapping of all the entities (I think - but this isn't relevant), but more importantly, using the old entity's GUID as a key (e.g. newents[oldGUID]) gives you another table. In this other table, the entity field gives you the new instance of the entity. (e.g. newents[oldGUID].entity)

 

The same can be done with a prefab/entity's .OnSave, except that the table is the first (and only) return value.

Then you can define .OnLoadPostPass which has the same parameters as the component version.

 

If that doesn't make sense to you, go and take a look at the leader component (as simplex mentioned), the Leader:OnSave and Leader:LoadPostPass methods.

Edited by TehSomeLuigi

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...