CS2D Lua: Image Movement for Characters

This isn’t so much a tutorial as it is a reminder to anyone dealing with the same problem. You’re making NPCs, you’re looking to optimise your image movement code and you’re thinking of using tween_move in an ms100 hook? Bad idea.

You see, I had the same thought. I thought, “ms100 gets called five times less often, so surely if I use tween_move I’ll get the same smoothness with even less lag?” Boy was I wrong. Still, I changed it accordingly in Space RPG, and lo – it was smooth. For a while. I left the server running to see how the load would be like, and after some fifteen minutes I rejoined. My framerate immediately fell to 3 FPS.

Let me explain something: my computer is good. It’s not top-of-the-line anymore, but it certainly was two or three years ago, and it can run modern games on high and very high settings very smoothly indeed. Imagine, then, my surprise when a 2D game running in a 640×480 window started running with fewer frames per second than Far Cry 4 on Ultra.

After some optimisations, restarting the server and joining it again with a few more people, we started testing. This time around, I had FPS drops instead of constant low FPS – the framerate would drop to 7-15 every half minute for some 10 seconds. The others, however, weren’t so lucky – their framerate never rose above three, and their game clients eventually crashed.

In the end, I decided against the tween_move + ms100 pattern. You’re welcome, and perhaps even encouraged, to try it yourself, but if you want tested, reliable code, then imagepos + always or ms100 is the way to go.

Published by

avatar

EngiN33R

Developer, human and programming language enthusiast, teacher at heart. Please remember: all my opinions are independent and subjective.

Leave a Reply

Your email address will not be published.