Closed canadaduane closed 3 years ago
Is there a way to initialize a component's property (NumberType, say) to a random value?
Unless i'm misunderstanding the problem can you not do this in the same place the component is added?
entity.add(MyComponent, { num: getRandomNumber() })
What's your use-case?
Yes, I could certainly do that. I'd just like to not have to remember to initialize it with a random number :)
The random number is more meaningful to the internals of the component than the API of the component.
Use case:
I'm creating a Shake
component that shakes an entity (with Transform
component) around. I'm using a SimplexNoise algorithm for the pseudo-random number generator. When I add Shake
to each thing, I don't want the seed to be the same (i.e. I don't want everything to "shake in sync"--they are each shaking independent of one another).
I just realized that components can have properties that are not listed as a member of props
, can't they? They're just regular instances of an ES class, aka JS object?
I'm creating a Shake component that shakes an entity (with Transform component) around. I'm using a SimplexNoise algorithm for the pseudo-random number generator. When I add Shake to each thing, I don't want the seed to be the same (i.e. I don't want everything to "shake in sync"--they are each shaking independent of one another).
I think components should remain simple descriptions of props here, and any kind of dynamic logic should be handled in prefab functions. It's much cleaner.
I'm doing something similar to your example where players are spawned into a world at a random spawn location. Internally i call "spawnPlayer(spawnPoints)" and that function picks one of the spawn points at random and then creates the player entity and components using that info.
I just realized that components can have properties that are not listed as a member of props, can't they? They're just regular instances of an ES class, aka JS object?
Yeah you can add whatever you want to those components but if you're planning on doing any kind of serialization/deserialisation or networking with a server you'll lose the ability to track those manually added props.
Thanks, that's helpful. I think I'm ok with losing the seed in this case (to the human eye, it's just "shaking around" and won't matter if it can't restore the exact same path).
What's a "prefab function"?
It's really nothing special. Just a plain old javascript function you use to create "things" in your world.
If your world regularly creates an "enemy" entity, instead of writing the same code all over the place to create the entity and attach all the components etc, you just use a function like createEnemy(options)
that does it for you. That way all the code related to creating enemies is in the same place.
Ah, I see. Thanks!
What do you think of making the default
property optionally a function?
export class Shake extends Component {
static props = {
seed: {
type: NumberType,
default: () => Math.random(),
editor: {
label: "Random seed. Default is random."
}
}
//...
}
}
Yeah that might be okay. I don't particularly like the added complexity and think that random values should be part of your game logic not part of the engine.
I'd like to initialize a property whenever a particular component is added to an entity. I see that components have a 'default' value in their props definition; however, it appears this default value is static.
Is there a way to initialize a component's property (NumberType, say) to a random value? Ideally, I'd like to be able to do so whenever the component is added or replaced, but I don't think that's strictly necessary.