Author Topic: string macros  (Read 2352 times)

Offline Nulvect

  • BFF
  • ***
  • Posts: 127
    • View Profile
string macros
« on: March 06, 2008, 10:14:01 pm »
This function can be used to make your own string macros:

Code: [Select]
varargs string expandos(string str, object tgt) {
  string del = "$";
  mapping macros = ([
    "N" : (: this_player() ? this_player()->query_cap_name() : "" :),
    "T" : (: $(tgt) ? $(tgt)->query_cap_name() : "" :),
    "SHORT" : (: query_short() :),
  ]);

  // always double check the input
  if (nullp(str)) return "";
  if (nullp(tgt)) tgt = 0;

  foreach (string mac, mixed f in macros)
    str = replace_string(str, del+mac, evaluate(f));

  return str;
}

This is very useful for setting up new lib objects with customizable strings. The example includes an optional parameter, this is not really needed if you set your objects up correctly. Any variables used inside a functional (that you don't declare in that functional) must be in the form $(varname). Possible uses for this:

Code: [Select]
set_long(expandos("A very small $SHORT."));

set_emotes(3, ({ expandos("$N growls.") }), 1);

message("combat", expandos("$N smacks $T across the face!"), environment(this_player()), ({ this_player(), tgt }) );

I know some libs have a built in form of this, but not all do. I hope someone finds this useful.
I release this into the public domain blah blah etc.

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1024
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Re: string macros
« Reply #1 on: March 06, 2008, 11:41:39 pm »
Quote
I release this into the public domain blah blah etc.

:)

Thank you.

-Crat