Jump to content

[1.18] What is the proper way of defining data values for entities?


Myxtro

Recommended Posts

I have a few questions regarding entities.

Whenever I spawn an entity (with a spawn egg at least) the defineSynchedData() function can't read data values that are being set in the constructor.

So I have two questions:

1) What is the proper way of defining data values which can change for each individual entity (like a hunger counter)

2) What is the proper way of defining data values that won't change, and are the same for each instance of the same entity class (like a max food level)

Edited by Myxtro
typo
Link to comment
Share on other sites

25 minutes ago, Myxtro said:

I have a few questions regarding entities.

Whenever I spawn an entity (with a spawn egg at least) the defineSynchedData() function can't read data values that are being set in the constructor.

Since you don't show the code you tried and/or post the error, your question is unanswerable. We have no psychic powers.

SynchedEntityData is used to send your entity instance data to the client. I am not sure of relevance for spawning entities, which should happen on the server?

https://forge.gemwire.uk/wiki/Networking_with_Entities

 

Quote

1) What is the proper way of defining data values which can change for each individual entity (like a hunger counter)

2) What is the proper way of defining data values that won't change, and are the same for each instance of the same entity class (like a max food level)

There are many different ways, which you use depends upon the use case, e.g.

* public static final fields to hold values that never change

* instance fields which may or may not be synched to the client and usually persisted to disk in add/readAdditionalSaveData()

* entity attributes that store values that can be modified by things like enchants or mob effects (e.g. max health, armor rating, etc.)

* writing your own entity capability for storing data on other people's entities

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

5 hours ago, warjort said:

* public static final fields to hold values that never change

This is probably the way I want to go with. But how would I do this in the scenario where I have an abstract class where the field must be known (so methods can use it) but not yet have a value. Where in the implementing class should I assign a value to the field? I have tried this setup but it doesn't seem to work:

 

//in the abstract class
public static int maxFood;

//In the constructor of the implementing class
maxFood = 100000;

 

Link to comment
Share on other sites

This is basic java, your question has nothing to do with forge or minecraft

 

You can't use a static field if it varies by subclass. Static fields are not polymorphic..

public abstract class AbstractClass {
    public int getMaxFood () {
         return 10000; // default value
    }
}

public class ConcreteClass extends AbstractClass {
    @Override
    public int getMaxFood () {
         return 20000; // different value for this class
    }
}

AbstractClass x = ...;
x.getMaxFood(); // will vary by the class of x

I would suggest you find something to help you learn java.

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

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.