Improve the Animator object!

As a Roblox developer, it’s currently too difficult to call to animations that are loaded or created in two separate functions within the same script, without bloating your script with extra variables.

My current usecase is being able to Load Animations into an AnimationController of a ViewModel, and then play the animation in a Fire function, Equip Function, etc. etc. However, with the way Animations currently work, you have to create multiple different variables that will index upon the animations being loaded. This creates a tremendous amount of bloat to a script. Admittedly, I could be doing it wrong, but I digress.

So I propose these new APIs, which will allow direct access to currently loaded animations:

PlayAnimation('string' name)
StopAnimation('string' name)

This seems a lot more intuitive to me and would signficantly cut down on the number of variables required to handle something like this.

You could, in theory, write code that looks like this:

for i, v in pairs(Animations:GetChildren()
	AnimationController:LoadAnimation(v)
end

--Fire Function--

function fire()
	AnimationController:PlayAnimation("FireAnim")
end

function equip()
	AnimationController:PlayAnimation("EquipAnim")
end

instead of like this:

local Fire = nil
local Equip = nil

for i, v in pairs(Animations:GetChildren()
	AnimationController:LoadAnimation(v)
	
	if v.Name == "FireAnim" then
		Fire = v
	elseif v.Name == "EquipAnim" then
		Equip = v
	end
end

function fire()
	AnimationController:PlayAnimation(Fire)
end

function equip()
	AnimationController:PlayAnimation(Equip)
end

You also would not need to create multiple variables for different objects, making the code much, much cleaner.

1 Like

So, how does the engine know what string to assign to an animation? If we assume it’s based off of the name then what happens when you have multiple animations of the same name?

Restructuring your code can make this sort of thing much cleaner. For example:

local animations = {}

for _, v in ipairs(script.Animations:GetChildren()) do
	animations[v.Name] = animationController:LoadAnimation(v)
end

function fire()
	AnimationController:PlayAnimation(animations["FireAnim"])
end

function equip()
	AnimationController:PlayAnimation(animations["EquipAnim"])
end

I believe it knows by string because if you print PlayingAnimations, it has the exact name that you gave it when you first loaded it - last I checked, anyways.