MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 1 | SYNOPSIS |
Zesstra | d59c389 | 2019-11-28 20:53:39 +0100 | [diff] [blame] | 2 | unknown call_other(object ob, string fun, mixed arg, ...) |
| 3 | unknown call_other(object *ob, string fun, mixed arg, ...) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 4 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 5 | ob->fun (mixed arg, ...) |
| 6 | ob->"fun" (mixed arg, ...) |
| 7 | ob->(fun) (mixed arg, ...) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 8 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 9 | DESCRIPTION |
| 10 | Call a member function <fun> in another object <ob> with an |
| 11 | the argument(s) <arg...>. Result is the value returned from |
| 12 | the called function (or 0 for non-existing or void functions). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 13 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 14 | Additionally the efun accepts an array of objects as <ob>: the |
| 15 | function is called with the same arguments in all the given objects. |
| 16 | The single results are collected in an array and yield the final |
| 17 | result. Array elements can be objects or the names of existing |
| 18 | objects; destructed objects and 0s will yield a '0' as result, but |
| 19 | don't cause an error. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 20 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 21 | The object(s) can be given directly or via a string (i.e. its |
| 22 | object_name). If it is given by a string and the object does not |
| 23 | exist yet, it will be loaded. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 24 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 25 | ob->fun(args) and "ob_name"->fun(args) is equivalent to |
| 26 | call_other(ob, "fun", args). Nowadays the ob_name string can |
| 27 | also be a variable. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 28 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 29 | ob->fun(args) and ob->"fun"(args) are equivalent to |
| 30 | call_other(ob, "fun", args). ob->(fun)(args) are equivalent |
| 31 | to call_other(ob, fun, args) where fun is a runtime expression |
| 32 | returning the function name. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 33 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 34 | If ob::fun does not define a publicly accessible function, the |
| 35 | efun will call the H_DEFAULT_METHOD hook if set. If the hook |
| 36 | is not set or can't resolve the call either, call_other() |
| 37 | will return 0, which is indistinguishable from a function returning 0. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 38 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 39 | Calls to the master object never use the H_DEFAULT_METHOD hook. |
| 40 | To force non-default calls, the efun call_direct() can be used. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 41 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 42 | "publicly accessible" means "public" when calling other objects, |
| 43 | and "public" or "static" when calling this_object(). "private" |
| 44 | and "protected" function can never be called with call_other(). |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 45 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 46 | The return type of call_other() is 'any' by default. However, |
| 47 | if your LPC code uses #pragma strict_types, the return type is |
| 48 | 'unknown', and the result of call_other() must be casted to |
| 49 | the appropriate type before you can use it for anything. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 50 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 51 | EXAMPLES |
| 52 | // All the following statements call the lfun QueryProp() |
| 53 | // in the current player with the argument P_SHORT. |
| 54 | string str, fun; |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 55 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 56 | str = (string)call_other(this_player(), "QueryProp", P_SHORT); |
| 57 | fun = "QueryProp"; |
| 58 | str = (string)call_other(this_player(), fun, P_SHORT); |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 59 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 60 | str = (string)this_player()->QueryProp(P_SHORT); |
| 61 | str = (string)this_player()->"QueryProp"(P_SHORT); |
| 62 | fun = "QueryProp"; |
| 63 | str = (string)this_player()->(fun)(P_SHORT); |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 64 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 65 | You have to do explicit type casting because of the unknown |
| 66 | return type, if you have set #pragma strict_types. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 67 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 68 | // This statement calls the lfun short() in all interactive users |
| 69 | // and stores the collected results in a variable. |
| 70 | string *s; |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 71 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame^] | 72 | s = (string *)users()->short(); |
| 73 | |
| 74 | !Compat: call_other("/users/luser/thing", "???", 0); |
| 75 | Compat: call_other("users/luser/thing", "???", 0); |
| 76 | |
| 77 | This looks a bit weird but it was used very often to just load |
| 78 | the object by calling a not existing function like "???". |
| 79 | Fortunately nowadays there is an efun load_object() for this |
| 80 | purpose. |
| 81 | |
| 82 | HISTORY |
| 83 | In LDMud 3.2.8 the following improvements were made: |
| 84 | - the forms x->"y"() and x->(y)() are recognized; |
| 85 | - the form x->y() no longer clashes with a local variable also |
| 86 | called "y"; |
| 87 | - a simul_efun call_other() also catches ->() calls. |
| 88 | - call_other can be applied on arrays of objects. |
| 89 | LDMud 3.2.10 made the call on arrays of objects configurable. |
| 90 | LDMud 3.3.113 introduced the H_DEFAULT_METHOD hook. |
| 91 | LDMud 3.5.0 made the call on arrays of objects non-optional. |
| 92 | |
| 93 | SEE ALSO |
| 94 | call_direct(E), call_strict(E), call_direct_strict(E), |
| 95 | call_resolved(E), call_direct_resolved(E), create(A), pragma(LPC), |
| 96 | extern_call(E), function_exists(E), functions(LPC), map_objects(E) |