ID:266311
 
What's the best way to prevent lag in a game? I know that if you code a game correctly a person should have minimal lag in it, sadly I'm not that experienced to be able to code like that, so what are some things that could help? For example, does putting all of your icons into one icon folder help at all, or putting all of your coding into one file, as opposed to making seperate files for specific types of coding? Thank you.

=The Wizard of How=
The only thing I know of to prevent lag (aside from getting DSL or Cable) is to make sure your game is running as few things as possible. That means instead of having a stat update proc that runs every 0.1 seconds, you might make it run every 5 seconds, and that would decrease lag.
Well usually in games people spam. There is certain things such as html encode that will block out them from using bold or font size and etc. There is also Length text which minimizes what people can say. Also you can code in which you can make them wait to talk again.
In response to Foomer
How much do you think the sleep() lags a game?
In response to The Wizard of How
sleep() only delays things, Foomer means calling procs like:

proc
weather()
//weather
sleep(10)
weather()//Causing a loop every second


Using a proc like that can cause lag (more/less depending on what the proc does) because it's called once a second.

proc
weather()
//weather
sleep(100)//Better
weather()


Will cause less lag than the other example because it gives the CPU more time to recover from the effects of the proc before it's called again.
In response to Darke Rage
That doesn't effect the lag of the actual game, it just effects the amount of information being passed through the connection which is like squeezing a hose, it just slows the amount of information that can get through your connection. (As far as I know.) That's lag caused by players, not by the game. It's the difference between the Anti-Lag topic and the Spam-Protection topic.

Along those lines though, the more information that is getting sent to you from the game, the less room you have for information. If your modem supports 100 information points per second, and the game sends you 50, you won't get any lag (unless you're downloading something and hogging 75 points from the connection, or something like that). If the game is sending you 110 points of information though, that means 10 points of that can't get through that seconds, so it will have to wait for the next second, which makes things a bit choppy. If the game sends you 300 points of information, it will take three seconds for your modem to get all that information, which means you'll get tons of lag!

The goal in Anti-Lag techniques is to keep the amount of information points sent from the game to the player to a minimum. The less things information that is being sent from the host to the client, the less lag that will be experienced. So if the host isn't updating the client's information every 0.1 seconds, but instead updates it every 10 seconds, then the player might only get an additional 10 points of information every 10 seconds, instead of every 0.1 seconds.

Get it?

Correct me if I'm wrong.
You can use an EventLoop to control how often mobs and such perform actions.

byond://Deadron.SimpleLoop

Evilkevkev
In response to Evilkevkev
Evilkevkev wrote:
You can use an EventLoop to control how often mobs and such perform actions.

byond://Deadron.SimpleLoop

Thanks for the reference. Maybe someday I'll win this war!

Anyway, yes, and the important thing is to make sure your players and mobs have their acting times offset from each other. It's a common mistake to start all mobs in the same tick and give them all the same action delay...then they do everything at once and lag the system.

I always create mobs with something like this (where next_action_time is the variable that controls when they move):

var/mob/new_mob = new()
new_mob.next_action_time = world.time + rand(10)