Hello you!
I’ve come across a dilemma here. Which one is better, using module script or using normal scripts to control the units?
-
What does the code do and what are you not satisfied with?
For now, it handles the movement of a unit in an RTS-make-army-and-fight game. -
What potential improvements have you considered?
For one, using the modular approach lets me have less headache coding the logic in the long run (still more headache-y in the short term though) -
How (specifically) do you want to improve the code?
Choose one, I just want to know what’s better
(Also I accept better code for the timing of when to stop. lol)
Modular Approach :
-- This is an example Lua code block
local _module = require(game:GetService("ServerScriptService"):WaitForChild("UnitMainLogic"):WaitForChild("LandUnit"))
_module.Initialize(script)
Normal Approach :
(Note that module.Initialize is the same as this whole code block)
local _remotes = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents")
local _logicHelper = require(game:GetService("ServerScriptService"):WaitForChild("UnitLogicHelper"))
local model = script.Parent
local _speed = model.Settings._speed
local _moveVelocity = model.MoveVelocity
local _gyro = model.Gyro
repeat
wait(0.1)
until model:CanSetNetworkOwnership()
model:SetNetworkOwner(nil)
local function Here()
local here;
if model:IsA("BasePart") then
here = model.Position
else
here = model.PrimaryPart.Position
end
return here
end
local function ActivatePhysics()
_moveVelocity.Velocity += Vector3.new(0,-9.81,0)
end
local now = os.time()
function Move (player, unitArray, positionArray)
now = os.time()
local _index = _logicHelper.GetUnitIndex(model,unitArray)
local vector3 = positionArray[_index]
if vector3 == nil then
return
end
local _rotTarget = Vector3.new(vector3.x,model.Position.y,vector3.z)
local rotation = CFrame.lookAt(Here(),_rotTarget)
local velocity = (vector3 - Here()).Unit * _speed.Value * Vector3.new(1,0,1)
_moveVelocity.Velocity = Vector3.new(velocity.x, _moveVelocity.Velocity.y, velocity.z)
_gyro.CFrame = rotation
_gyro.MaxTorque = Vector3.new(1,1,1) * 40000000
local _timeTaken = (vector3 - Here()).Magnitude / _speed.Value
print(_timeTaken)
repeat
wait(0.1)
until os.time() - now > _timeTaken
now = os.time()
_moveVelocity.Velocity = Vector3.new(0,_moveVelocity.Velocity.y,0)
end
ActivatePhysics()
_remotes.OnSetPosition.OnServerEvent:Connect(Move)
Both code works, but I have a bias that the normal approach works better for performance. Am I imagining things or is there no difference at all?
Also this is why I’m asking :
Blue cuboid is the one with module script,
Green cuboid is the one with normal script.
Or is it that my Initialize() code is just less good?
I do notice some weird times where the unit just stop too early. (see : Green cuboid)