beyond-all-reason / spring

A powerful free cross-platform RTS game engine
https://beyond-all-reason.github.io/spring/
Other
210 stars 100 forks source link

Optimise basecontent LUS gadget #1068

Open sprunk opened 10 months ago

sprunk commented 10 months ago

Here are some ways the LUS gadget could be optimized (without touching engine).

And here are some ways to do more with engine support:

Beherith commented 10 months ago

Regarding unitID per unit with shared script. Could units still have their unitID but share functions?

Beherith commented 10 months ago

Also, do you think that the sharing of functions can only be done with an engine update, and cannot be done with a gadget update only?

sprunk commented 10 months ago

Regarding unitID per unit with shared script. Could units still have their unitID but share functions?

I see three basic ways to share functions: 1) call setfenv so the global being referenced is correct. Sounds like it would ruin perf though because not only it is an extra function call that you'd have to call on all functions (since you can't tell which use the global and which don't) and would also force you to use globals (as opposed to being upvalues local to the chunk scope). Note that this also applies to other upvalues that aren't necessarily unitID (think all the static-vars in cob), these would have to turn from local upvalues into globals as well. I don't think this option is viable. 2) pass the environment table as the parameter. This should be better than setfenv but changes the interface. 3) make people explicitly put functions in a "static" portion (for example via staticScript.Foo instead of script.Foo) and keep the rest (who use per-unit upvalues) as-is. Note that in this approach unitID can still be in a static function because of Spring.UnitScript.GetActiveUnitID(). This is IMO the best approach.

do you think that the sharing of functions can only be done with an engine update, and cannot be done with a gadget update only?

This ticket is in the engine repo because that's where basecontent gadgets live but some improvements are gadgetside. This includes the shared functions stuff (note that the engine doesn't even have a concept of unit script environment per se).

Beherith commented 9 months ago

Could a metatable not be used for sharing functions?

sprunk commented 9 months ago

Possibly! Worth investigating.

loveridge commented 4 months ago

Does the order of these calls matter?

Turn(turret, x_axis, math.rad(12), math.rad(34))
Turn(gun, y_axis, math.rad(5), math.rad(6))
Turn(screws, z_axis, math.rad(78), math.rad(90))
sprunk commented 4 months ago

If it's literally those 3 lines like that then no, it's not distinguishable. But you could have

Turn(turret, x_axis, math.rad(12), math.rad(34))
if Spring.UnitScript.IsInTurn(turret, x_axis) then
 -- stuff
end
Turn(gun, y_axis, math.rad(5), math.rad(6))
Turn(screws, z_axis, math.rad(78), math.rad(90))

and now it matters.