Author Topic: Moving a command to a daemon  (Read 2017 times)

Offline Maze of Ith

  • Acquaintance
  • *
  • Posts: 33
  • Sometimes nothing can be a really cool hand.
    • View Profile
Moving a command to a daemon
« on: June 04, 2013, 04:58:43 PM »
I wrote a command for changing the title and position of users, and it works fine, but when I move the function from the command to a daemon I receive the following error:
Code: [Select]
*Illegal type of index
Here is the code of the working function in the command:
Code: [Select]
#include <globals.h>

mapping accts = ([]);
void change_title(string user, string title, int i) {
    string temp = capitalize(user);
    if(file_exists(ACCT_DATA) {
        restore_object(ACCT_DATA);
    }

    accts[temp]["position"] = i;
    accts[temp]["title"] = title;
    save_object(ACCT_DATA);
}

The above code works, but when I move the function to my daemon, and pass the daemon the same values, I receive the above error. I am sure I am missing something small, but I am not sure at this point. Any ideas?

Thanks!
zed @ looney2.com 8888
zed <at> lilypadmudlib <dot> com

Offline quixadhal

  • BFF
  • ***
  • Posts: 618
    • View Profile
    • A Waste of Time
Re: Moving a command to a daemon
« Reply #1 on: June 04, 2013, 05:54:34 PM »
Are you declaring the GLOBAL variable you're using there, in your daemon?

Offline Nulvect

  • BFF
  • ***
  • Posts: 127
    • View Profile
Re: Moving a command to a daemon
« Reply #2 on: June 04, 2013, 08:28:11 PM »
I bet your daemon calls restore_object(). That function often zeroes out existing variables, even if you use the optional arg to tell it not to do that. So your mapping winds up as 0. Have your function check for this and fix it before assignment.

Offline Maze of Ith

  • Acquaintance
  • *
  • Posts: 33
  • Sometimes nothing can be a really cool hand.
    • View Profile
Re: Moving a command to a daemon
« Reply #3 on: June 05, 2013, 10:00:49 AM »
Are you declaring the GLOBAL variable you're using there, in your daemon?

Yessir. The same code in the command works fine, which is why I am a little baffled.

I bet your daemon calls restore_object(). That function often zeroes out existing variables, even if you use the optional arg to tell it not to do that. So your mapping winds up as 0. Have your function check for this and fix it before assignment.

Thanks, Nulvect - I will add a few things for checking and see what I come up with.
zed @ looney2.com 8888
zed <at> lilypadmudlib <dot> com

Offline Maze of Ith

  • Acquaintance
  • *
  • Posts: 33
  • Sometimes nothing can be a really cool hand.
    • View Profile
Re: Moving a command to a daemon
« Reply #4 on: June 05, 2013, 03:11:16 PM »
Here is the final code that is now working IN THE DAEMON.  :)

Thanks for pointing me in the right direction!

Code: [Select]
void change_title(string user, string ntitle, int i) {
    string temp = capitalize(user);
    if(file_exists(ACCT_DATA)) {
        restore_object(ACCT_DATA);
    }
    if(sizeof(accts) > 0) {
        write("Good data. accts mapping is not empty, continuing...\n");
        accts[temp]["position"] = i;
        accts[temp]["title"] = capitalize(ntitle);
        save_object(ACCT_DATA);
    }
        else {
        write("ERROR! Empty mapping!\nAww, shucks...\n");
return;
        }
}

Ith\Zed
zed @ looney2.com 8888
zed <at> lilypadmudlib <dot> com

Offline quixadhal

  • BFF
  • ***
  • Posts: 618
    • View Profile
    • A Waste of Time
Re: Moving a command to a daemon
« Reply #5 on: June 06, 2013, 02:04:43 AM »
Rather than error out, you should just initialize the mapping.

After calling restore_object(), just check the mapping there.
Code: [Select]
if(file_exists(ACCT_DATA)) {
    restore_object(ACCT_DATA);
}
if(!accts) {
    accts = ([]);
}
if(!accts[temp]) {
    accts[temp] = ([]);
}
accts[temp]["position"] = i;
accts[temp]["title"] = capitalize(ntitle);
save_object(ACCT_DATA);

Offline Maze of Ith

  • Acquaintance
  • *
  • Posts: 33
  • Sometimes nothing can be a really cool hand.
    • View Profile
Re: Moving a command to a daemon
« Reply #6 on: June 08, 2013, 12:03:34 AM »
That would overwrite the data of the existing file if there was an error loading the file and yet it existed though, right?
zed @ looney2.com 8888
zed <at> lilypadmudlib <dot> com

Offline quixadhal

  • BFF
  • ***
  • Posts: 618
    • View Profile
    • A Waste of Time
Re: Moving a command to a daemon
« Reply #7 on: June 08, 2013, 03:06:00 AM »
If there's an error loading the file, you're already in the state of needing to dig for backups.  The only reason it wouldn't load (if it existed) would be user error... IE: you edited it by hand, or you messed with the file permissions by hand.

Offline Maze of Ith

  • Acquaintance
  • *
  • Posts: 33
  • Sometimes nothing can be a really cool hand.
    • View Profile
Re: Moving a command to a daemon
« Reply #8 on: June 10, 2013, 09:57:37 AM »
Ah, yes. That makes perfect sense. Thank you - I am going to add a function that creates the bones of the mapping then if it is unable to load. Cheers!
zed @ looney2.com 8888
zed <at> lilypadmudlib <dot> com