What are some good ways to optimize your game-mode?

Does anyone know of ways to make sure your game mode is optimized?

What are the rules not to do? I have read about things like " Do not have a bunch of think hooks".

I am trying to make sure my game mode is optimized before I release a alpha for it.

Some of the issues I am running into are like my HUD.

Also the game-mode I am making is supposed to not make any sense if anyone starts asking questions about what abomination I am working on.

CRAZY+WEIRD = FUN

---------------------------HUD ISSUE---------------------------------

I started working on a HUD today, but I ran into two issues. The fps dropping when drawing a texture on the screen, and the textures having a white line tracing around them. I made sure they had a alpha channel and the background was the color (127,127,127). I exported them as .tga files and then changed them into .vtf files.

Here is how the HUD I am making looks. So far the FPS drops too around 20 with all of that stuff on it.

I have already decided that I am gonna remove the stuff for armor meter, and also I am debating on just using one bullet for the ammo. Now that I look at the ammo it looks retarded with a bunch of them behind it.

http://cloud-2.steampowered.com/ugc/577876801403062360/E7DBB798FDC1DBD711AB64C5932FBF02B7C3DF84/

-----------------------------CODING STUFF-----------------------------------------

Now I only want to know the do’s and don’ts for optimizing your game mode while your coding. I made sure to keep it as organized and simple as possible. I even managed to cut down on a bunch of if statements by using a table for classes. I did try the player.lua way, and class_default way, but I could not get them working, so I just made a table inside of the init, with each class in it that I want. This still allows me to customize the character with ease the way I set it up.

Also I am a tad bit confused with some things. Is there a way to make a table automatically be retrieved from another file when you include that file? I can already guess you have to send the data or something, I was just trying to see if there was another way.

IMO, if you want to optimize. You should bench mark things that are constantly being called and test different methods to see if it runs faster or not.

Or you could post your code(.zip file) and you could be explained to, by using examples of your code.

Make sure you aren’t creating the material every frame

Yea I was a idiot I had my code to declare the textures inside of HUDPaint. My fps shot up past 100 when I took it out of there.

Take a look at this:
https://www.facepunch.com/showthread.php?t=1258511

Also, try to avoid heavy amounts of distance calculations and avoid going through ents.getall too often.

I used to do the same thing

If you want to be crazy stupid optimized you can localize all functions within your files.

For instance, at the top of an average file you would find:
[lua]
local file = file
local math = math
local print = print
local LocalPlayer = LocalPlayer
[/lua]

But that’s only if you want to go nuts.

Real quick. I am trying to make a variable accessable by both the server and the client. I put it in the shared.lua, and it worked last time. When I restarted the gamemode it threw a error at me and can no longer retrive the values.

Here is how the code looks:

shared.lua:


//Round Stuff*****************************************
round = {}

round.level = 0
round.level_limit = 3
round.time = 300
//round.time_limit = 300
round.kill_limit = 30

round.team_adults = 0
round.team_babies = 0


//end round stuff**************************************




//Teams*************************************************

teams = {}

teams[1] = {name = "TEAM_SPECTATOR", color = Vector(.2, .2, .2), weapons = {""} }
teams[2] = {name = "TEAM_ADULTS", color = Vector(1.0, .2, .2), weapons = {""} }
teams[3] = {name = "TEAM_BABIES", color = Vector(.2, .2, 1.0), weapons = {""} }


//Teams end*******************************************





//Classes*********************************************


//-----------------adult classes-----------------------
adult_class = {}

adult_class[1] = {
					name = "Adults", 
					health = 100,
					walkspeed = 110,
					runspeed = 225,
					size = 1,
					model = "",
					taunt = "",
					weapons = {
								"weapon_stunstick",
								"weapon_pistol"
							}
				}

				
				
//------------------Baby classes-------------------------						
baby_class = {}

baby_class[1] = {
					name = "Babies", 
					health = 100,
					walkspeed = 190,
					runspeed = 295,
					size = 1,
					model = "",
					taunt = "",
					weapons = {
								"weapon_crowbar",
								"weapon_pistol"
							}
				}
				
		
//Classes end*******************************************

I can not retrieve the data for the round.level.

Error:




[ERROR] gamemodes/babywars/gamemode/cl_init.lua:360: attempt to index global 'round' (a nil value)
  1. v - gamemodes/babywars/gamemode/cl_init.lua:360
   2. unknown - lua/includes/modules/hook.lua:82


is cl_init.lua include()'d before shared.lua? If so, that could be the problem. The global won’t exist when cl_init runs. This would also explain why it works fine with autorefresh.

Yea that did not fix it. I am a little lost as to what is causing the error.

--------------------------------edit--------------------------------------

I found the problem, but in order to fix it I gotta get rid of team spawning on different sides. I will see how to fix it. If I have and issues I will come back.

Isn’t it only an unnoticeable amount faster to do this though?

Here are my tips for optimizing stuff (note that these are rather minor optimizations that are only really worth while if your running a loop thousands of times or something similar):

Declare temporary variables outside the code block they are used in so they don’t have to be reallocated each time they are needed. The same goes for models or materials and COLORS. Create them outside so your not wasting time allocating memory.

Avoid creating temporary tables or objects (creating a vector just to do a little bit of math with it wastes time by allocating memory. You should either edit the .x, .y, and .z values of an existing vector or, if that vector is being used somewhere else and you need to avoid pointer errors, perform the arithmatic manually then stick it into a new vector (unless your using a constant offset vector in which case that is fine, just don’t create temporary Vectors just to do math with them)

LOCAL VARIABLES
The global variable table is big and slow to index. Always use local variables when possible to reduce indexing time and generally speed stuff up. This is also a good code practice in general to prevent other scripts from changing or overwriting values unexpectedly.

MEMOIZATION (yes that is actually a thing)
If a value isn’t going to change for a given period of time then cache it instead of recalculating it every frame. Believe it or not calculations do actually use up CPU time which can be expensive.

MATH FUNCTIONS TO AVOID:
Calculating math.pow, math.sqrt and math.log can be rather slow. It is better to avoid frequent calls to these functions. For example: x*x will be faster than math.pow(x,2)

Once again, it is worth noting that these optimizations are rather extreme. I would only recommend using them in algorithms that need to run EXTREMELY fast or if you just want to have some fun writing the fastest code possible.

Well for me its more of a thing to just create optimized code. I know at the end of the day int he gaming world, it is not as big of a deal as long as the people who are playing it do not notice.

I just like to know in-case I get issues and to also help keep my coding skills from getting lazy.

-------------------------------One More issue-----------------------------------------------

Ok I am having issues with this. This should be 1 of the last things I need to do to get this into alpha/beta.

The round is not counting up.

Here is the code

this is the end part of the code that it checks every time someone spawns. as not to constantly restart the round.


	if timer.Exists("Roundtimer") then
	
	else
		//begin round
		if team.NumPlayers(2)==1 and team.NumPlayers(3) == 1 then
			GAMEMODE:StartRound()
		else
		//Just for testing purposes
			GAMEMODE:StartRound()
		end
	
	
	end
	

	

end



function GM:StartRound()
	game.CleanUpMap()
			//Start round timer
	timer.Create("Roundtimer", 300, 0, function()
	GAMEMODE:EndRound()
	end)
	
	//start timer
	round.start = 1
	timer.Create("Roundtimer2", 5, 1, function()
	round.start = 0
	end)
	
	
	round.level = round.level + 1	
		
	for k,v in pairs(player.GetAll()) do
		v:ConCommand("play vo/announcer_am_gamestarting04.wav
")
		
		v:Respawn()
		
		if round.level == 0 then
		v:SetFrags(0)
		end
   end
	
	for k, v in pairs( player.GetAll() ) do
		v:Freeze( false)
	end
end

function GM:EndRound()
	timer.Destroy("Roundtimer")

	//freeze all dem peps
	for k, v in pairs( player.GetAll() ) do
		v:Freeze( true )
	end
	
	//Get the winner
	if round.team_adults < round.team_babies then
		//Broadcast to the whole server who won.
		//Adults won
		round.team_adults = round.team_adults + 1
		print("Adults Have Won")
	elseif round.team_adults > round.team_babies then
		//Babies won
		round.team_babies = round.team_babies + 1
		print("Babies Have Won")
	else
		//It is a tie
		round.team_adults = round.team_adults + 1
		round.team_babies = round.team_babies + 1
		print("It is a tie!!!")
	end
	
	//Check if round is on round limit, if not it restarts the round
	if round.level == round.level_limit then
	
	else
	
	//Restarts the round
	GAMEMODE:StartRound()
	end
	
	
end


Also on a side note ROUND.LEVEL++ WHY YOU NO WORK