• Can Someone look over this code please?
    4 replies, posted
Can someone look over this code and comments next to them to see if I'm grasping what functions and statements do ? I tried to lable it in my own way that i'd understand so bear that in mind. FYI: This is a file I made for myself so it's not actually implemented in any gamemode :D [CODE]-- In http://wiki.garrysmod.com/ the Hooks section is for functions so for example: function ENT:Touch( entity ) -- This is a function in the Entity ANIM section because I set it to that in the test_flag.lua at the top :) if ( entity:IsPlayer() ) then -- ok so an if statement is like a check to see if something is in place in game / happening in game or server. a then statement gets ready to tell the code to do it if the if statement happens :) self:Remove(); end; -- there is 2 ends because an if statement was called so it is needed to end it :D end; -- this ends the function called so you can see: function at the top right ? well you always need to remember to end it :D -- Also in the wiki the classes are like what certain things can do inside all functions...pretty handy to know :D --local means it only can be used in that function so: function GM:CreateFlags() --Custom function named at my choice for creating the flags :D local redflag = ents.Create("test_flag"); -- So I created a Local which means it creats a custom local variable inside the function...Notice how I named it redflag ? that's what its refered to if I want to call it. ents.create is an entity class for ent section ;D to actualy create the entity and spawn it redflag:SetPos( Vector(Co-Ordinates here)); redflag:Spawn(); --for it to actually spawn :D redflag:SetTeam(TEAM_KEK); --What team does the entitie do the action for ? (teams made in shared.lua) local blueflag = ents.Create("test_flag"); -- So I created a Local which means it creats a custom local variable inside the function...Notice how I named it redflag ? that's what its refered to if I want to call it. ents.create is an entity class for ent section ;D to actualy create the entity and spawn it blueflag:SetPos( Vector(Co-Ordinates here)); blueflag:Spawn(); --for it to actually spawn :D blueflag:SetTeam(TEAM_FUKBOI); --What team does the entitie do the action for ? (teams made in shared.lua) end; function GM:InitPostEntity() -- Ok so this function is for after the entite is loaded, basically telling it what to do :) self:CreateFlags() end; --then in lua is for when you want to do something after another piece of code. usually used after an if, or, and statement For example: function ENT:Touch(entity) if ( entity:IsPlayer() ) then -- Notice how it goes: if the entity touches the player (Because we are in ent:touch function) THEN <---MAGIC WORD....the player dies self:Kill(); end; end;[/CODE]
First things first, you are trying to use GM:* hooks in an entity, this is wrong! You must use hook library ( hook.Add ) for GM:* hooks outside of the gamemode folder.
I would advise against directly overriding GM hooks the way you are doing, especially with something as commonly used in gamemodes as InitPostEntity. Instead, create your hooks using hook.Add. The callback doesn't return the gamemode table however, so to call the CreatFlags function, you will have to call GAMEMODE:CreateFlags(), though I don't understand why you aren't just putting the code directly in the InitPostEntity hook. Also, semi colons aren't required in lua, though if you are going to use them for style, I find it best if you are consistent with where you use it. I use semi colons after function calls and variable definitions, never after keywords or within function calls. [editline]3rd July 2015[/editline] Why is everyone ninja'ing me as of late.
[QUOTE=Robotboy655;48110996]First things first, you are trying to use GM:* hooks in an entity, this is wrong! You must use hook library ( hook.Add ) for GM:* hooks outside of the gamemode folder.[/QUOTE] Was told not to do that in the video I was watching, But I'll start using hook.Add from now on. I assumed it was for addons :P [editline]3rd July 2015[/editline] [QUOTE=James xX;48111003]I would advise against directly overriding GM hooks the way you are doing, especially with something as commonly used in gamemodes as InitPostEntity. Instead, create your hooks using hook.Add. The callback doesn't return the gamemode table however, so to call the CreatFlags function, you will have to call GAMEMODE:CreateFlags(), though I don't understand why you aren't just putting the code directly in the InitPostEntity hook. Also, semi colons aren't required in lua, though if you are going to use them for style, I find it best if you are consistent with where you use it. I use semi colons after function calls and variable definitions, never after keywords or within function calls. [editline]3rd July 2015[/editline] Why is everyone ninja'ing me as of late.[/QUOTE] I did not realise GAMEMODE:customname() was a thing...guess I have been doing it old fashioned way :P
[QUOTE=BeZerk;48111107]Was told not to do that in the video I was watching, But I'll start using hook.Add from now on. I assumed it was for addons :P [editline]3rd July 2015[/editline] I did not realise GAMEMODE:customname() was a thing...guess I have been doing it old fashioned way :P[/QUOTE] GM is the gamemode table before its loaded, GAMEMODE is the gamemode table while its running. But as said above, you needn't worry about this if you use the hook module correctly.
Sorry, you need to Log In to post a reply to this thread.