Add a damage parameter for projectiles to surface.create_entity
Posted: Tue Apr 22, 2025 8:58 pm
I am spawning projectiles at runtime with an `on_script_trigger_effect`, but they are not inheriting the damage modifiers from projectile damage technologies. I am setting the source and cause entities when available, but the projectiles still only apply the base projectile's damage. I spoke with Rseding91 and he mentioned an API call for setting the damage modifier from script when creating the projectile is what's currently missing. The code I'm using to spawn the projectile is:
And the force it's spawned with does have the correct modifiers defined, though since the modifier is coded into the projectile at spawn and inherits it from the item, not the source entity, it makes sense that this doesn't work as it currently stands. Adding a damage_modifier parameter to the surface.create_entity function call for projectiles would entirely fix this issue.
Aside/details:
It would also be potentially handy for the script_trigger_effect event to have an item parameter that could be used here, since the quality of the ammo item used would also be valuable, though that I can determine from the source_entity and isn't strictly necessary as a result.
The reason I'm using a script to spawn the projectile rather than using a projectile trigger delivery is because:
1) the direction_deviation effect isn't applied for projectiles spawned with the ammo_type's target_type set to "entity" (which is important for this effect to work).
2) when the same projectiles are fired at asteroids, they don't actually have a target_entity set, and instead just a target_position. I'm assuming this is for understandable optimization reasons, but the target_position given doesn't account for target leading, so when an asteroid is moving perpendicular to or away from the turret, almost all of the projectiles "hit" where the asteroid was and don't do any damage as a result, which causes a lot of ammunition to be wasted attempting to hit the target.
If those issues were solved instead (and could be done in a performant way), that would greatly improve the performance of my mod since I wouldn't need to use on_script_trigger_effect for each projectile fired. While the direction_deviation is a visual polish thing, the aspect that's important for the gameplay is the target leading when the target is converted to a position, since that wastes a lot of bullets and defeats the purpose of the ammo being resource-efficient.
Thank you for reading!
Code: Select all
local projectile = surface.create_entity{
name = "smart-rounds-magazine",
source = event.source_entity,
position = event.source_position,
force = event.source_entity and event.source_entity.force,
target = event.target_entity or event.target_position,
cause = event.cause_entity,
item = ammo_stack, -- gotten from the source_entity, has no effect
speed = 0.5
}
Aside/details:
It would also be potentially handy for the script_trigger_effect event to have an item parameter that could be used here, since the quality of the ammo item used would also be valuable, though that I can determine from the source_entity and isn't strictly necessary as a result.
The reason I'm using a script to spawn the projectile rather than using a projectile trigger delivery is because:
1) the direction_deviation effect isn't applied for projectiles spawned with the ammo_type's target_type set to "entity" (which is important for this effect to work).
2) when the same projectiles are fired at asteroids, they don't actually have a target_entity set, and instead just a target_position. I'm assuming this is for understandable optimization reasons, but the target_position given doesn't account for target leading, so when an asteroid is moving perpendicular to or away from the turret, almost all of the projectiles "hit" where the asteroid was and don't do any damage as a result, which causes a lot of ammunition to be wasted attempting to hit the target.
If those issues were solved instead (and could be done in a performant way), that would greatly improve the performance of my mod since I wouldn't need to use on_script_trigger_effect for each projectile fired. While the direction_deviation is a visual polish thing, the aspect that's important for the gameplay is the target leading when the target is converted to a position, since that wastes a lot of bullets and defeats the purpose of the ammo being resource-efficient.
Thank you for reading!