How to think the ECS way?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
togFox
Party member
Posts: 608
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

How to think the ECS way?

Post by togFox »

Trying to understand how to use ECS correctly, as in, the "data model" for want of a better term.

There is, of course, entities, components and systems and I'm hoping an example will make this easy to understand. I'm a bit of a drag car fan so want to make a drag race game. Will involve reaction times but also starting with a basic drag car and building up modules so that the car gets better. Bit of a meta-game to it I guess.

I think my 'data model' will be this:

- two cars racing down a straight track simultaneously.
- the cars will have bits that make it perform better
- those bits will be "common" in the sense that a super-charger on one car will perform and behave the same on any other car.

If I have an entity called "car" and an entity called "super-charger" then I can link the two together in a table that belongs to 'car'. Yes?

The super-charger will have a neat ability to multiple acceleration by 1.5. Is that a component?

And if "motion" is something that happens to the car, but is influenced by the super-charger then ... idk. How to unpack all that? (and haven't even mentioned 'system' yet).
Current project:
https://togfox.itch.io/asteroid-hunter
Slow burn 2d zero-g space game with meta
User avatar
darkfrei
Party member
Posts: 736
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to think the ECS way?

Post by darkfrei »

For example
the entity car:

Code: Select all

car = {
	x=x, y=y, vx=vx, vy=vy, -- just position and velocity
	baseMass = 100,
	baseForce = 100,
	modules = {superChargerModel3, lightAlloyWheelsMk3},
}
car.mass = car.baseMass
car.force = car.baseForce 
car.acceleration = car.force/car.mass
where

Code: Select all

superChargerModel3 = {
	forceFactor = 1.2,
	mass = 3,
	type = "supercharger", -- check that you cannot have two same type modules 
}
lightAlloyWheelsMk3 = {
	mass = -8,
	type = "wheels", -- check that you cannot have two same type modules 
}
So on the adding/removing of modules recalculate parameters as

Code: Select all

car.mass = car.baseMass
car.force = car.baseForce
for i, module in ipairs (car.modules) do
	if module.mass then
		car.mass = car.mass + module.mass
	end
	if module.forceFactor then
		car.force = car.force * module.forceFactor
	end
end
car.acceleration = car.force/car.mass
What is ECS?
User avatar
ReFreezed
Party member
Posts: 580
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: How to think the ECS way?

Post by ReFreezed »

What you're experiencing is the completely unnecessary complexity of components (i.e. what value/functionality belongs where). darkfrei's example is pretty simple because it doesn't use components and just puts everything on the entity. In an ECS the "car" should likely be a component. You can think of entities as just being containers for components. A car would not an entity, but rather, and entity could be a car (among other things).

Do you have a good reason to use an ECS?
darkfrei wrote: Thu Aug 11, 2022 12:35 pm What is ECS?
Entity Component System.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests