1. Check out the Store if you're looking to Buy HL2RP or other schemas for your server. You can click Buy Schemas on the very top navigation bar to visit the store.
  2. Use the Plugin Center to easily subscribe to and auto-install Clockwork plugins to your server, or submit and share plugins you have developed.
  3. Having trouble setting up or developing with Clockwork? Check out the Wiki or post in the Support Forum for advice from fellow users.
Dismiss Notice
Hi Guest, you need a Steam account to register and post on these forums. Login with Steam at the top of the forums to get started!
Dismiss Notice
Hi Guest, do you want to buy HL2RP or another Clockwork schema? Visit the Cloud Sixteen Store to get started!

Possibly a newbie question about LUA

Discussion in 'Help' started by connall, Jun 17, 2012.

Thread Status:
Not open for further replies.
  1. connall

    connall Guest

    Right so anyway, I have become more interested in programming in LUA that I have decided to crack open the OA files and have a look through the technique of learning by doing, now there is one thing that I can't wrap my head around and I believe if I can understand how this one thing is done then it might help me understand LUA a bit more.

    Anyway I was looking in the HL2RP sv_hooks file and I came across this line.

    if (self:playerIsCombine(player) or player:QueryCharacter("faction") == FACTION_ADMIN) then

    Right so let me see if I got this right and if not correct me

    If (basic if statement not much to say)

    self: (so You the player not some other player on the server)

    PlayerIsCombine (Seeing if it's one of the combine factions? Rather ambigious, why not OTA? MPF? Or is this one umbrella term used for all factions.)

    (player) -This meaning the players name? E.g OTA-King.1993 or whatever you name them?

    or (Not much to say here either.)

    Player:QueryCharacter (Don't know what this means.)

    ("faction") -Again not sure. Is "faction" just meaning any faction name, but then it's in quotation marks which is usually associated with actually text like "You are dead."

    == FACTION_ADMIN (Okay so whatever the last statement meant, equals the Administrator faction.

    Then (Then do whatever comes next.)

    So any help would be appreciated, there is one thing I want to ask. How can it understand this part: "PlayerIsCombine" is it declared somewhere? I looked in the factions but it says something different FACTION.isCombineFaction = true; so then how does it understand what it means by "PlayerIsCombine" is it two things put together? Player then somewhere in the faction code it would be IsCombine = true. I feel if I could figure out what this means I would be on my way too understanding LUA a bit more. Also can you change it so like PlayerIsCombine to something like PlayerIsVortigaunt how would you do that?

    Any answers to any of my questions would be much appreciated. I also wasn't sure where to post this since it's not really a support issue, if it's in the wrong place I apologise. :(
     
  2. Razor

    Razor Guest

    "self" is not the player. I'll explain you this: If you make a function inside a schema file called "openAura.schema:LOL()", if you want to call it again inside the same file, you have to replace openAura.schema with "self". So it will be "self:LOL()"
    However I didn't get this system yet, and I don't even know how it's called. I am still trying to figure it out.
    I think you have to replace it with "self" also if you call it in a file that is related to the one where you declared the function.

    My opinions from looking at the code, is that:
    Whatever you put before the ":" in a function, is replaced by "self" when you call the function in the same file, or a related file. (for example, if you declare the function in a file related to the server, when you call it in another file related to the server, you have to replace the stuff before ":" with "self".)

    Or maybe you can use "self" in files that include the file where you declared the function?

    However, I am not sure if this right, but I'm probably wrong.

    In other stuff anyways, (such as panels), "self" would be the panel you're working with at the moment... I have to get an idea on this ???
    ______________________________________________________

    Moving to the stuff I know!

    "player" is not the player's name but it's the player's ID. player:Name() would have been the player's name.

    QueryCharacter checks for the mysql data of a character, the data specified in brackets. so ("faction") means that it looks for the faction.

    So the full line basically means:

    If the player is from one of the combine factions, or a city administrator, the code that comes next is run.

    PlayerIsCombine is a function declared in sv_auto.lua, the schema (hl2rp).

    Here's the full code:

    Code:
    -- A function to check if a player is Combine.
    function openAura.schema:PlayerIsCombine(player, bHuman)
    	if ( IsValid(player) and player:GetCharacter() ) then
    		local faction = player:QueryCharacter("faction");
    		
    		if ( self:IsCombineFaction(faction) ) then
    			if (bHuman) then
    				if (faction == FACTION_MPF) then
    					return true;
    				end;
    			elseif (bHuman == false) then
    				if (faction == FACTION_MPF) then
    					return false;
    				else
    					return true;
    				end;
    			else
    				return true;
    			end;
    		end;
    	end;
    end;
     
  3. connall

    connall Guest

    I see. Thanks! It's just all so confusing trying to comprehend it all, when I look at the code it always seems like it is more complicated than it has to be in some cases. The main reason for learning this stuff is so I can add admin commands and the like. I hope Clockwork is easier to understand. :p

    Thanks for the help anyway.
     
  4. connall

    connall Guest

    Uggg. I believe in working to solve your own problems and I wish I could actually get something to work.

    I have been working on a command that when done it will give the target player question boxes they must fill in, which will then notify the person who gave the application his answers. This is what I have so far.

    Code:
    COMMAND = openAura.command:New();
    COMMAND.tip = "Gives an MPF application to the player.";
    COMMAND.text = "<Player Name>";
    COMMAND.flags = CMD_DEFAULT;
    COMMAND.arguments = 1;
    
    -- Called when the command has been run.
    function COMMAND:OnRun(player, arguments)
    	if (player:QueryCharacter("faction") == FACTION_MPF) then
    		local target = openAura.player:Get( arguments[1] );
    		
    		if (target) then
    			Derma_StringRequest("Metropolice Force Application", "Why do you want to join the MPF", nil, function(q1) end);
    		else
                openAura.player:Notify(player, arguments[1].." is not a valid player!");		
    		end;
    	else
    		openAura.player:Notify(player, "You are not an MPF!");
    	end;
    end;
    
    openAura.command:Register(COMMAND, "ApplicationMPF");
    Like I said, work in progress and all seems to work except the Derma_StringRequest because my server tells me this...

    "OpenAura --> the ApplicationMPF command has failed to run.[gamemodes\hl2rp\gamemode\schema\sh_coms.lua:72] attempt to call global 'Derma_StringRequest' (a nil value)"

    Now when I had a similar error it was the command was case sensitive, however this is how all the sites I checked wrote it and it's the same, so I imagine it has to do with the file it's in instead.

    As you can also see, this is still a WIP the final code will have added:

    openAura.player:Notify(player, "Question 1")
    openAuraplayer:Notify(Player "Why do you want to join MPF?")
    openAura.player:Notify(player, q1);

    then a sleep command or delay command (if LUA has one, please god let it have one.) before it shows the answer to the next question. I should also probably point out now that the questions would obviously stack so it would become

    Derma_StringRequest("Metropolice Force Application", "Why do you want to join the MPF", nil, function(q1) end);
    Derma_StringRequest("Metropolice Force Application", "Tell us about yourself", nil, function(q2) end);

    This is probably really ineffecient but I was able to troubleshoot just about all my errors which made me extremely happy, however this one has me stumped.

    Thanks in advance.

    Also this may seem poorly written, I apologise it's 1:30AM and I have been working on this problem for about 2 hours. I need to get some sleep.
     
  5. Razor

    Razor Guest

    This could be a good idea... I think I'll try it myself.
     
  6. Professorial Mister Man

    Professorial Mister Man I don&#039;t know what I&#039;m doing. Active Member

    Well if you guys are already making these things, send me a copy when you finish! ;D
     
  7. connall

    connall Guest

    Damn that's why I didn't want to show the code, I'm quite proud at how I've done. At least send me how you fix it when you do.

    or steam perhaps?
     
  8. Razor

    Razor Guest

    Most likely steam.
     
  9. connall

    connall Guest

    I assume Razor is your steam name. Mines Coble or connall

    EDIT: Lots of Razors it would appear. My original steam name was connall it's now Coble, avatar is Barney.

    EDIT 2: I think I might actually know what the problem is. Also following the clues, I think I found your Steam name.
     
  10. connall

    connall Guest

    If I don't speak to you before tonight Razor I will see if my idea worked.
     
  11. -- Called when the command has been run.
    function COMMAND:OnRun(player, arguments)
    if (player:QueryCharacter("faction") == FACTION_MPF) then
    local target = openAura.player:Get( arguments[1] );


    This part here is wrong. What you're doing is checking if the character is in the MPF faction when it should be checking if they aren't in the MPF faction. So now you just do the opposite.

    "If character is not in faction MPF"


    -- Called when the command has been run.
    function COMMAND:OnRun(player, arguments)
    if (player:QueryCharacter("faction") != FACTION_MPF) then
    local target = openAura.player:Get( arguments[1] );


    Just change the = sign on the left to an exclamation point. That means not equal to.
     
  12. Razor

    Razor Guest

    Mhm, never really noticed that. I could of told him earlier if I noticed :L
     
  13. connall

    connall Guest

    Thanks, though the code has drastically changed since then.

    That is actually there so the person who is giving the application runs the command on the target person

    /applicationMPF Sir Asshat

    That way people don't abuse the command and it's up to the discretion of the MPF when they recruit. I have run into a different problem since then that I'm trying to resolve.

    So only people in the MPF can authorise applications being sent out. ;)
     
  14. Razor

    Razor Guest

  15. connall

    connall Guest

    1. Don't know how to make paper re-editable.

    2. It's just cleaner through a command.

    3. I want something thats mine rather than creating something new off someone else's code.
     
  16. Razor

    Razor Guest

    I meant making an item that acts like an application form.
     
  17. connall

    connall Guest

    Hmm never thought of that. I will work on that on other iterations of this. I am so close to finishing this I might as well finish it.

    I was tempted of finding a way to put it into a Derma menu out of curiosity and too learn.
     

Previous Readers (Total: 0)

Thread Status:
Not open for further replies.