1 (edited by gerichhome 2010-02-12 23:06:46)

Topic: [SUGGESTION]def,undef,undefall,dump_defs commands in console

Hi all! I've written 4 new commands for tee console:

def ssr
undef s
undefall
dump_all

command def allows you define a new command for console.
Syntax:

def command_name "Help message" "action"

Example:

def hook_fire "Hooks and fires at one time" "+hook;+fire"

then you can use them just like any command:

bind p hook_fire

Defined commands are saved to config file on exit and aer loaded on console init.
They are showed in command list with help just like any other command.

You can undefine defined commands using undef command:

undef hook_fire

Or undefine all commands using

undefall

You can watch list of defines:

dump_defs

Defined commands are protected from recursive calls:

def recurse "Trying to raise an error" recurse
recurse

will not raise an error. It'll write:

Recursive calls are prohibited!

I suggest to include this function in new version.
P.S. Have I got rights to upload files to svn of Teeworlds?
P.P.S. In archive there is a patch to 0.5.x version and 2 new files, please check if all is ok(it's my first patch)

Download patch & 2 files

2 (edited by gerichhome 2010-02-12 23:19:33)

Re: [SUGGESTION]def,undef,undefall,dump_defs commands in console

Hm, this patch allows you to make so "trigger" buttons:

def mode1 "Switch to mode 1" "bind p mode2;bind mouse1 \"+fire;emote 2\""
def mode2 "Switch to mode 2" "bind p mode1;bind mouse1 +fire"
mode1

this will make P button to be a trigger for mode with auto-emote on fire and just fire

3

Re: [SUGGESTION]def,undef,undefall,dump_defs commands in console

gerichhome wrote:

Defined commands are protected from recursive calls:

def recurse "Trying to raise an error" recurse
recurse

will not raise an error. It'll write:

Recursive calls are prohibited!

What about circular recursions? For example:

def recurse1 ... recurse2
def recurse2 ... recurse1
aka cheesy

4

Re: [SUGGESTION]def,undef,undefall,dump_defs commands in console

I was not able to take a look at it yet, because you uploaded it on rapidshare. Please upload it smewhere else, as rapidshare only allows very restricted downloads to non premium mebers, and is overstrained at the moment. However, your patch won't get implemented anyway, if it is written for 0.5.x sources, as this is not the version teeworlds is developed from, currently. If you want supply patches for the next release, adjust them to the sources in the refactor branch(which is going to be the next release). And of course you don't have the rights to upload on the svn repository, if everyone had, guess what shit some people might do... hmm. Finally I'm not really sure if this is needed, because your "trigger buttons" which you described above, are almost the same that can be achieved using the exec commad.

Example:

mode_1.cfg:

bind p exec "mode_2.cfg";bind mouse1 "+fire;emote 2"

mode_2.cfg:

bind p exec "mode_1.cfg";bind mouse1 +fire

and in console:

exec mode_1.cfg

This will result in exactly the same functionality, as you described it.

About the general thing: I don't think it is neccessary, the only thing you have in advantage to using normal binds is that you have a description. As you probably would define your own actions, you should know what they do anyway(and if you bind them, you don't see the description anymore, so no need for it).

Also I believe, like grummi, that recursions are possible, the way he described it. I don't think there is a way to get around it here.

All in all my opinion is, it should not be included.

5 (edited by gerichhome 2010-02-13 21:23:41)

Re: [SUGGESTION]def,undef,undefall,dump_defs commands in console

grummi wrote:
gerichhome wrote:

Defined commands are protected from recursive calls:

def recurse "Trying to raise an error" recurse
recurse

will not raise an error. It'll write:

Recursive calls are prohibited!

What about circular recursions? For example:

def recurse1 ... recurse2
def recurse2 ... recurse1
ghost91 wrote:

Also I believe, like grummi, that recursions are possible, the way he described it. I don't think there is a way to get around it here.

Circular recursions are recognized too, it's simple, look:

#define CALL_STACK_SIZE 50
static const char* call_stack[CALL_STACK_SIZE];
static int call_stack_len = 0;
...
//is called when you call your defined command,
//user_data
void CONSOLE_COMMAND::con_do_console_command(void *result, void *user_data)
{
    if(call_stack_len==CALL_STACK_SIZE)
    {
        dbg_msg("console_command", "Call stack overflow!");
        return;
    }

    const char *command = (char *)user_data;
    for (int i=0; i<call_stack_len; i++)
    {
        if(str_comp_nocase(call_stack[i], command) == 0)
        {
            dbg_msg("console_command", "Recursive calls are prohibited!");
            return;
        }
    }
    call_stack[call_stack_len++] = command;
    dbg_msg("console_command", "Executing console command %s", command);
    console_execute_line(command);
    call_stack_len--;
}

To ghost91: I didn't know about your example, but I needed to make trigger=) So, I've written this code.
I won't be upset it won't be included in next release. I've made it for my needs)

P.S. I'll add new link later
P.P.S. code above contains a little not serious error, I'll fix it

6

Re: [SUGGESTION]def,undef,undefall,dump_defs commands in console

So, I've written code for latest revision in svn(2222).
Patch: http://dl.dropbox.com/u/1691759/console_command.patch

I've also found a bug with echo command in this revision.. it just fails when you call echo.