I’m making a pretty animation-heavy game, based on All-Star Cheerleading. People will be able to create routines and then play the routines at competitions.
My problem is I’m wondering what is the best way to load animations in this instance? I want them to be smooth (as that is the whole point of the game), but at the same time I don’t want hundreds of animations loaded onto 30 different NPCs at once.
My current plan is when the player is creating the routine, they are loading all of the animations (on their client) that they have unlocked onto each NPC (up to 30 on each Cheer team) so if they go to preview it, it’s already loaded, and then when they go into competition, it only loads the animations that are used in the routine.
I am also using a lot of tweens because I have implemented root motion for the animations.
Can anyone suggest a better method for loading the animations? I am very new to memory management.
Create a pool of animations to reuse instances rather than loading new ones every time. This can help minimize memory allocation and improve performance.
2. Lazy Loading
Load animations only when needed. For example, during routine creation, load animations as the player selects them rather than loading all at once.
3. Streaming and Preloading
Preload animations required for a routine just before the competition starts. This ensures they are ready to go without causing delays during gameplay.
You can likely do a combination of all three. Personally, I would choose to store animations in my pool as they are selected in the server:
local AnimationPool = {}
-- Function to load an animation, reusing from the pool if available
local function LoadAnimation(animationId)
if not AnimationPool[animationId] then
local newAnimation = Instance.new("Animation")
newAnimation.AnimationId = animationId
AnimationPool[animationId] = newAnimation
end
return AnimationPool[animationId]
end
-- Function to play a routine
local function PlayRoutine(npc, routine)
for _, step in ipairs(routine.steps) do
local animation = LoadAnimation(step.animationId)
local animationTrack = npc:LoadAnimation(animation)
animationTrack:Play()
end
end
-- Function to preload animations before a competition
local function PreloadAnimations(routine)
for _, step in ipairs(routine.steps) do
LoadAnimation(step.animationId)
end
end
-- Example usage
local npc = workspace.NPC -- Your NPC
local routine = {
steps = {
{animationId = "rbxassetid://1234567890"},
{animationId = "rbxassetid://2345678901"},
-- Add more steps as needed
}
}
-- Preload animations before the competition
PreloadAnimations(routine)
-- Play the routine
PlayRoutine(npc, routine)
Well hard to say without seeing your current system right now, and does each time have an NPC designated to them? Are the players themselves the ones who are going to be doing the routine animations, kinda confused
Sorry it is a bit complicated and difficult to explain. I don’t have a current system as I wanted to make sure I did it the best way before I made it (not a fan of wasting time).
The way the game would work is each player creates a team of 11-30 athletes (NPCs). They would then create their routine (possibly in a separate server/game idk), picking animations (tumbling, stunting) that would play at certain moments of the routine. It’s basically like a giant animation replay system.
Once they’ve got their routine, it would be played at a competition with the other players. Each routine will have hundreds of animations being played.
Well when scripting something more then likely, you’re going to be rewriting stuff because theres always a better way to do something but I digress
For the problem you’re facing though, since animations are replicated from the client → server, assuming the client has network ownership of their character which obviously they usually do, inside of you’re game you could have the NPCs be owned by those players by setting their rootparts using SetNetworkOwner
what this will do is allow the player to load animations from the client onto their assigned NPCs, and have it replicate to the server, the reason why this will works is because each client is handling the animations on their end but the server is also replicating, I mean the other side of the coin is having the server do all the animations and does that really sound ideal?