Jump to content

Recommended Posts

Posted (edited)

I was wondering what the best way to store persistent data in an entity is.

Right now, I'm attaching a custom capability to my entity and attempting to store the capability's data in EntityDataAccessors  every time the Entity is constructed.

This isn't working, and I was wondering if there was a better way of doing this?

Thank you!

 

Edit: By non-volatile I mean saves the data to the specific instance of the entity even when the game is restarted

Edited by squidlex
Clarification
Posted

Thank you for your help! I'm only using the accessors for my own Entity and I'm implementing INBTSerializable in my provider so this shouldn't cause any issues.

I'm trying to access my capability in my Entity's constructor but it isn't working, however it works if I try to access it in another method such as interactAt(). Do you know if there is a way I can access my attached capability when my entity is being constructed? Thanks again.

 

Posted

Thanks for the tip! I've now got:

	@Override
	public void readAdditionalSaveData(CompoundTag pCompound) {
		super.readAdditionalSaveData(pCompound);
		this.nameId = pCompound.getInt("nameId");
	}
	
	@Override
	public void addAdditionalSaveData(CompoundTag pCompound) {
		super.addAdditionalSaveData(pCompound);
		pCompound.putInt("nameId", this.nameId);
	}

but this is not saving correctly. Do I need to call these methods myself?

Posted

Sorry, I should have been more clear.

I am checking my field on the client side in an interaction result, where if they are their default value of -1 I randomly generate a new value:

	@Override
	public InteractionResult interactAt(Player player, Vec3 position, InteractionHand hand) {
		if (level.isClientSide()) {
			if(this.nameId < 0) {
			this.nameId = level.random.nextInt(10);
		}
			GuiWrapper.openGUI(level, player, this);
			System.out.println("Client: " + this.nameId); // I know this is inefficient but just for testing purposes
		} 
		
		return super.interactAt(player, position, hand);
	}

And here is the field:

	private int nameId = -1;

The issue is that every time I close and open the world, they reset to -1 and a new value is generated.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.