Lua Plugin Howto

From HLSW Wiki

Jump to: navigation, search

How to write a HLSW Lua Extension

This howto show you a simple HLSW Lua Extension to read serverinfos and display it in some controls

Step 1

Create a new lua file at "HLSW/Plugins" folder and add this lines to it (perhaps sample.lua)

-- Title: "Your Title"
-- Description: "Your Description"
-- Interface: 1000
-- Type: xy
-- Version: X.X.X.X
-- Author: "My Name"
-- URL: yourwebsite.com

Image:Plugin1.png

Step 2

Now we need some controls to display our infos and a view which we can add to the rconsection. In our case we use some simple LuaLabel controls and a LuaView control and initilize them.

Define some controls. Here we use two labels and one view control

HostnameLabel = LuaLabel()
MapLabel = LuaLabel()
MyRconView = LuaView()

Now we can catch two events. The first event is EVT_APP_RCON_INIT. This is called on creation of the rcon section, here we can create our own rcon view and the controls what we need. In our example we create a rcon view called "MyRconView" you can see it on the screenshot and it to the rcon section tab control with Window.AddRconView. The next step is to create the label controls on the view. You need the coordinates and the view on which be added.

Ok, now we need a second Event, which is called EVT_SERVER_CHANGED to show the serverdetails. This event is called when the user change the active server. Then we get the current selected server with the global function Server.GetCurrent which return a server object. The last to lines of the event code are only to set the window text of the both controls with the hostname and the current map.

function OnEvent(event)
  if(event == Events.EVT_APP_RCON_INIT) then
    MyRconView:Create("MyRconView")
    Window.AddRconView("MyRconView", MyRconView)

    HostnameLabel:Create(MyRconView, {5, 5, 200, 15})
    MapLabel:Create(MyRconView, {5, 20, 200, 35})
  elseif(event == Events.EVT_SERVER_CHANGED) then
    srvobj = Server.GetCurrent()
    HostnameLabel:SetText(srvobj:GetHostName())
    MapLabel:SetText(srvobj:GetMapName())
  end
end

Image:Plugin2.png

Personal tools
Developer