Why is my game lagging?

Monkey Forums/Monkey Beginners/Why is my game lagging?

Impmaster(Posted 2014) [#1]
Hi all. I have a game that I'm making for ludum dare.


I have a game that is lagging on my computer. Now, it's a pretty beefy computer, and i don't know why it's lagging. My other game which has similar code runs at 60. Also, if i restart my computer, it stops lagging.


computercoder(Posted 2014) [#2]
There could be any number of reasons why your game is not performing like you expect it to. Please provide more details around what you are doing in the game that seems to be having problems. It seems to me you have a memory issue going on, you are rendering too many off-screen objects, or you are performing far too many loops between each screen update.

Some quick notes in general:
-- If you are using a tile map, make sure that you are not trying to render more than you need to make sure the scrolling renders smoothly ON-SCREEN. If you have a large map, it will take a lot of time to draw everything the belongs onscreen and what does not.

-- Make sure you destroy all objects that should no longer be in the game. If you keep adding objects that are no longer used, your game will slow down due to the amount of memory your computer has left is being used up.

When you reboot your computer, your memory is freed up and everything goes back normal until the game gets to where it lags again.

Look through your code and see where you are creating and destroying objects.


Impmaster(Posted 2014) [#3]
The only thing I have is a player who walks around. I don't have an array or anything. Just a player and a background image.

EDIT: It seems that whenever I open chrome it starts lagging. However, I have 8 gigs of RAM, I don't know why that would be. :/


computercoder(Posted 2014) [#4]
There have been reports of chrome's latest browser having issues.

Check out this post on the forums:
http://www.monkeycoder.co.nz/Community/posts.php?topic=8381

[EDIT] I was assuming that you are using the HTML5 target in Monkey X? If so, this link will directly apply. If not, then I'm not sure if there are any plugins you have loaded in Chrome that would make your game suffer like that. The other targets should not be affected by *ANY* web browser that I am aware of.


Impmaster(Posted 2014) [#5]
I'm using the desktop target :/....


computercoder(Posted 2014) [#6]
Assuming you are running Windows as your desktop, try looking at the Task Manager to see how much memory AND % of CPU Usage it is consuming. This will tell you better what Chrome is doing while its running.


dawlane(Posted 2014) [#7]
I'm using the desktop target :/....
Which version of Monkey and what OS.


Impmaster(Posted 2014) [#8]
I'm using the latest version of Monkey, on Windows 7. Neither my GPU nor CPU are being taxed. My CPU is at like 50% usage and my gpu is at like 25%. Nothing else is lagging on my computer apart from this program.


dawlane(Posted 2014) [#9]
You will have to reproduce the problem with some code or you will just be getting people making wild guesses. If all the Desktop sample programs that come with Monkey work as they should, then the problem is with your code.


Impmaster(Posted 2014) [#10]
Actually, the program that I thought was working earlier seems to be having the same lag.

Here's a link to the program though.

https://www.dropbox.com/sh/fgbijlzvmi2f3z7/bk8taWXSHZ


dawlane(Posted 2014) [#11]
Test the code from the link, built with both MinGW 4.8.1 and VC2010 and using both Monkey77f and Monkey78g. It works as it should with no choppiness or lagging.
Did you open the Resource Monitor under the Performance tab in the Task Manager? It gives you a detailed view of what's going on. Sometimes I get system slow downs due so a process accessing the hard drive, so check to see if anything is thrashing it. The usual culprits for slow downs are Windows Update and Internet Security suites. There is also the chance that your hard drive needs defragging. The defrag tool that comes with windows is in one word crap. There are a good few alternative out there some are free, but you have to be wary of what you install as these need elevated privileges to access your drives and you never know what they are actually doing.


Goodlookinguy(Posted 2014) [#12]
When I first used Monkey I had what I thought were lag issues. I'm guaranteeing you that it's you and not Monkey in this case. You need to learn about translating with integers and drawing from integer points only. That is to say, cast your floats to integers. It will solve 99% of these perceived problems.


Impmaster(Posted 2014) [#13]
How can I cast floats to integers in Monkey X?


therevills(Posted 2014) [#14]
Local x:Float = 100.5
Local y:Float = 22.4
DrawImage sprite, Int(x), Int(y)



dawlane(Posted 2014) [#15]
To cast one value into another the syntax is type( value ).
e.g.
DrawImage my_image, Int(my_floatX), Int(my_floatY)
.


Goodlookinguy(Posted 2014) [#16]
Local intValue:Int = Int(3.333333) ' 3

Translate(Int(x), Int(y))


Using floating point numbers has a lot of inaccuracy. When the inaccuracies are changed they are never changed at precise intervals. That's what leads to some frames looking like more is happening and then other frames look like less is happening. Casting the floating point values to integers helps to deal with the inaccuracies between the frames. Just thought I would mention that.


Impmaster(Posted 2014) [#17]
Oh I know that. The reason that I said it was laggy was because I was getting bad framerates with Fraps, down in the low 20s and whatnot. However, today I seem to have a steady 30, so I'm fine with that.


Goodlookinguy(Posted 2014) [#18]
You never mentioned you were using recording software. Screen recording software has a tendency to slow down a majority of computers.


Impmaster(Posted 2014) [#19]
I'm not actually recording anything. I'm just using Fraps to have the little frame rate counter in the corner of my screen. And in any case, my computer should be fast enough to run it. I can run Far Cry 3 on almost max graphics at 50-60 fps. I have a gtx 760 in here. I haven't recorded much, but my brother records league of legends matches with fraps sometimes and it doesn't lag. So there must be an out of date Java runtime or something on my computer, whatever Monkey X uses as it's language.


dawlane(Posted 2014) [#20]
The Desktop target uses native CPU machine code.
The only targets that use JAVA are HTML5 and Android.
Monkey X translates your code into a code that can be compiled by another compiler into what code the target use natively.
That's why it can target a number of platforms, and why others can be easily added.

If you are suffering lag then something is running in the background.


therevills(Posted 2014) [#21]
The only targets that use JAVA are HTML5 and Android.

Sorry, but that is incorrect. Java is produced only for Android, and for the HTML5 target JavaScript is generated.

Java is different to JavaScript.


dawlane(Posted 2014) [#22]
@therevills:Yeah I keep forgetting that. But you still need a JAVA Runtime installed to use either type.


therevills(Posted 2014) [#23]
No you don't... For HTML 5 you just need a modern browser.


dawlane(Posted 2014) [#24]
@therevills: I stand corrected. I must brush up on the the latest thing with web browsers. Current modern browsers have their own JavaScript Engine process virtual machine. In stalling Oracles Java 7 comes bundled with the Mozilla base Rhino JavaScript Engine while Java 8 is Nashorn. Google have their own version.


Gerry Quinn(Posted 2014) [#25]
Just on the subject of floats and ints, that can give an effect that may look like lag, but isn't. If you have a sprite moving 7.4 pixels per frame,and it gets painted on exact pixels, it will be seen to move 7 pixels in some frames and 8 in others. This stuttering isn't lag, though - the framerate doesn't change. Rounding the movement down to 7 pixels per frame will give a smoother motion, though.