ldtk icon indicating copy to clipboard operation
ldtk copied to clipboard

Default Value For Arrays

Open nyalloc opened this issue 3 years ago • 2 comments

Summary

LDtk's support for default values for arrays is very limited. There is currently no way of setting a default array for an array field.

Proposal

Add a way to provide a default array:

image

Background

Default Values

LDtk allows you to set default values for fields, which is useful for the user as it means they don't have to update the fields of every entity instance if there's a sensible default.

For example, if you have an Entity called "NPC", you might want to give them a field called "Health":

image

Having to set this value for every NPC on the map would be a pain, so instead we can set the default value to "100".

image

Now if we interact with an NPC, we see that the Health field is 100:

image

Every NPC now has a health of 100 by default. If one of the NPCs needs to be stronger or weaker than the average NPC, we can override the Health field's value:

image

Array Fields

LDtk also allows you to create array fields, which are simply an array of values all of the same type. You can set a default value for an array, but the behavior is not consistent with single values.

For example, you may want to add a field to the NPC entity that lets the designer add simple dialogue. So you could add an array of strings called "Dialogue", with each string representing a different line of speech when the NPC is interacted with in game. In 1.1.3, you can set the default value for thie field, but you will find that you can only set a single value.

image

But when you click on the NPC field, you will see that the field is actually empty, and not initialised.

image

This is because the default value for arrays does not behave the same way as the default value for single values. Instead of representing a default value for the array field, it represents the default value when you add a new element to the array. So if you add 5 new elements to the array, you will see this:

image

This means that if you have an array field, you cannot provide a default array, and you have to manually initialise the array for every NPC entity, even if most of them will have the same value.

Solution

The best solution here would be to change the default value behavior for arrays to be consistent with that of default values - let the user provide an array of values that will be taken as the default and allow this array to be overriden by individual NPC instances.

With a new UI, you could create a default set of lines for NPCs:

image

Later, you could override those lines for specific NPCs:

image

The behavior for setting a default value for new array elements could also be retained, though this is likely less useful as the majority of users are probably not going to create array fields filled with the same value.

nyalloc avatar Dec 01 '22 12:12 nyalloc

👍 This would be a useful feature.

I was planning declare enemy entity "collision masks" (e.g., what category, what it can collide with) using an array of enums. But because of how limited the default value is this didn't work out well. I'll probably just encode these types of bitmasks as a string instead. The UI for adding array values is great for entity instances, it's just that entity declaration is missing the same functionality.

nurpax avatar Apr 02 '23 07:04 nurpax

Related #972

deepnight avatar Oct 11 '23 10:10 deepnight