#Beginner needs help with understanding AddComponent

1 messages · Page 1 of 1 (latest)

chilly mortar
#

So im learning a bit of DOTS at the moment and came across this line of code, i dont rly understand it, so i know entitys are the things in your world and i know what TranformUsageFlags do but i dont understand the AddComponent method? he is doing something there that i dont understand. I know a few basic stuff about the addcomponent like u need to tell what entity u want to add the component too and u need to tell the component wich position the entity must have but i dont understand what the prefab is? is it to tell it what sort of object it is? a cube or a circle etc..?

rapid valve
#

A component can be almost anything. This is a custom component they've made called CubeSpawnerComponent that has those fields

drowsy grove
#

A prefab in this case is just a gameobject (not part of DOTS or ECS). Since the Component is called CubeSpawner, I assume the prefab is the Cube to be Spawned 🤷‍♂️

The hybrid approach to ECS can use gameobjects as templates for creating entities. That way you can populate data via the inspector for entities just like the normal Unity workflow.

#

He might even just be spawning GameObjects, because that part is not shown in your screenshot, but I assume not

chilly mortar
#

i thought components were the things u add to a gameobject in the inspector, i just cant grasp the thought that the component can be the cube?

drowsy grove
#

Components are just data containers in ECS, and Systems are what act on the components. Entities are just a memory location id basically

daring bramble
#

I advise you to read this first before diving into other ECS tutorials

chilly mortar
#

Oohh... ok ok damn, i'll need to learn the ECS world first ig

chilly mortar
#

so if i understand correctly, entitys are the identifiers to the boxes and the boxes are named components and in those boxes (components) u can have different functions

#

if u put it at a very simple explanation?

ruby ruin
#

No

#

Entity is a container for components and can have any combination of those

#

ECS specifically splits logic from data (components)

#

So components are not meant to have it

#

Instead logic is declared in systems that have an ability to iterate any Combination of components

#

Try to reaf some articles on ECS that explain this pattern

#

Understanding thr basic goal of pattern will probably save you some trouble understanding different decisions from unity for dots

chilly mortar
#

so an entity can have any combination of components in it?

ruby ruin
#

yes

chilly mortar
#

so in this case, the code that i sent u, u have a prefab component and then a transform? because he gives a position to it?

ruby ruin
#

in dots, baking handles transform baking for you

#

you just need to specify correct flags

#

or if you want to fully manually bake all transform components - you use ManualOverride flag

chilly mortar
#

well, he sets the position of the spawning prefab in the code, is that not a transform?

ruby ruin
#

no, he simply stored float3 field for later use in runtime

chilly mortar
#

im not used to prefab being a component lol

ruby ruin
#

that's not an equivalent of baked entity transform

#

prefab is not component

#

prefab is Entity with Prefab component

#

referenced as Entity field

chilly mortar
#

so there is a component Prefab?

ruby ruin
#

you can use components to store prefabs

#

or any other data that you want

#

so I assume that's what he does in example above

chilly mortar
#

Aaah lol okay now i get it!

#

so he stores prefab in a component and the spawnposution too and the spawnrate etc...

#

so its not like im used to it, because in unity u can put components on gameobjects like a rigidbody2d or a transform component, in this case components are a sort of variables?

ruby ruin
#

no, it's almost same thing as game objects regarding that. It's just that game object component are meant to have logic, while ECS ones don't

#

in ECS, you fully separate logic from data

#

this allows you scale horizontally

#

as in: any feature/assembly (even 3rd party one) can add systems that iterates existing components

chilly mortar
#

i just dont understand what the prefab is in the component?

azure gate
#

It's like saying public GameObject prefab; in a MonoBehaviour, you're referencing something that you're going to instantiate later

#

just that entities requires things to be referenced with Entity, so your field would be public Entity Prefab; spawned with like Entity spawned = EntityManager.Instantiate(Prefab);

chilly mortar
#

But why is it in a component? a prefab isnt a component right?

ruby ruin
#

the only way to have data in ECS - components

chilly mortar
#

So u cant compare components from ecs to components like i normally saw in unity inspector?

#

bcs in components im used to u cant store prefabs...

#

but that isnt a component is it?

#

is a gameobject like a cube or a circle a component?

ruby ruin
#

public class MonoBehaviour : Component

azure gate
chilly mortar
#

but i dont understand what the prefab is in the component? is it just data u put into the component?

ruby ruin
#

yes

chilly mortar
#

aah so in this guy his code he is actually programming a new component

#

and he gives a few datas with it, a prefab a spawnposition

#

etc..

chilly mortar
#

Okay, thank u guys, i appreciate the help alot! ur right about learning the fundamentals of dots first because i lack them, but still a big thank you (: