-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a ESM Script with attributes #6785
Comments
How about options 4:
|
With scripts 2.0, I think that attributes are separate from properties and should only be set if a schema exists scripts.create('scriptName', { attributes: { myNum: 1 }}) // Warns if no schema exists scripts.create('scriptName', { properties: { myNum: 1 }}) // Always assigns/copies regardless of attributes and/or schema |
Hey that makes sense. So how about the engine users have access to and the Editor uses internal API when the engine users pass attributes to .create function, it should error out. This would help them port code from 2.0 to ESM. Regarding deep copy. That would still be great for Vec3 and similar types, but not for Entities and similar .. not sure what would be the best here. |
This makes sense, but is't it more straightforward to have a single Regarding deep copy, I feel we should provide options here as I can see many different valid use cases. Maybe you can optionally provide a function at the end, which defaults to function create(scriptName, { attributes, properties }, operation = Object.assign){} And if you wanted to deep copy you just override create("scriptName", { properties }, deepCopy) At least then it's clear what's happening |
Single function is fine too I guess .. as long as when we don't have a schema, we Debug.warn or similar when the attributes are used, instead of just quietly ignoring them. I don't like the extra parameter that handles cloning. That's equivalent of not cloning internally and just asking user to clone. They can do it on their side completely in that case. We either clone by default, or let them clone. Helpers are not needed? |
Yep agreed.
Yep, but this would happen before So create(name, { properties }, operation = Object.assign) {
script = new Script(name)
operation(script, properties)
script.initialize()
} |
The user can clone properties any way it wants to before passing it to
|
Yep, that's fair, if we don't clone internally and just make it clear that the properties are assigned |
The important aspect of this is how engine-only users can easily save/load attribute data from the entity's script component. {
"attrs": [
{
"name": "vertex_position",
"semantic": "POSITION"
},
{
"name": "vertex_normal",
"semantic": "NORMAL"
},
{
"name": "vertex_uv0",
"semantic": "TEXCOORD0"
},
{
"name": "vertex_bone_index",
"semantic": "BLENDINDICES"
}
],
"vertex": 177287037,
"fragment": 177287044,
"textures": [
{
"name": "texture_albedo",
"asset": 182217219
}
],
"depthTest": true,
"depthWrite": true
} And script 2.0 has this attribute definition: CustomMaterial.attributes.add('vertex', { type: 'asset', assetType: 'shader' });
CustomMaterial.attributes.add('fragment', { type: 'asset', assetType: 'shader' });
CustomMaterial.attributes.add('depthTest', { type: 'boolean', default: true });
CustomMaterial.attributes.add('depthWrite', { type: 'boolean', default: true });
CustomMaterial.attributes.add('attrs', {
type: 'json',
array: true,
schema: [{
name: 'name',
type: 'string'
}, {
name: 'semantic',
type: 'string',
enum: [
{ 'position': pc.SEMANTIC_POSITION },
{ 'normal': pc.SEMANTIC_NORMAL },
{ 'color': pc.SEMANTIC_COLOR },
{ 'uv0': pc.SEMANTIC_TEXCOORD0 },
{ 'blend-indices': pc.SEMANTIC_BLENDINDICES },
{ 'blend-weight': pc.SEMANTIC_BLENDWEIGHT },
{ '12': pc.SEMANTIC_ATTR12 },
{ '13': pc.SEMANTIC_ATTR13 },
{ '14': pc.SEMANTIC_ATTR14 },
{ '15': pc.SEMANTIC_ATTR15 }
]
}]
});
CustomMaterial.attributes.add('textures', {
type: 'json',
array: true,
schema: [{
name: 'name',
type: 'string'
}, {
name: 'asset',
type: 'asset',
assetType: 'texture'
}]
}); Will the user be able to simply add script to entity and provide JSON as an attribute argument from above with a new system, and expect attributes to be in correct (by the schema) types? |
I'm oversimplifying here, but ignoring events + asset/entity resolution etc, the attribute system...
In an Engine only scenario, 1 is unnecessary. For 2, we should really lean on early in-editor type validation, so that type errors can be flagged before running. One issue with I'd need to check what the implications of this would be, but it feels like a valid option |
Scripts components have a
create()
method for instantiating scripts with attributes.This depends on an attribute schema existing. ESM Scripts use JSDoc tags for their schema which is unavailable in engine only environments, meaning attributes cannot be set on a script
For ESM scripts in engine only, properties must be set after a script is instantiated:
Ideally properties can also be set in-line on a script instance, at the same time as it's created.
Possible solutions
1. Re-use
attribute
propertyIn the editor, a schema will exist, so it will be used to create the attributes, when it doesn't, the values are simply assigned as properties onto the script instance
Pros
Simple. Re-uses existing api.
Cons
Inconsistent behaviour between editor/engine. - In editor, this will validate and deep copy values. In engine, it will not.
semantics Attributes don't inherently exist in engine only, so this is overloading the term.
2. Separate method for engine only
Create a new method eg
scripts.createWithProperties('scriptName', { myNum: 1 })
(TBD) that assigns values when instantiatedPros
Clear and separate behaviour. Does not overload the
attributes
propertyCons
Creates an additional method which could lead to confusion.
create()
still exists and would work for instantiating a script with no props/attributes3. Create an additional property
Attributes are applied first if they and schema exist, and
properties
are then assigned after.Pros
No additional method. Clear distinction between behaviour of
attributes
andproperties
(attributes require schema, props do not)Cons
Adds additional property. ??
Any thoughts @mvaligursky @willeastcott @Maksims @slimbuck
The text was updated successfully, but these errors were encountered: