You are not logged in.

Applications: [GameMaster: OPEN] | [Volunteer Testers: OPEN]


This forum will be permanently shut down on Friday 13.07.2018
Please copy or save all important information from old forum before they will be deactivated
We have moved to new board. https://forum.runesofmagic.gameforge.com/Come join us.

Peryl

Intermediate

  • "Peryl" started this thread

Posts: 313

Location: Elsewhere

  • Send private message

1

Friday, February 3rd 2012, 12:55am

Creating Custom Functions

[SIZE=+2]Introduction[/SIZE]
Since the forums have been reworked, many older tutorials and guides have been removed. Some of these would have still been valid and the guide to creating custom functions for Runes of Magic originally posted by Sixpax is one of them. As Sixpax no longer plays or frequents the forums, he won't be available to repost it, nor would it be appropriate to grab it from the internet archive and repost without permission. Therefore, here's my own version of a guide on creating custom functions.


Please note that this is not a tutorial on programming in Lua, so I will be assuming some familiarity with the Lua language in general as well as some rudimentary knowledge of RoM specific functions. However, that is knowledge you'll only need when creating the functions themselves. If you want to learn Lua programming, your first stop should be here. If you want info on RoM specific functions, try The Runes of Magic Wiki.


[SIZE=+2]One Tool to Write it All,[/SIZE]
The only tool you are going to need for this is a good text editor. I recommend Notepad++ though any text editor will do. Though why not try Notepad++ anyway, it's free!


[SIZE=+2]One Tool to Find 'em.[/SIZE]
You will also need to know how to navigate and create folders, but that is something I assume you already know how to do.


[SIZE=+2]One Tool to Bring Them All...[/SIZE]
In order to get our custom function(s) into the game, we'll need to actually create a little add-on. All this add-on will do is load up our functions so that we can use them in-game, so nothing really terribly complex to do here.


First things first, navigate to the folder the game is installed (typically C:\Program Files\Runes of Magic) and make sure there is a folder there called Interface. If there isn't, create this folder. Inside the Interface folder, verify that there is a sub-folder called Addons (again, create it if it doesn't exist). Navigate into this sub-folder.


Now we must decide what to call our little add-on. For this guide, I'll call it CustomFuncs though you can give it some other name if you wish. With the name decided, create another sub-folder inside the Addons folder with the name of the add-on. For this example the folder to create will be called CustomFuncs. Navigate into this folder as well.


For RoM to recognize an add-on, it checks for any sub-folders in the Addons folder and looks inside these for the presence of a text file whose name is the same as the sub-folder and has a .toc file extension. If present, it will load this file which it uses as the list of filenames it needs to load up in order to activate the add-on. Since we are calling this add-on CustomFuncs we need to create a new text file called CustomFuncs.toc. So create one, and open it in a text editor.


Since CustomFuncs.toc is merely a list of files for RoM to load, we merely need to list the files we want, one file per line. We can also add comments to this file by starting a line with two hash marks (##). Let's call our file MyFuncs.lua and add a nice little header to the file so that we know what this is about in the future. Add the following as the contents for CustomFuncs.toc:

Source code

1
2
3
4
5
6
## Name: CustomFuncs
## Description: Defines some custom functions for use in RoM.
## Author: Your Name Here


MyFuncs.lua

Save the file, making sure that it is saved as CustomFuncs.toc and not getting a .txt extension added to the name or anything.


[SIZE=+2]...And in the Darkness Bind 'em[/SIZE]
With the above done, we need to create the file MyFuncs.lua in this add-on's folder and open it in our text editor. This is where we will create the actual functions.


For this guide, I'll provide a function that I've used in the past and is quite handy for add-on developers. This function will dump the contents of objects (such as frames, textures, buttons) and any inherited sub-classes. Add the following to MyFuncs.lua:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- Some color definitions for DumpObject()
local VarTypeColors = { 
            ['number'] = "ff6080ff",
            ['boolean'] = "ff6080ff",
            ['string'] = "ffff8060",
            ['table'] = "ffffc040",
            ['function'] = "ff80ff60",
            ['userdata'] = "ffa0a0a0",
            ['thread'] = "ffa040a0",
            ['nil'] = "ffff2020",
        };


--[[ DumpObject ]]--
-- Scans and dumps contents of an object, also scans metatables of objects.
-- Cannot scan objects with protected metatables.
function DumpObject(object)
    if not object then
        DEFAULT_CHAT_FRAME:AddMessage("No object given.");
    elseif type(object) == "table" then
        local o = object;
        local lvl = 0;
        local t = "";
        
        while o do
            for k,v in pairs(o) do
                DEFAULT_CHAT_FRAME:AddMessage(t..k.."  (|c"..VarTypeColors[type(v)]..type(v).."|r)", 1, 1, 1)
            end
            
            lvl = lvl+1
            
            o = getmetatable(o)
            if o then
                DEFAULT_CHAT_FRAME:AddMessage(t..":", 0.6, 0.6, 0.6)
                DEFAULT_CHAT_FRAME:AddMessage(t.."+ Inherits "..lvl..":", 0.6, 0.6, 0.6)
            end


            t = t.."  "
        end
        DEFAULT_CHAT_FRAME:AddMessage("---------")
    else
        DEFAULT_CHAT_FRAME:AddMessage("object not a table")
    end
end

Save the file and once again, make sure that it is saved as MyFuncs.lua and not adding a .txt or anything.


[SIZE=+2]In the Land of Mor-...erm Taborea,[/SIZE]
So now everything is ready. Start the game up and if everything loads correctly (as in no errors in our add-on code), we should now be able to use our brand spanking new DumpObject function.


Try typing this in the chat edit box:

Source code

1
/run DumpObject(MainActionBarFrame)



This should dump the contents of MainActionBarFrame (the frame that holds the main action bar).


[SIZE=+2]Where the Shadows Lie.[/SIZE]
That's pretty much it. If you want to add more functions simply add them to MyFuncs.lua and you are ready to go.


Another thing you can use this for is to define your own slash commands for even greater convenience. I won't bother showing how to do this here since I already mention how to do this in my New Macro Guide here.
2013... The year from hell....

2

Saturday, February 4th 2012, 2:36pm

Wow, thanks for your guides!
This is also a useful page for develop.
http://maurits.tv/data/garrysmod/wiki/wi…e=Lua_Libraries

3

Friday, February 24th 2012, 10:05pm

Try this:
function print(msg) SELECTED_CHAT_FRAME:AddMessage(tostring(msg)) end

Instead of repeating DEFAULT_CHAT_FRAME:AddMessage. Also you can implement function print(args)

4

Monday, July 23rd 2012, 1:32am

Can you help with the coding to add a wait a certain amount of time please?

Peryl

Intermediate

  • "Peryl" started this thread

Posts: 313

Location: Elsewhere

  • Send private message

5

Monday, July 23rd 2012, 1:38am

Quoted from "bilalab;547717"

Can you help with the coding to add a wait a certain amount of time please?

How do you mean. You want a function that can wait a period of time, a function to start after a period of time, or something else?
2013... The year from hell....

6

Tuesday, July 24th 2012, 9:19pm

i want a function that can stop the current running macro for a period of time and then resume it, similar to the /wait command in-game

Peryl

Intermediate

  • "Peryl" started this thread

Posts: 313

Location: Elsewhere

  • Send private message

7

Wednesday, July 25th 2012, 2:41am

No can do. Macros are run from within the game.

You could create an add-on that effectively runs macro code via ExecuteMacroLine(), but you would lose the use of /cast and /use once you add the timing to it.

Anyway, if you are still interested in doing timing stuff, check out LibTimers.
2013... The year from hell....

8

Thursday, July 26th 2012, 2:18pm

thanks for the info :)