Welcome to the PtokaX Wiki!


This wiki is devoted to let users share information on the great hubsoft PtokaX. However, it is not limited to PtokaX, it is destined to share and discuss information on different parts of the LUA programming language, but the site is NOT intended to be a replacement for the primary LUA Board aka the PtokaX Portal, nor the secondary LUA Board or the PtokaX resources.

If you’re not familiar with wikis then in short: they are user-contributed sites: pages can be edited by (almost) anyone. As such, there is always kind of up-to-date and (hopefully) proper content. They are better than forums, since relevant pieces of information are on the same page. Do not hesitate to share your knowledge, and we all hope you can learn and teach a lot here! — bastya_elvtars



API 2 Script Examples

--[[
 A brief example script by Mutor...
 
	Example code within:
	- Creating and using a table
	- Registering a bot
	- Setting paths and loading a file
	- Adding and removing timers
	- Using the global OnError function for report messages
 
 Sometimes we make a table of configuration variables.
 This allows us to set commonly used strings, numbers or boolean values
 In this way we may make fewer calls to the API and thus save on memory and CPU usage
]]--
Tab = {
	-- "Botname" ["" = hub bot]
	Bot = "[PxDev]",
	-- Register Bot? [show on user list] true/false
	BotReg = true,
	-- "Description for Bot" ["" = Script Name]
	BotDesc = "",
	-- Email Address for Bot ["" = None]
	BotMail = "",
	-- Should Bot have a key? true/false
	BotKey = true,
	--"Command Menu" ["" = hub name]
	Menu = "",
	--"Command SubMenu" ["" = script name]
	SubMenu = "",
	-- Admins nick for status / error messages, Set this to your nick
	OpNick = "Mutor",
	-- Message file path/name
	File = "test.dat",
}
 
 
OnStartup = function()
	-- Set another variable to the table above, a name for the script
	Tab.Scp = "Test Script 1.0"
	-- If this string from the table above is emtpy, we will set the script bot's name to that of the hub bot
	if Tab.Bot == "" then Tab.Bot = SetMan.GetString(21) end
	-- If this string from the table above is emtpy, we will set the script bot's description to script's name
	if Tab.BotDesc == "" then Tab.BotDesc = Tab.Scp end
	-- Should we register the bot?
	if Tab.BotReg then
		-- The value from the configuration table is true.
		-- If the botname is not equal to the hub bot's name let's reg the bot, using the parameters from the table
		if Tab.Bot ~= SetMan.GetString(21) then Core.RegBot(Tab.Bot,Tab.BotDesc,Tab.BotMail,Tab.BotKey) end
	end
	-- Given the changes to the path call and the porting to other OS's...
	-- When working with saving data to file let's ensure we have the correct and absolute path.
	-- I want to save to the script directory, so I'll set the Path variable to the root of Ptokax and ammend "scripts/"
	local Path = Core.GetPtokaXPath().."scripts/"
	-- Now to check if the path to file is complete, if not set it as such
	if not Tab.File:find("^"..Path,1,true) then Tab.File = Path..Tab.File end
	-- Is the code executable? [table, or Lua code etc.] We can use the loadfile statement for check this.
	-- If it is we will load it, if not we will use the OnError function to fire an error message
	if loadfile(Tab.File) then dofile(Tab.File) else OnError(Tab.File.." could not be loaded.") end
	-- Do we need a timer in this script? For example sake, we do :P
	-- Add a timer with a given interval in ms. [3000 = 3 seconds] An Id is returned from this function.
	-- The global Tmr var is assigend that Id. Written as such the timer will call the OnTimer function at interval
	Tmr1 = TmrMan.AddTimer(3000)
	-- We can aslo specify a certain function for the timer. We'll do so. Now this timer will call the Hello function.
	Tmr2 = TmrMan.AddTimer(5000,"Hello")
end
 
-- Script errors shall be sent to this function. We can also call it 'manually'.
OnError = function(msg)
	-- We intend to send these messages to the OpNick listed in the table above.
	-- It would be foolish to try and send the message if the OpNick is offline, let's check first.
	local user = Core.GetUser(Tab.OpNick)
	-- If OpNick is online, send the message...
	if user then Core.SendToUser(user,"<"..Tab.Bot.."> "..msg.."|") end
end
 
OnTimer = function(Id)
	-- As we can use multiple timers in a script, it is wise ot compare the Id and execute the relative code.
	if Tmr1 and Id == Tmr1 then
		-- The Id is a match, well just send a quick message, the actual value if the Tmr1 Id
		-- As it is a number, we shall convert it to a string and send it...
		OnError(tostring(Tmr1))
		-- Ok we're done with the timer, let's remove it.
		TmrMan.RemoveTimer(Tmr1)
		-- Subsequentlly, set the Tmr1 global variable to nil
		Tmr1 = nil
		-- We shall now report the value of Tmr1, which now should be nil
		OnError("Tmr = "..tostring(Tmr1))
	end
end
 
-- This function is called by Tmr2
Hello = function()
	-- A simple message to confirm the timer's operation
	OnError("Hello World!")
	-- Done, remove the timer and set Tmr2 global variable to nil
	TmrMan.RemoveTimer(Tmr2)
	Tmr2 = nil
end
 
 
--[[
 This is what should be returned from the script
 
<[PxDev]> C:/PxDev/scripts/test.dat could not be loaded.
<[PxDev]> 25723
<[PxDev]> Tmr = nil
<[PxDev]> Hello World!
]]

Personal Tools