Stack Overflow ?

Hello there,

I am trying to make it so when you pick a team (via. the Derma Panel) and it changes them to the team, but there are a few problems, :frowning:

  1. you stay on spectate camera
  2. i get this error

ERROR: GAMEMODE:‘PlayerSpawn’ Failed: gamemodes\sandbox\gamemode\init.lua:40: stack overflow

I dont know what it means :frowning: , here is my init.lua

[LUA]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )

include( ‘shared.lua’ )

function GM:PlayerInitialSpawn( ply )
ply:SetTeam(3)
ply:Spectate(5)
SendUserMessage( “team_menu”, ply )
end

function PlayerDeathSpectate( ply )
ply:Spectate(5)
SendUserMessage( “team_menu”, ply )
end
hook.Add(“PlayerDeathSpectate”, PlayerDeathSpectate)

function GM:PlayerLoadout( ply )
ply:StripWeapons()
ply:RemoveAllAmmo()

if ply:Team() == 1 then
	ply:Spawn()
	ply:Give( "weapon_crowbar" )

elseif ply:Team() == 2 then
ply:Spawn()
ply:Give( “weapon_smg” )
ply:Give( “weapon_pistol” )
ply:Give( “weapon_crowbar” )

elseif ply:Team() == 3 then
return
end

end

function GM:ShowHelp( ply )
SendUserMessage( “team_menu”, ply )
end

concommand.Add( “team_1”, function (ply)
ply:SetTeam(1)
ply:Spawn()
end )

concommand.Add( “team_2”, function (ply)

ply:SetTeam(2)
	ply:Spawn()

end )

concommand.Add( “team_3”, function (ply)
ply:SetTeam(3)
ply:Spectate(5)
end )

[/LUA]

I dont know but it could be to do with your return in loadout

It is probably because you are spawning the player in GM.PlayerLoadout, which is calling GM.PlayerSpawn, which in turn is calling GM.PlayerLoadout again, causing a stack overflow.

So how could i fix this? , im gettin very confused, :frowning:

Remove all the ply:Spawn() shit

[editline]02:27PM[/editline]

It makes it call player spawn, which calls loadout, which calls spawn, which cause a infinite loop and and overflows the stack

I have removed all the the ply:Spawn() and it still happens :S

Think of the stack as a pile of plates. When you add a plate on top, to get to the plate underneath it you need to take the plate off the top of the stack of plates. This is called “popping” the stack.

When you call a function inside a function, it leaves a trace back to the first function you called from.

[lua]function derp()

end

function herp()
derp()
end

function bro()
herp()
end
[/lua]

When you call bro, the stack will look like this

derp
herp
bro

In order to continue the bro function, you need to finish “derp”. After finishing derp, you finish herp. Then you can finish “bro”, and the stack is empty.

However, what you did was add a function back onto the stack, which adds the original function to the stack. Something like this.

[lua]
function herp()
derp()
end

function derp()
herp()
end[/lua]

This means that the stack will look like this


herp
derp
herp
derp
herp
derp
herp
derp
herp
derp
herp
derp
herp
derp
herp
derp
herp
derp

An infinite number of times. Obviously, you want to avoid this.

That’s a nice analogy you have there.

thankyou for this, this has made it easier to understand (: but ima be anoying, andi still dont know how to fix this problem :frowning:

Post your current code.

[LUA]
AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )
AddCSLuaFile( “cl_scoreboard.lua” )

include( ‘shared.lua’ )

function GM:PlayerInitialSpawn( ply )
ply:SetTeam(3)
ply:Spectate(5)
SendUserMessage( “team_menu”, ply )
end

function PlayerDeathSpectate( ply )
ply:Spectate(5)
SendUserMessage( “team_menu”, ply )
end
hook.Add(“PlayerDeath”, “PlayerDeathSpectate”, PlayerDeathSpectate)

function GM:PlayerLoadout( ply )
ply:StripWeapons()
ply:RemoveAllAmmo()

if ply:Team() == 1 then
	ply:Give( "weapon_crowbar" )

elseif ply:Team() == 2 then
ply:Give( “weapon_smg” )
ply:Give( “weapon_pistol” )
ply:Give( “weapon_crowbar” )

elseif ply:Team() == 3 then
ply:Spectate(5)
end

end

function GM:ShowHelp( ply )
SendUserMessage( “team_menu”, ply )
end

concommand.Add( “team_1”, function (ply)
ply:SetTeam(1)
ply:Spawn()
end )

concommand.Add( “team_2”, function (ply)

ply:SetTeam(2)
	ply:Spawn()

end )

concommand.Add( “team_3”, function (ply)
ply:SetTeam(3)
ply:Spectate(5)
end )

[/LUA]

You’re making them spectate in playerloadout, try removing that.

doing that still comes with the error :frowning:

Is it the exact same error as before?

Don’t you need to use self.BaseClass in a few functions when deriving from sandbox?

[lua] self.BaseClass:PlayerInitialSpawn ( ply )[/lua]

In GM:PlayerInitialSpawn

[editline]08:24PM[/editline]

And then
[lua]self.BaseClass:PlayerSpawn ( ply )[/lua]

In GM:PlayerSpawn

[editline]08:25PM[/editline]

I have no idea what they do, apparantly they fix a few bugs, but otherwise nothing else.

They call the function that you are overriding.

Well, you learn something new everyday :smiley:

I know it may not make a difference in this case, but it is better to do this:

[lua]self.BaseClass.PlayerSpawn(self, ply)
– self.BaseClass:PlayerSpawn(ply) – not this[/lua]

[editline]08:32PM[/editline]

Broke my automerge :frowning:

I brake most things, get used to it :3:

Also, why would that change be of any diffference?

I also have a question as well.

When using surface.DrawTexturedRect, can I use ScrW() and ScrH() for both the position and the size? or are they just meant for position?