• Where did I go wrong?
    5 replies, posted
Hello guys I hit a little snag while working on my gamemode here, I was hoping someone could shed some light on it. Please don't make fun this is my first coding project ever, I have NEVER started out in an empty file. I'm sure I coded things like a barbarian, I have only done minor edits/change variables to existing code in the past. The Problem: It currently just cycles through each round phase extremely fast. It goes from **Round Preparing----->Round is Starting----->Round has Ended.** I have been messing with this for a few hours and I can't figure it out, ANY help would be very much appreciated! [code] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( "shared.lua" ) ---------------------------------------------------------------------------------------------- -- Round structure shit, I will move it to its own seperate file when its working as intended. ---------------------------------------------------------------------------------------------- CreateConVar( "hns_roundtime_seconds", "45" ) CreateConVar( "hns_postround_seconds", "8" ) CreateConVar( "hns_preround_seconds", "3" ) CreateConVar( "hns_round_limit", "8" ) CreateConVar( "hns_freezetime_seconds", "20" ) SetGlobalVar( "RoundPhase", ROUND.WAITING ) Round_Phase = GetGlobalVar( "RoundPhase" ) RoundsLeft = GetConVar( "hns_round_limit" ):GetInt() freezetime = GetConVar( "hns_freezetime_seconds" ):GetInt() Time = {} Time.Waiting = 0 Time.Active = GetConVar( "hns_roundtime_seconds" ):GetInt() Time.Post = GetConVar( "hns_postround_seconds" ):GetInt() Time.Pre = GetConVar( "hns_preround_seconds" ):GetInt() ROUND = {} ROUND.WAITING = ROUND.WAITING ROUND.PRE = ROUND.PRE ROUND.ACTIVE = ROUND.ACTIVE ROUND.POST = ROUND.POST function RoundTimer() --Just subtracts 1 second from each round phase if !ROUND.WAITING then Time.Pre = Time.Pre - 1 Time.Active = Time.Active - 1 Time.Post = Time.Post - 1 end end timer.Create( "RoundTimerFunc", 1, 0, RoundTimer ) hider = team.NumPlayers( TEAM_HIDER) seeker = team.NumPlayers( TEAM_SEEKER) function GM:Think() if #player.GetAll() < 2 then -- If we dont have enough players we will keep it in the waiting Phase. SetGlobalVar( "RoundPhase", ROUND.WAITING ) print("WAITING FOR PLAYERS") end if Round_Phase == ROUND.WAITING and hider >= 1 and seeker >=1 then -- If each team has 1 player, we will move on the to PreRound Phase. SetGlobalVar( "RoundPhase", ROUND.PRE ) print( "Preparing the round" ) end if hider >= 1 and seeker >=1 then -- If in the Pre-Round Phase, and each team has a player, and in time is 0, we will move on to the ActiveRound Phase. elseif Time.Pre <= 0 and Round_Phase == ROUND.PRE then SetGlobalVar( "RoundPhase", ROUND.ACTIVE ) print ( "The Round is starting" ) end if Round_Phase == ROUND.ACTIVE and Time.Active >=0 then -- If in Active-Round Phase, and time is 0, and a team has 0 players, we will move on to the PostRound Phase SetGlobalVar( "RoundPhase", ROUND.POST ) print ( "The Round Is Ending" ) end if Round_Phase == ROUND.ACTIVE and seeker == 0 or hider == 0 then SetGlobalVar( "RoundPhase", ROUND.POST ) print ( "The Round Is Ending" ) end if Round_Phase == ROUND.POST and Time.Post >=0 then -- If in the Post-Round Phase and time 0, we will move on to the Pre-Round Phase. SetGlobalVar( "RoundPhase", ROUND.PRE ) end end function GM:RoundStructure() if Round_Phase == ROUND.WAITING then Time = Time.Waiting end if Round_Phase == ROUND.PRE then RoundsLeft = RoundsLeft - 1 game.CleanUpMap() Time = Time.Pre end if Round_Phase == ROUND.ACTIVE then Time = Time.Active end if Round_Phase == ROUND.POST then Time = Time.Post end if RoundsLeft == 1 then print("CHANGE THE MAP FOOL!") end end function WhoWon( ply ) --Simple win checker to see who won the round. --If round phase is Active and Time=0 and the hiders have players we will default them to winner. if Round_Phase == ROUND.ACTIVE and Time.Active <= 0 and team.NumPlayers( TEAM_HIDER) >= 1 then print("THE HIDERS HAVE WON THE ROUND" ) --If round phase is Active and a team has 0 players, we will find the winning team. elseif Round_Phase == ROUND.ACTIVE and hider == 0 or seeker == 0 then local winner = ply:Alive(), team.GetName -- print( "" ..winner.. "HAS WON THE ROUND" ) --I'm sure I fucked this up end end timer.Create( "WinChecker", 1, 0, WhoWon ) [/code]
I don't want to go completely off the topic, but why are all your variables global ? And why do you make a separate function for each round ? And why are you hooking "round checkers" to Think hook ?
[QUOTE=Netheous;40833247]I don't want to go completely off the topic, but why are all your variables global ? And why do you make a separate function for each round ? And why are you hooking "round checkers" to Think hook ?[/QUOTE] Don't I only have one global var? The rest are console commands so people can easily edit things in the server.cfg. Also I wasn't sure If they would have to be global so I could access them client side while doing my GUI's. **Edit: Would setting variables in the shared.lua be a way around using GlobalVariables? At the time is was easier for me to visualize everything that way, but I will end up putting them all in a RoundStructure() function. I was using the Think function because thats where I started and it was before I learned how to use timers like I did in the WinChecker(). Would I be better off to do a timer or just hook into the Think function rather then using it directly? Thanks for any input! Edit **Don't forget this is my cherry for coding :p**
First off the entirety of your code is complete shit. That being said, I didn't bother optimizing it, but it should work now. [lua] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include( "shared.lua" ) ---------------------------------------------------------------------------------------------- -- Round structure shit, I will move it to its own seperate file when its working as intended. ---------------------------------------------------------------------------------------------- CreateConVar( "hns_roundtime_seconds", "45" ) CreateConVar( "hns_postround_seconds", "8" ) CreateConVar( "hns_preround_seconds", "3" ) CreateConVar( "hns_round_limit", "8" ) CreateConVar( "hns_freezetime_seconds", "20" ) -- We move the table up here because the 'RoundPhase' var is trying to access it. ROUND = {} -- Unless these variables existed elsewhere, these variables will be accesing "nil" ROUND.WAITING = 1 ROUND.PRE = 2 ROUND.ACTIVE = 3 ROUND.POST = 4 SetGlobalVar( "RoundPhase", ROUND.WAITING ) Round_Phase = GetGlobalVar( "RoundPhase" ) RoundsLeft = GetConVar( "hns_round_limit" ):GetInt() freezetime = GetConVar( "hns_freezetime_seconds" ):GetInt() Time = {} Time.Waiting = 0 Time.Active = GetConVar( "hns_roundtime_seconds" ):GetInt() Time.Post = GetConVar( "hns_postround_seconds" ):GetInt() Time.Pre = GetConVar( "hns_preround_seconds" ):GetInt() function RoundTimer() --Just subtracts 1 second from each round phase if !ROUND.WAITING then Time.Pre = Time.Pre - 1 Time.Active = Time.Active - 1 Time.Post = Time.Post - 1 end end timer.Create( "RoundTimerFunc", 1, 0, RoundTimer ) local hider = team.NumPlayers( TEAM_HIDER) local seeker = team.NumPlayers( TEAM_SEEKER) local function CheckRound() if #player.GetAll() < 2 then -- If we dont have enough players we will keep it in the waiting Phase. if GetGlobalVar("RoundPhase") != ROUND.WAITING then SetGlobalVar( "RoundPhase", ROUND.WAITING ) end print("WAITING FOR PLAYERS") -- We stop the entirety of the rest of the function. return end if Round_Phase == ROUND.WAITING and hider >= 1 and seeker >=1 then -- If each team has 1 player, we will move on the to PreRound Phase. SetGlobalVar( "RoundPhase", ROUND.PRE ) print( "Preparing the round" ) end if hider >= 1 and seeker >= 1 and Round_Phase == ROUND.PRE and Time.Pre <= 0 then -- If in the Pre-Round Phase, and each team has a player, and in time is 0, we will move on to the ActiveRound Phase. SetGlobalVar( "RoundPhase", ROUND.ACTIVE ) print ( "The Round is starting" ) end if Round_Phase == ROUND.ACTIVE and Time.Active <= 0 or seeker == 0 or hider == 0 then -- If in Active-Round Phase, and time is 0, and a team has 0 players, we will move on to the PostRound Phase SetGlobalVar( "RoundPhase", ROUND.POST ) print ( "The Round Is Ending" ) end if Round_Phase == ROUND.POST and Time.Post <= 0 then -- If in the Post-Round Phase and time 0, we will move on to the Pre-Round Phase. SetGlobalVar( "RoundPhase", ROUND.PRE ) end end timer.Create("CheckingRound", 1, 0, function() CheckRound() end) function GM:RoundStructure() print("Didn't modify this portion. Commented out.") --[[if Round_Phase == ROUND.WAITING then Time = Time.Waiting end if Round_Phase == ROUND.PRE then RoundsLeft = RoundsLeft - 1 game.CleanUpMap() Time = Time.Pre end if Round_Phase == ROUND.ACTIVE then Time = Time.Active end if Round_Phase == ROUND.POST then Time = Time.Post end if RoundsLeft == 1 then print("CHANGE THE MAP FOOL!") end]] end -- Not exactly what the fuck you were trying to accomplish here, so i commented it out. --[[function WhoWon( ply ) --Simple win checker to see who won the round. --If round phase is Active and Time=0 and the hiders have players we will default them to winner. if Round_Phase == ROUND.ACTIVE and Time.Active <= 0 and team.NumPlayers( TEAM_HIDER) >= 1 then print("THE HIDERS HAVE WON THE ROUND" ) --If round phase is Active and a team has 0 players, we will find the winning team. elseif Round_Phase == ROUND.ACTIVE and hider == 0 or seeker == 0 then local winner = ply:Alive(), team.GetName -- print( "" ..winner.. "HAS WON THE ROUND" ) --I'm sure I fucked this up end end timer.Create("WinChecker", 1, 0, function() WhoWon end)]] [/lua] I didn't bother with the WinChecker or the GM:RoundStructure() but other then that, I guess it should work now.
ShinyCow thanks for the help! If you happen to have time could you show me the proper way to do things? I am trying to get better at coding and I don't want to code things like a complete asshole. thanks again!
I am glad he fixed stuff I mentioned, I am way too sleepy to do anything right now, but it was bothering me
Sorry, you need to Log In to post a reply to this thread.