Page 1 of 1
player.vehicle.rotation_speed not working.
Posted: Fri Dec 23, 2022 11:08 am
by MrDayne
I can't figure out how to change that in a function inside control.lua. The player.vehicle reference doesn't have the car's property (rotation speed) and I don't know how to get a reference to the car, and not it's parent vehicle.
Any help would be appreciated.
Re: player.vehicle.rotation_speed not working.
Posted: Fri Dec 23, 2022 11:19 am
by Deadlock989
LuaEntity.rotation_speed doesn't exist. LuaEntityPrototype.rotation_speed is
read only and can only be defined in the data stage. You can't change vehicle rotation speeds at runtime.
I don't know what you mean by a "the car and not the car's parent vehicle". Cars are vehicles.
Re: player.vehicle.rotation_speed not working.
Posted: Fri Dec 23, 2022 11:27 am
by MrDayne
Cars have all the variables from vehicle, since they inherit from it, but cars have some variables of their own that vehicles don't, like rotation speed. When I get player.vehicle I can modify all vehicle variables, but not car variables.
Re: player.vehicle.rotation_speed not working.
Posted: Fri Dec 23, 2022 11:51 am
by Deadlock989
Oh, I see. Some things are read-only and fixed, defined in the data stage as a prototype property (which is why rotation_speed is a property of LuaEntityPrototype and not LuaEntity). You can't change car rotation speed at runtime.
Re: player.vehicle.rotation_speed not working.
Posted: Fri Dec 23, 2022 11:53 am
by Xorimuth
You’re mixing up data stage and control stage. In data stage, you create prototypes using the wiki docs. In control stage you do runtime scripting using the api docs at
https://lua-api.factorio.com/latest.
Maybe
https://lua-api.factorio.com/latest/Data-Lifecycle.html will help clear up your confusion.
Re: player.vehicle.rotation_speed not working.
Posted: Fri Dec 23, 2022 12:12 pm
by Pi-C
xan186 wrote: Fri Dec 23, 2022 11:27 am
Cars have all the variables from vehicle, since they inherit from it, but cars have some variables of their own that vehicles don't, like rotation speed. When I get player.vehicle I can modify all vehicle variables, but not car variables.
You must distinguish between the car entity and the car prototype. The entity is what you place on the ground, enter, and drive around with. The prototype defines the default values which all entities of the same type and name will share.
As Deadlock989 already explained, prototypes are defined during the data stage. You can change the prototypes in whatever way you like from data.lua, data-updates.lua, and data-final-fixes.lua. Once the data stage has finished, the prototypes can't be changed anymore.
The control stage comes after the data stage. Here, you only deal with entities, which are real-game manifestations of a prototype. While you can't
change the prototype at this point, you can still
read some of its properties:
Code: Select all
local player = game.player
local my_car = player.surface.create_entity{name = "car", position = player.position}
local prototype = my_car.prototype
The variable prototype will then contain a
LuaEntityPrototype from which you can read the default values. For example, if you'd want to find out whether your car is damaged, you could run this on every tick:
Code: Select all
if my_car.health < my_car.prototype.max_health then
game.print("Damaged!")
end
(This is just a stupid example! In a real mod, you'd listen to the on_entity_damaged event.)
Re: player.vehicle.rotation_speed not working.
Posted: Fri Dec 23, 2022 2:51 pm
by MrDayne
Ok, so I guess the only way is to manually correct rotation on every tick, and limit it more and more the faster the car is going. Do you think this could be doable??
Re: player.vehicle.rotation_speed not working.
Posted: Fri Dec 23, 2022 3:33 pm
by Pi-C
xan186 wrote: Fri Dec 23, 2022 2:51 pm
Ok, so I guess the only way is to manually correct rotation on every tick, and limit it more and more the faster the car is going. Do you think this could be doable??
In
Autodrive, we have straight paths between two waypoints. If a vehicle is driving from A to B, we determine the current position of the vehicle, calculate the angle between vehicle position and B, and set
vehicle.orientation accordingly. Something like this should work for you as well, you just have to factor in vehicle.speed somehow.
