Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Lua in SourceOP
#1

So I've finally started implementing some SourceOP stuff in Lua. What this means is that you will be able to customize or add commands and other things in SourceOP. Here's an example of e_kill.
Code:
function drawkillbeam( startpos, endpos )
  local beamindex = util.PrecacheModel("effects/laser1.vmt");
  effects.Beam(startpos, endpos, beamindex, 0, 0, 0, 0.2, 5, 5, 0, 5, math.random(200,255), math.random(0,15), math.random(0,15), 255, 20)
  effects.Beam(startpos, endpos, beamindex, 0, 0, 0, 0.2, 5, 5, 0, 1, math.random(200,255), math.random(100,146), math.random(32,96), 255, 20)
end

// specifies entities that are not valid for killing
function validkillent( classname )
  if(string.sub(classname, 1, 12) == "prop_vehicle") then
    return false
  end
  return true
end

function ekill( playerid, command, arguments )
  local pPlayer = player.GetByID(playerid)
  if(pPlayer) then
    if(pPlayer:IsAdmin(1024, "e_kill")) then
      if(!pPlayer:IsEntMoving()) then
        local ent, endpos = pPlayer:FindEntityForward(MASK_ALL)
        if(ent) then
          local classname = ent:GetClassname()
          if(validkillent(classname)) then
            drawkillbeam(pPlayer:GetAbsOrigin(), endpos)
            if(classname != "player") then
              // kill the entity with explosion sound
              ent:EmitSound("^weapons/explode" .. math.random(3,5) .. ".wav", 60, math.random(90,110))
              ent:Kill()
            else
              // slay the player
              local pDeadPlayer = player.GetByID(ent:EntIndex())
              pDeadPlayer:Kill()
            end
          end
        end
      else
        pPlayer:SayText("You are currently moving an entity and cannot run " .. command .. ".\n")
      end
    else
      pPlayer:SayText("You do not have access to the command " .. command .. ".\n", HUD_PRINTCONSOLE)
    end
  else
    // command run at server console
    // kill each entity id specified on command line
    for k,v in pairs(arguments) do
      if k >= 1 then
        local ent = ents.GetByIndex(tonumber(v))
        ent:Kill()
      end
    end
  end
end

if(sourceop.FeatureStatus(FEAT_ENTCOMMANDS)) then
  concommand.Add("e_kill", ekill)
end

What can you do with this?
Well, one example I thought of is if I or anybody else wanted, we could add another command (e.g e_kill_fromplayer) based off this code that allows an admin to force a player to run e_kill. This command could be run from the server console or executed by event scripts or something to force a player to e_kill. In fact, such a command would be simple:
Code:
function ekillfromplayer( playerid, command, arguments )
  local pPlayer = player.GetByID(arguments[1])
  if(pPlayer) then
    local ent, endpos = pPlayer:FindEntityForward(MASK_ALL)
    if(ent) then
      local classname = ent:GetClassname()
      if(validkillent(classname)) then
        drawkillbeam(pPlayer:GetAbsOrigin(), endpos)
        if(classname != "player") then
          // kill the entity with explosion sound
          ent:EmitSound("^weapons/explode" .. math.random(3,5) .. ".wav", 60, math.random(90,110))
          ent:Kill()
        else
          // slay the player
          local pDeadPlayer = player.GetByID(ent:EntIndex())
          pDeadPlayer:Kill()
        end
      end
    end
  end
end

concommand.Add("e_kill_fromplayer", ekillfromplayer)
I haven't tested that e_kill_fromplayer code, but it should work. Almost all of this code was reused from e_kill.

This is just a preview of things to come. Any comments?
Reply
#2

This is perfect... very useful for us Eventscript coders. Thanks!

Will these also work with your +commands, eventually?
Reply
#3

In one way or another, yes. I'm not sure how I'm going to go about it. For example, do I make +grabent just do pPlayer:StartEntMoving() or do I write out all the code for that in Lua. Either way, you should be able to do what you want with those eventscripts but writing it all out in Lua will make it more robust.

Also, since the forum doesn't do a very good job of making code look good, I posted a prettier version at http://www.sourceop.com/e_kill.lua.html .
Reply
#4

When will this be in the download-able sourceop? Sounds like fun stuff.
Reply
#5

Next release. Perhaps early January?
Reply
#6

so if i learn LUA scripting you are saying I can make my own commands for source op? is there compiling involved or would id just be in some sort of text file?

edit
ps dont know anything about LUA incase (this seems like a dumb Q).
Reply
#7

it would be in an LUA file, and as long as you write it to be compatible and set SourceOP to look for it, it will work fine

/sarcasm
Reply
#8

The way it works is a script called lua/includes/init.lua is loaded at server start. After this, all scripts in the lua/autorun are loaded. There is no compiling process required.

I'm naming the functions and libraries similar to how they are named in Garry's mod. I'm doing this so that it's familiar to people, and because it's overall a good naming convention.

There will obviously be some differences. I can't put in every gmod function, but on the other hand there will be some functions that aren't in gmod.

Also, Lua is written as "Lua". It is not an acronym.
Reply
#9

Thanks DF for clearing things up, you too omega. I am going to have to teach my self Lua, I see there are good tutorials here for anyone else who might be interested. http://www.garrysmod.com/wiki/?title=Lua
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)