visual_script_func_nodes.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  1. /*************************************************************************/
  2. /* visual_script_func_nodes.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_script_func_nodes.h"
  31. #include "engine.h"
  32. #include "io/resource_loader.h"
  33. #include "os/os.h"
  34. #include "scene/main/node.h"
  35. #include "scene/main/scene_tree.h"
  36. #include "visual_script_nodes.h"
  37. //////////////////////////////////////////
  38. ////////////////CALL//////////////////////
  39. //////////////////////////////////////////
  40. int VisualScriptFunctionCall::get_output_sequence_port_count() const {
  41. if ((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_method_const(basic_type, function)))
  42. return 0;
  43. else
  44. return 1;
  45. }
  46. bool VisualScriptFunctionCall::has_input_sequence_port() const {
  47. if ((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_method_const(basic_type, function)))
  48. return false;
  49. else
  50. return true;
  51. }
  52. #ifdef TOOLS_ENABLED
  53. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  54. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  55. return NULL;
  56. Ref<Script> scr = p_current_node->get_script();
  57. if (scr.is_valid() && scr == script)
  58. return p_current_node;
  59. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  60. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  61. if (n)
  62. return n;
  63. }
  64. return NULL;
  65. }
  66. #endif
  67. Node *VisualScriptFunctionCall::_get_base_node() const {
  68. #ifdef TOOLS_ENABLED
  69. Ref<Script> script = get_visual_script();
  70. if (!script.is_valid())
  71. return NULL;
  72. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  73. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  74. if (!scene_tree)
  75. return NULL;
  76. Node *edited_scene = scene_tree->get_edited_scene_root();
  77. if (!edited_scene)
  78. return NULL;
  79. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  80. if (!script_node)
  81. return NULL;
  82. if (!script_node->has_node(base_path))
  83. return NULL;
  84. Node *path_to = script_node->get_node(base_path);
  85. return path_to;
  86. #else
  87. return NULL;
  88. #endif
  89. }
  90. StringName VisualScriptFunctionCall::_get_base_type() const {
  91. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid())
  92. return get_visual_script()->get_instance_base_type();
  93. else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  94. Node *path = _get_base_node();
  95. if (path)
  96. return path->get_class();
  97. }
  98. return base_type;
  99. }
  100. int VisualScriptFunctionCall::get_input_value_port_count() const {
  101. if (call_mode == CALL_MODE_BASIC_TYPE) {
  102. Vector<StringName> names = Variant::get_method_argument_names(basic_type, function);
  103. return names.size() + (rpc_call_mode >= RPC_RELIABLE_TO_ID ? 1 : 0) + 1;
  104. } else {
  105. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  106. if (mb) {
  107. return mb->get_argument_count() + (call_mode == CALL_MODE_INSTANCE ? 1 : 0) + (rpc_call_mode >= RPC_RELIABLE_TO_ID ? 1 : 0) - use_default_args;
  108. }
  109. return method_cache.arguments.size() + (call_mode == CALL_MODE_INSTANCE ? 1 : 0) + (rpc_call_mode >= RPC_RELIABLE_TO_ID ? 1 : 0) - use_default_args;
  110. }
  111. }
  112. int VisualScriptFunctionCall::get_output_value_port_count() const {
  113. if (call_mode == CALL_MODE_BASIC_TYPE) {
  114. bool returns = false;
  115. Variant::get_method_return_type(basic_type, function, &returns);
  116. return returns ? 1 : 0;
  117. } else {
  118. int ret;
  119. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  120. if (mb) {
  121. ret = mb->has_return() ? 1 : 0;
  122. } else
  123. ret = 1; //it is assumed that script always returns something
  124. if (call_mode == CALL_MODE_INSTANCE) {
  125. ret++;
  126. }
  127. return ret;
  128. }
  129. }
  130. String VisualScriptFunctionCall::get_output_sequence_port_text(int p_port) const {
  131. return String();
  132. }
  133. PropertyInfo VisualScriptFunctionCall::get_input_value_port_info(int p_idx) const {
  134. if (call_mode == CALL_MODE_INSTANCE || call_mode == CALL_MODE_BASIC_TYPE) {
  135. if (p_idx == 0) {
  136. PropertyInfo pi;
  137. pi.type = (call_mode == CALL_MODE_INSTANCE ? Variant::OBJECT : basic_type);
  138. pi.name = (call_mode == CALL_MODE_INSTANCE ? String("instance") : Variant::get_type_name(basic_type).to_lower());
  139. return pi;
  140. } else {
  141. p_idx--;
  142. }
  143. }
  144. if (rpc_call_mode >= RPC_RELIABLE_TO_ID) {
  145. if (p_idx == 0) {
  146. return PropertyInfo(Variant::INT, "peer_id");
  147. } else {
  148. p_idx--;
  149. }
  150. }
  151. #ifdef DEBUG_METHODS_ENABLED
  152. if (call_mode == CALL_MODE_BASIC_TYPE) {
  153. Vector<StringName> names = Variant::get_method_argument_names(basic_type, function);
  154. Vector<Variant::Type> types = Variant::get_method_argument_types(basic_type, function);
  155. return PropertyInfo(types[p_idx], names[p_idx]);
  156. } else {
  157. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  158. if (mb) {
  159. return mb->get_argument_info(p_idx);
  160. }
  161. if (p_idx >= 0 && p_idx < method_cache.arguments.size()) {
  162. return method_cache.arguments[p_idx];
  163. }
  164. return PropertyInfo();
  165. }
  166. #else
  167. return PropertyInfo();
  168. #endif
  169. }
  170. PropertyInfo VisualScriptFunctionCall::get_output_value_port_info(int p_idx) const {
  171. #ifdef DEBUG_METHODS_ENABLED
  172. if (call_mode == CALL_MODE_BASIC_TYPE) {
  173. return PropertyInfo(Variant::get_method_return_type(basic_type, function), "");
  174. } else {
  175. if (call_mode == CALL_MODE_INSTANCE) {
  176. if (p_idx == 0) {
  177. return PropertyInfo(Variant::OBJECT, "pass", PROPERTY_HINT_TYPE_STRING, get_base_type());
  178. } else {
  179. p_idx--;
  180. }
  181. }
  182. PropertyInfo ret;
  183. /*MethodBind *mb = ClassDB::get_method(_get_base_type(),function);
  184. if (mb) {
  185. ret = mb->get_argument_info(-1);
  186. } else {*/
  187. ret = method_cache.return_val;
  188. //}
  189. if (call_mode == CALL_MODE_INSTANCE) {
  190. ret.name = "return";
  191. } else {
  192. ret.name = "";
  193. }
  194. return ret;
  195. }
  196. #else
  197. return PropertyInfo();
  198. #endif
  199. }
  200. String VisualScriptFunctionCall::get_caption() const {
  201. if (call_mode == CALL_MODE_SELF)
  202. return " " + String(function) + "()";
  203. if (call_mode == CALL_MODE_SINGLETON)
  204. return String(singleton) + ":" + String(function) + "()";
  205. else if (call_mode == CALL_MODE_BASIC_TYPE)
  206. return Variant::get_type_name(basic_type) + "." + String(function) + "()";
  207. else if (call_mode == CALL_MODE_NODE_PATH)
  208. return " [" + String(base_path.simplified()) + "]." + String(function) + "()";
  209. else
  210. return " " + base_type + "." + String(function) + "()";
  211. }
  212. String VisualScriptFunctionCall::get_text() const {
  213. if (rpc_call_mode) {
  214. return "RPC";
  215. }
  216. return "";
  217. }
  218. void VisualScriptFunctionCall::set_basic_type(Variant::Type p_type) {
  219. if (basic_type == p_type)
  220. return;
  221. basic_type = p_type;
  222. _change_notify();
  223. ports_changed_notify();
  224. }
  225. Variant::Type VisualScriptFunctionCall::get_basic_type() const {
  226. return basic_type;
  227. }
  228. void VisualScriptFunctionCall::set_base_type(const StringName &p_type) {
  229. if (base_type == p_type)
  230. return;
  231. base_type = p_type;
  232. _change_notify();
  233. ports_changed_notify();
  234. }
  235. StringName VisualScriptFunctionCall::get_base_type() const {
  236. return base_type;
  237. }
  238. void VisualScriptFunctionCall::set_base_script(const String &p_path) {
  239. if (base_script == p_path)
  240. return;
  241. base_script = p_path;
  242. _change_notify();
  243. ports_changed_notify();
  244. }
  245. String VisualScriptFunctionCall::get_base_script() const {
  246. return base_script;
  247. }
  248. void VisualScriptFunctionCall::set_singleton(const StringName &p_type) {
  249. if (singleton == p_type)
  250. return;
  251. singleton = p_type;
  252. Object *obj = Engine::get_singleton()->get_singleton_object(singleton);
  253. if (obj) {
  254. base_type = obj->get_class();
  255. }
  256. _change_notify();
  257. ports_changed_notify();
  258. }
  259. StringName VisualScriptFunctionCall::get_singleton() const {
  260. return singleton;
  261. }
  262. void VisualScriptFunctionCall::_update_method_cache() {
  263. StringName type;
  264. Ref<Script> script;
  265. if (call_mode == CALL_MODE_NODE_PATH) {
  266. Node *node = _get_base_node();
  267. if (node) {
  268. type = node->get_class();
  269. base_type = type; //cache, too
  270. script = node->get_script();
  271. }
  272. } else if (call_mode == CALL_MODE_SELF) {
  273. if (get_visual_script().is_valid()) {
  274. type = get_visual_script()->get_instance_base_type();
  275. base_type = type; //cache, too
  276. script = get_visual_script();
  277. }
  278. } else if (call_mode == CALL_MODE_SINGLETON) {
  279. Object *obj = Engine::get_singleton()->get_singleton_object(singleton);
  280. if (obj) {
  281. type = obj->get_class();
  282. script = obj->get_script();
  283. }
  284. } else if (call_mode == CALL_MODE_INSTANCE) {
  285. type = base_type;
  286. if (base_script != String()) {
  287. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  288. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  289. }
  290. if (ResourceCache::has(base_script)) {
  291. script = Ref<Resource>(ResourceCache::get(base_script));
  292. } else {
  293. return;
  294. }
  295. }
  296. }
  297. //print_line("BASE: "+String(type)+" FUNC: "+String(function));
  298. MethodBind *mb = ClassDB::get_method(type, function);
  299. if (mb) {
  300. use_default_args = mb->get_default_argument_count();
  301. method_cache = MethodInfo();
  302. for (int i = 0; i < mb->get_argument_count(); i++) {
  303. #ifdef DEBUG_METHODS_ENABLED
  304. method_cache.arguments.push_back(mb->get_argument_info(i));
  305. #else
  306. method_cache.arguments.push_back(PropertyInfo());
  307. #endif
  308. }
  309. if (mb->is_const()) {
  310. method_cache.flags |= METHOD_FLAG_CONST;
  311. }
  312. #ifdef DEBUG_METHODS_ENABLED
  313. method_cache.return_val = mb->get_return_info();
  314. #endif
  315. if (mb->is_vararg()) {
  316. //for vararg just give it 10 arguments (should be enough for most use cases)
  317. for (int i = 0; i < 10; i++) {
  318. method_cache.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i)));
  319. use_default_args++;
  320. }
  321. }
  322. } else if (script.is_valid() && script->has_method(function)) {
  323. method_cache = script->get_method_info(function);
  324. use_default_args = method_cache.default_arguments.size();
  325. }
  326. }
  327. void VisualScriptFunctionCall::set_function(const StringName &p_type) {
  328. if (function == p_type)
  329. return;
  330. function = p_type;
  331. if (call_mode == CALL_MODE_BASIC_TYPE) {
  332. use_default_args = Variant::get_method_default_arguments(basic_type, function).size();
  333. } else {
  334. //update all caches
  335. _update_method_cache();
  336. }
  337. _change_notify();
  338. ports_changed_notify();
  339. }
  340. StringName VisualScriptFunctionCall::get_function() const {
  341. return function;
  342. }
  343. void VisualScriptFunctionCall::set_base_path(const NodePath &p_type) {
  344. if (base_path == p_type)
  345. return;
  346. base_path = p_type;
  347. _change_notify();
  348. ports_changed_notify();
  349. }
  350. NodePath VisualScriptFunctionCall::get_base_path() const {
  351. return base_path;
  352. }
  353. void VisualScriptFunctionCall::set_call_mode(CallMode p_mode) {
  354. if (call_mode == p_mode)
  355. return;
  356. call_mode = p_mode;
  357. _change_notify();
  358. ports_changed_notify();
  359. }
  360. VisualScriptFunctionCall::CallMode VisualScriptFunctionCall::get_call_mode() const {
  361. return call_mode;
  362. }
  363. void VisualScriptFunctionCall::set_use_default_args(int p_amount) {
  364. if (use_default_args == p_amount)
  365. return;
  366. use_default_args = p_amount;
  367. ports_changed_notify();
  368. }
  369. void VisualScriptFunctionCall::set_rpc_call_mode(VisualScriptFunctionCall::RPCCallMode p_mode) {
  370. if (rpc_call_mode == p_mode)
  371. return;
  372. rpc_call_mode = p_mode;
  373. ports_changed_notify();
  374. _change_notify();
  375. }
  376. VisualScriptFunctionCall::RPCCallMode VisualScriptFunctionCall::get_rpc_call_mode() const {
  377. return rpc_call_mode;
  378. }
  379. int VisualScriptFunctionCall::get_use_default_args() const {
  380. return use_default_args;
  381. }
  382. void VisualScriptFunctionCall::set_validate(bool p_amount) {
  383. validate = p_amount;
  384. }
  385. bool VisualScriptFunctionCall::get_validate() const {
  386. return validate;
  387. }
  388. void VisualScriptFunctionCall::_set_argument_cache(const Dictionary &p_cache) {
  389. //so everything works in case all else fails
  390. method_cache = MethodInfo::from_dict(p_cache);
  391. }
  392. Dictionary VisualScriptFunctionCall::_get_argument_cache() const {
  393. return method_cache;
  394. }
  395. void VisualScriptFunctionCall::_validate_property(PropertyInfo &property) const {
  396. if (property.name == "base_type") {
  397. if (call_mode != CALL_MODE_INSTANCE) {
  398. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  399. }
  400. }
  401. if (property.name == "base_script") {
  402. if (call_mode != CALL_MODE_INSTANCE) {
  403. property.usage = 0;
  404. }
  405. }
  406. if (property.name == "basic_type") {
  407. if (call_mode != CALL_MODE_BASIC_TYPE) {
  408. property.usage = 0;
  409. }
  410. }
  411. if (property.name == "singleton") {
  412. if (call_mode != CALL_MODE_SINGLETON) {
  413. property.usage = 0;
  414. } else {
  415. List<Engine::Singleton> names;
  416. Engine::get_singleton()->get_singletons(&names);
  417. property.hint = PROPERTY_HINT_ENUM;
  418. String sl;
  419. for (List<Engine::Singleton>::Element *E = names.front(); E; E = E->next()) {
  420. if (sl != String())
  421. sl += ",";
  422. sl += E->get().name;
  423. }
  424. property.hint_string = sl;
  425. }
  426. }
  427. if (property.name == "node_path") {
  428. if (call_mode != CALL_MODE_NODE_PATH) {
  429. property.usage = 0;
  430. } else {
  431. Node *bnode = _get_base_node();
  432. if (bnode) {
  433. property.hint_string = bnode->get_path(); //convert to loong string
  434. } else {
  435. }
  436. }
  437. }
  438. if (property.name == "function") {
  439. if (call_mode == CALL_MODE_BASIC_TYPE) {
  440. property.hint = PROPERTY_HINT_METHOD_OF_VARIANT_TYPE;
  441. property.hint_string = Variant::get_type_name(basic_type);
  442. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  443. property.hint = PROPERTY_HINT_METHOD_OF_SCRIPT;
  444. property.hint_string = itos(get_visual_script()->get_instance_id());
  445. } else if (call_mode == CALL_MODE_SINGLETON) {
  446. Object *obj = Engine::get_singleton()->get_singleton_object(singleton);
  447. if (obj) {
  448. property.hint = PROPERTY_HINT_METHOD_OF_INSTANCE;
  449. property.hint_string = itos(obj->get_instance_id());
  450. } else {
  451. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  452. property.hint_string = base_type; //should be cached
  453. }
  454. } else if (call_mode == CALL_MODE_INSTANCE) {
  455. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  456. property.hint_string = base_type;
  457. if (base_script != String()) {
  458. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  459. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  460. }
  461. if (ResourceCache::has(base_script)) {
  462. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  463. if (script.is_valid()) {
  464. property.hint = PROPERTY_HINT_METHOD_OF_SCRIPT;
  465. property.hint_string = itos(script->get_instance_id());
  466. }
  467. }
  468. }
  469. } else if (call_mode == CALL_MODE_NODE_PATH) {
  470. Node *node = _get_base_node();
  471. if (node) {
  472. property.hint = PROPERTY_HINT_METHOD_OF_INSTANCE;
  473. property.hint_string = itos(node->get_instance_id());
  474. } else {
  475. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  476. property.hint_string = get_base_type();
  477. }
  478. }
  479. }
  480. if (property.name == "use_default_args") {
  481. property.hint = PROPERTY_HINT_RANGE;
  482. int mc = 0;
  483. if (call_mode == CALL_MODE_BASIC_TYPE) {
  484. mc = Variant::get_method_default_arguments(basic_type, function).size();
  485. } else {
  486. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  487. if (mb) {
  488. mc = mb->get_default_argument_count();
  489. }
  490. }
  491. if (mc == 0) {
  492. property.usage = 0; //do not show
  493. } else {
  494. property.hint_string = "0," + itos(mc) + ",1";
  495. }
  496. }
  497. if (property.name == "rpc_call_mode") {
  498. if (call_mode == CALL_MODE_BASIC_TYPE) {
  499. property.usage = 0;
  500. }
  501. }
  502. }
  503. void VisualScriptFunctionCall::_bind_methods() {
  504. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptFunctionCall::set_base_type);
  505. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptFunctionCall::get_base_type);
  506. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptFunctionCall::set_base_script);
  507. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptFunctionCall::get_base_script);
  508. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptFunctionCall::set_basic_type);
  509. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptFunctionCall::get_basic_type);
  510. ClassDB::bind_method(D_METHOD("set_singleton", "singleton"), &VisualScriptFunctionCall::set_singleton);
  511. ClassDB::bind_method(D_METHOD("get_singleton"), &VisualScriptFunctionCall::get_singleton);
  512. ClassDB::bind_method(D_METHOD("set_function", "function"), &VisualScriptFunctionCall::set_function);
  513. ClassDB::bind_method(D_METHOD("get_function"), &VisualScriptFunctionCall::get_function);
  514. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptFunctionCall::set_call_mode);
  515. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptFunctionCall::get_call_mode);
  516. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptFunctionCall::set_base_path);
  517. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptFunctionCall::get_base_path);
  518. ClassDB::bind_method(D_METHOD("set_use_default_args", "amount"), &VisualScriptFunctionCall::set_use_default_args);
  519. ClassDB::bind_method(D_METHOD("get_use_default_args"), &VisualScriptFunctionCall::get_use_default_args);
  520. ClassDB::bind_method(D_METHOD("_set_argument_cache", "argument_cache"), &VisualScriptFunctionCall::_set_argument_cache);
  521. ClassDB::bind_method(D_METHOD("_get_argument_cache"), &VisualScriptFunctionCall::_get_argument_cache);
  522. ClassDB::bind_method(D_METHOD("set_rpc_call_mode", "mode"), &VisualScriptFunctionCall::set_rpc_call_mode);
  523. ClassDB::bind_method(D_METHOD("get_rpc_call_mode"), &VisualScriptFunctionCall::get_rpc_call_mode);
  524. ClassDB::bind_method(D_METHOD("set_validate", "enable"), &VisualScriptFunctionCall::set_validate);
  525. ClassDB::bind_method(D_METHOD("get_validate"), &VisualScriptFunctionCall::get_validate);
  526. String bt;
  527. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  528. if (i > 0)
  529. bt += ",";
  530. bt += Variant::get_type_name(Variant::Type(i));
  531. }
  532. List<String> script_extensions;
  533. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  534. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  535. }
  536. String script_ext_hint;
  537. for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
  538. if (script_ext_hint != String())
  539. script_ext_hint += ",";
  540. script_ext_hint += "*." + E->get();
  541. }
  542. ADD_PROPERTY(PropertyInfo(Variant::INT, "call_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type,Singleton"), "set_call_mode", "get_call_mode");
  543. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  544. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  545. ADD_PROPERTY(PropertyInfo(Variant::STRING, "singleton"), "set_singleton", "get_singleton");
  546. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  547. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  548. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "argument_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_argument_cache", "_get_argument_cache");
  549. ADD_PROPERTY(PropertyInfo(Variant::STRING, "function"), "set_function", "get_function"); //when set, if loaded properly, will override argument count.
  550. ADD_PROPERTY(PropertyInfo(Variant::INT, "use_default_args"), "set_use_default_args", "get_use_default_args");
  551. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "validate"), "set_validate", "get_validate");
  552. ADD_PROPERTY(PropertyInfo(Variant::INT, "rpc_call_mode", PROPERTY_HINT_ENUM, "Disabled,Reliable,Unreliable,ReliableToID,UnreliableToID"), "set_rpc_call_mode", "get_rpc_call_mode"); //when set, if loaded properly, will override argument count.
  553. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  554. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  555. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  556. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  557. BIND_ENUM_CONSTANT(CALL_MODE_SINGLETON);
  558. BIND_ENUM_CONSTANT(RPC_DISABLED);
  559. BIND_ENUM_CONSTANT(RPC_RELIABLE);
  560. BIND_ENUM_CONSTANT(RPC_UNRELIABLE);
  561. BIND_ENUM_CONSTANT(RPC_RELIABLE_TO_ID);
  562. BIND_ENUM_CONSTANT(RPC_UNRELIABLE_TO_ID);
  563. }
  564. class VisualScriptNodeInstanceFunctionCall : public VisualScriptNodeInstance {
  565. public:
  566. VisualScriptFunctionCall::CallMode call_mode;
  567. NodePath node_path;
  568. int input_args;
  569. bool validate;
  570. int returns;
  571. VisualScriptFunctionCall::RPCCallMode rpc_mode;
  572. StringName function;
  573. StringName singleton;
  574. VisualScriptFunctionCall *node;
  575. VisualScriptInstance *instance;
  576. //virtual int get_working_memory_size() const { return 0; }
  577. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  578. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  579. _FORCE_INLINE_ bool call_rpc(Object *p_base, const Variant **p_args, int p_argcount) {
  580. if (!p_base)
  581. return false;
  582. Node *node = Object::cast_to<Node>(p_base);
  583. if (!node)
  584. return false;
  585. int to_id = 0;
  586. bool reliable = true;
  587. if (rpc_mode >= VisualScriptFunctionCall::RPC_RELIABLE_TO_ID) {
  588. to_id = *p_args[0];
  589. p_args += 1;
  590. p_argcount -= 1;
  591. if (rpc_mode == VisualScriptFunctionCall::RPC_UNRELIABLE_TO_ID) {
  592. reliable = false;
  593. }
  594. } else if (rpc_mode == VisualScriptFunctionCall::RPC_UNRELIABLE) {
  595. reliable = false;
  596. }
  597. node->rpcp(to_id, !reliable, function, p_args, p_argcount);
  598. return true;
  599. }
  600. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  601. switch (call_mode) {
  602. case VisualScriptFunctionCall::CALL_MODE_SELF: {
  603. Object *object = instance->get_owner_ptr();
  604. if (rpc_mode) {
  605. call_rpc(object, p_inputs, input_args);
  606. } else if (returns) {
  607. *p_outputs[0] = object->call(function, p_inputs, input_args, r_error);
  608. } else {
  609. object->call(function, p_inputs, input_args, r_error);
  610. }
  611. } break;
  612. case VisualScriptFunctionCall::CALL_MODE_NODE_PATH: {
  613. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  614. if (!node) {
  615. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  616. r_error_str = "Base object is not a Node!";
  617. return 0;
  618. }
  619. Node *another = node->get_node(node_path);
  620. if (!another) {
  621. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  622. r_error_str = "Path does not lead Node!";
  623. return 0;
  624. }
  625. if (rpc_mode) {
  626. call_rpc(node, p_inputs, input_args);
  627. } else if (returns) {
  628. *p_outputs[0] = another->call(function, p_inputs, input_args, r_error);
  629. } else {
  630. another->call(function, p_inputs, input_args, r_error);
  631. }
  632. } break;
  633. case VisualScriptFunctionCall::CALL_MODE_INSTANCE:
  634. case VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE: {
  635. Variant v = *p_inputs[0];
  636. if (rpc_mode) {
  637. Object *obj = v;
  638. if (obj) {
  639. call_rpc(obj, p_inputs + 1, input_args - 1);
  640. }
  641. } else if (returns) {
  642. if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) {
  643. if (returns >= 2) {
  644. *p_outputs[1] = v.call(function, p_inputs + 1, input_args, r_error);
  645. } else if (returns == 1) {
  646. v.call(function, p_inputs + 1, input_args, r_error);
  647. } else {
  648. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  649. r_error_str = "Invalid returns count for call_mode == CALL_MODE_INSTANCE";
  650. return 0;
  651. }
  652. } else {
  653. *p_outputs[0] = v.call(function, p_inputs + 1, input_args, r_error);
  654. }
  655. } else {
  656. v.call(function, p_inputs + 1, input_args, r_error);
  657. }
  658. if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) {
  659. *p_outputs[0] = *p_inputs[0];
  660. }
  661. } break;
  662. case VisualScriptFunctionCall::CALL_MODE_SINGLETON: {
  663. Object *object = Engine::get_singleton()->get_singleton_object(singleton);
  664. if (!object) {
  665. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  666. r_error_str = "Invalid singleton name: '" + String(singleton) + "'";
  667. return 0;
  668. }
  669. if (rpc_mode) {
  670. call_rpc(object, p_inputs, input_args);
  671. } else if (returns) {
  672. *p_outputs[0] = object->call(function, p_inputs, input_args, r_error);
  673. } else {
  674. object->call(function, p_inputs, input_args, r_error);
  675. }
  676. } break;
  677. }
  678. if (!validate) {
  679. //ignore call errors if validation is disabled
  680. r_error.error = Variant::CallError::CALL_OK;
  681. r_error_str = String();
  682. }
  683. return 0;
  684. }
  685. };
  686. VisualScriptNodeInstance *VisualScriptFunctionCall::instance(VisualScriptInstance *p_instance) {
  687. VisualScriptNodeInstanceFunctionCall *instance = memnew(VisualScriptNodeInstanceFunctionCall);
  688. instance->node = this;
  689. instance->instance = p_instance;
  690. instance->singleton = singleton;
  691. instance->function = function;
  692. instance->call_mode = call_mode;
  693. instance->returns = get_output_value_port_count();
  694. instance->node_path = base_path;
  695. instance->input_args = get_input_value_port_count() - ((call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0);
  696. instance->rpc_mode = rpc_call_mode;
  697. instance->validate = validate;
  698. return instance;
  699. }
  700. VisualScriptFunctionCall::TypeGuess VisualScriptFunctionCall::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  701. if (p_output == 0 && call_mode == CALL_MODE_INSTANCE) {
  702. return p_inputs[0];
  703. }
  704. return VisualScriptNode::guess_output_type(p_inputs, p_output);
  705. }
  706. VisualScriptFunctionCall::VisualScriptFunctionCall() {
  707. validate = true;
  708. call_mode = CALL_MODE_SELF;
  709. basic_type = Variant::NIL;
  710. use_default_args = 0;
  711. base_type = "Object";
  712. rpc_call_mode = RPC_DISABLED;
  713. }
  714. template <VisualScriptFunctionCall::CallMode cmode>
  715. static Ref<VisualScriptNode> create_function_call_node(const String &p_name) {
  716. Ref<VisualScriptFunctionCall> node;
  717. node.instance();
  718. node->set_call_mode(cmode);
  719. return node;
  720. }
  721. //////////////////////////////////////////
  722. ////////////////SET//////////////////////
  723. //////////////////////////////////////////
  724. int VisualScriptPropertySet::get_output_sequence_port_count() const {
  725. return call_mode != CALL_MODE_BASIC_TYPE ? 1 : 0;
  726. }
  727. bool VisualScriptPropertySet::has_input_sequence_port() const {
  728. return call_mode != CALL_MODE_BASIC_TYPE ? true : false;
  729. }
  730. Node *VisualScriptPropertySet::_get_base_node() const {
  731. #ifdef TOOLS_ENABLED
  732. Ref<Script> script = get_visual_script();
  733. if (!script.is_valid())
  734. return NULL;
  735. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  736. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  737. if (!scene_tree)
  738. return NULL;
  739. Node *edited_scene = scene_tree->get_edited_scene_root();
  740. if (!edited_scene)
  741. return NULL;
  742. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  743. if (!script_node)
  744. return NULL;
  745. if (!script_node->has_node(base_path))
  746. return NULL;
  747. Node *path_to = script_node->get_node(base_path);
  748. return path_to;
  749. #else
  750. return NULL;
  751. #endif
  752. }
  753. StringName VisualScriptPropertySet::_get_base_type() const {
  754. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid())
  755. return get_visual_script()->get_instance_base_type();
  756. else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  757. Node *path = _get_base_node();
  758. if (path)
  759. return path->get_class();
  760. }
  761. return base_type;
  762. }
  763. int VisualScriptPropertySet::get_input_value_port_count() const {
  764. int pc = (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 2 : 1;
  765. return pc;
  766. }
  767. int VisualScriptPropertySet::get_output_value_port_count() const {
  768. return (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0;
  769. }
  770. String VisualScriptPropertySet::get_output_sequence_port_text(int p_port) const {
  771. return String();
  772. }
  773. void VisualScriptPropertySet::_adjust_input_index(PropertyInfo &pinfo) const {
  774. if (index != StringName()) {
  775. Variant v;
  776. Variant::CallError ce;
  777. v = Variant::construct(pinfo.type, NULL, 0, ce);
  778. Variant i = v.get(index);
  779. pinfo.type = i.get_type();
  780. }
  781. }
  782. PropertyInfo VisualScriptPropertySet::get_input_value_port_info(int p_idx) const {
  783. if (call_mode == CALL_MODE_INSTANCE || call_mode == CALL_MODE_BASIC_TYPE) {
  784. if (p_idx == 0) {
  785. PropertyInfo pi;
  786. pi.type = (call_mode == CALL_MODE_INSTANCE ? Variant::OBJECT : basic_type);
  787. pi.name = (call_mode == CALL_MODE_INSTANCE ? String("instance") : Variant::get_type_name(basic_type).to_lower());
  788. _adjust_input_index(pi);
  789. return pi;
  790. } else {
  791. p_idx--;
  792. }
  793. }
  794. PropertyInfo pinfo = type_cache;
  795. pinfo.name = "value";
  796. _adjust_input_index(pinfo);
  797. return pinfo;
  798. }
  799. PropertyInfo VisualScriptPropertySet::get_output_value_port_info(int p_idx) const {
  800. if (call_mode == CALL_MODE_BASIC_TYPE) {
  801. return PropertyInfo(basic_type, "out");
  802. } else if (call_mode == CALL_MODE_INSTANCE) {
  803. return PropertyInfo(Variant::OBJECT, "pass", PROPERTY_HINT_TYPE_STRING, get_base_type());
  804. } else {
  805. return PropertyInfo();
  806. }
  807. }
  808. String VisualScriptPropertySet::get_caption() const {
  809. static const char *opname[ASSIGN_OP_MAX] = {
  810. "Set", "Add", "Subtract", "Multiply", "Divide", "Mod", "ShiftLeft", "ShiftRight", "BitAnd", "BitOr", "BitXor"
  811. };
  812. String prop = String(opname[assign_op]) + " " + property;
  813. if (index != StringName()) {
  814. prop += "." + String(index);
  815. }
  816. return prop;
  817. }
  818. String VisualScriptPropertySet::get_text() const {
  819. if (call_mode == CALL_MODE_BASIC_TYPE) {
  820. return String("On ") + Variant::get_type_name(basic_type);
  821. }
  822. static const char *cname[3] = {
  823. "Self",
  824. "Scene Node",
  825. "Instance"
  826. };
  827. return String("On ") + cname[call_mode];
  828. }
  829. void VisualScriptPropertySet::_update_base_type() {
  830. //cache it because this information may not be available on load
  831. if (call_mode == CALL_MODE_NODE_PATH) {
  832. Node *node = _get_base_node();
  833. if (node) {
  834. base_type = node->get_class();
  835. }
  836. } else if (call_mode == CALL_MODE_SELF) {
  837. if (get_visual_script().is_valid()) {
  838. base_type = get_visual_script()->get_instance_base_type();
  839. }
  840. }
  841. }
  842. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  843. if (basic_type == p_type)
  844. return;
  845. basic_type = p_type;
  846. _change_notify();
  847. _update_base_type();
  848. ports_changed_notify();
  849. }
  850. Variant::Type VisualScriptPropertySet::get_basic_type() const {
  851. return basic_type;
  852. }
  853. void VisualScriptPropertySet::set_base_type(const StringName &p_type) {
  854. if (base_type == p_type)
  855. return;
  856. base_type = p_type;
  857. _change_notify();
  858. ports_changed_notify();
  859. }
  860. StringName VisualScriptPropertySet::get_base_type() const {
  861. return base_type;
  862. }
  863. void VisualScriptPropertySet::set_base_script(const String &p_path) {
  864. if (base_script == p_path)
  865. return;
  866. base_script = p_path;
  867. _change_notify();
  868. ports_changed_notify();
  869. }
  870. String VisualScriptPropertySet::get_base_script() const {
  871. return base_script;
  872. }
  873. void VisualScriptPropertySet::_update_cache() {
  874. if (!Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop()))
  875. return;
  876. if (!Engine::get_singleton()->is_editor_hint()) //only update cache if editor exists, it's pointless otherwise
  877. return;
  878. if (call_mode == CALL_MODE_BASIC_TYPE) {
  879. //not super efficient..
  880. Variant v;
  881. Variant::CallError ce;
  882. v = Variant::construct(basic_type, NULL, 0, ce);
  883. List<PropertyInfo> pinfo;
  884. v.get_property_list(&pinfo);
  885. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  886. if (E->get().name == property) {
  887. type_cache = E->get();
  888. }
  889. }
  890. } else {
  891. StringName type;
  892. Ref<Script> script;
  893. Node *node = NULL;
  894. if (call_mode == CALL_MODE_NODE_PATH) {
  895. node = _get_base_node();
  896. if (node) {
  897. type = node->get_class();
  898. base_type = type; //cache, too
  899. script = node->get_script();
  900. }
  901. } else if (call_mode == CALL_MODE_SELF) {
  902. if (get_visual_script().is_valid()) {
  903. type = get_visual_script()->get_instance_base_type();
  904. base_type = type; //cache, too
  905. script = get_visual_script();
  906. }
  907. } else if (call_mode == CALL_MODE_INSTANCE) {
  908. type = base_type;
  909. if (base_script != String()) {
  910. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  911. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  912. }
  913. if (ResourceCache::has(base_script)) {
  914. script = Ref<Resource>(ResourceCache::get(base_script));
  915. } else {
  916. return;
  917. }
  918. }
  919. }
  920. List<PropertyInfo> pinfo;
  921. if (node) {
  922. node->get_property_list(&pinfo);
  923. } else {
  924. ClassDB::get_property_list(type, &pinfo);
  925. }
  926. if (script.is_valid()) {
  927. script->get_script_property_list(&pinfo);
  928. }
  929. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  930. if (E->get().name == property) {
  931. type_cache = E->get();
  932. return;
  933. }
  934. }
  935. }
  936. }
  937. void VisualScriptPropertySet::set_property(const StringName &p_type) {
  938. if (property == p_type)
  939. return;
  940. property = p_type;
  941. index = StringName();
  942. _update_cache();
  943. _change_notify();
  944. ports_changed_notify();
  945. }
  946. StringName VisualScriptPropertySet::get_property() const {
  947. return property;
  948. }
  949. void VisualScriptPropertySet::set_base_path(const NodePath &p_type) {
  950. if (base_path == p_type)
  951. return;
  952. base_path = p_type;
  953. _update_base_type();
  954. _change_notify();
  955. ports_changed_notify();
  956. }
  957. NodePath VisualScriptPropertySet::get_base_path() const {
  958. return base_path;
  959. }
  960. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  961. if (call_mode == p_mode)
  962. return;
  963. call_mode = p_mode;
  964. _update_base_type();
  965. _change_notify();
  966. ports_changed_notify();
  967. }
  968. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  969. return call_mode;
  970. }
  971. void VisualScriptPropertySet::_set_type_cache(const Dictionary &p_type) {
  972. type_cache = PropertyInfo::from_dict(p_type);
  973. }
  974. Dictionary VisualScriptPropertySet::_get_type_cache() const {
  975. return type_cache;
  976. }
  977. void VisualScriptPropertySet::set_index(const StringName &p_type) {
  978. if (index == p_type)
  979. return;
  980. index = p_type;
  981. _update_cache();
  982. _change_notify();
  983. ports_changed_notify();
  984. }
  985. StringName VisualScriptPropertySet::get_index() const {
  986. return index;
  987. }
  988. void VisualScriptPropertySet::set_assign_op(AssignOp p_op) {
  989. ERR_FAIL_INDEX(p_op, ASSIGN_OP_MAX);
  990. if (assign_op == p_op)
  991. return;
  992. assign_op = p_op;
  993. _update_cache();
  994. _change_notify();
  995. ports_changed_notify();
  996. }
  997. VisualScriptPropertySet::AssignOp VisualScriptPropertySet::get_assign_op() const {
  998. return assign_op;
  999. }
  1000. void VisualScriptPropertySet::_validate_property(PropertyInfo &property) const {
  1001. if (property.name == "base_type") {
  1002. if (call_mode != CALL_MODE_INSTANCE) {
  1003. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  1004. }
  1005. }
  1006. if (property.name == "base_script") {
  1007. if (call_mode != CALL_MODE_INSTANCE) {
  1008. property.usage = 0;
  1009. }
  1010. }
  1011. if (property.name == "basic_type") {
  1012. if (call_mode != CALL_MODE_BASIC_TYPE) {
  1013. property.usage = 0;
  1014. }
  1015. }
  1016. if (property.name == "node_path") {
  1017. if (call_mode != CALL_MODE_NODE_PATH) {
  1018. property.usage = 0;
  1019. } else {
  1020. Node *bnode = _get_base_node();
  1021. if (bnode) {
  1022. property.hint_string = bnode->get_path(); //convert to loong string
  1023. } else {
  1024. }
  1025. }
  1026. }
  1027. if (property.name == "property") {
  1028. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1029. property.hint = PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1030. property.hint_string = Variant::get_type_name(basic_type);
  1031. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1032. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1033. property.hint_string = itos(get_visual_script()->get_instance_id());
  1034. } else if (call_mode == CALL_MODE_INSTANCE) {
  1035. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1036. property.hint_string = base_type;
  1037. if (base_script != String()) {
  1038. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1039. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1040. }
  1041. if (ResourceCache::has(base_script)) {
  1042. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  1043. if (script.is_valid()) {
  1044. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1045. property.hint_string = itos(script->get_instance_id());
  1046. }
  1047. }
  1048. }
  1049. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1050. Node *node = _get_base_node();
  1051. if (node) {
  1052. property.hint = PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1053. property.hint_string = itos(node->get_instance_id());
  1054. } else {
  1055. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1056. property.hint_string = get_base_type();
  1057. }
  1058. }
  1059. }
  1060. if (property.name == "index") {
  1061. Variant::CallError ce;
  1062. Variant v = Variant::construct(type_cache.type, NULL, 0, ce);
  1063. List<PropertyInfo> plist;
  1064. v.get_property_list(&plist);
  1065. String options = "";
  1066. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1067. options += "," + E->get().name;
  1068. }
  1069. property.hint = PROPERTY_HINT_ENUM;
  1070. property.hint_string = options;
  1071. property.type = Variant::STRING;
  1072. if (options == "")
  1073. property.usage = 0; //hide if type has no usable index
  1074. }
  1075. }
  1076. void VisualScriptPropertySet::_bind_methods() {
  1077. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptPropertySet::set_base_type);
  1078. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptPropertySet::get_base_type);
  1079. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptPropertySet::set_base_script);
  1080. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptPropertySet::get_base_script);
  1081. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptPropertySet::set_basic_type);
  1082. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptPropertySet::get_basic_type);
  1083. ClassDB::bind_method(D_METHOD("_set_type_cache", "type_cache"), &VisualScriptPropertySet::_set_type_cache);
  1084. ClassDB::bind_method(D_METHOD("_get_type_cache"), &VisualScriptPropertySet::_get_type_cache);
  1085. ClassDB::bind_method(D_METHOD("set_property", "property"), &VisualScriptPropertySet::set_property);
  1086. ClassDB::bind_method(D_METHOD("get_property"), &VisualScriptPropertySet::get_property);
  1087. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptPropertySet::set_call_mode);
  1088. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptPropertySet::get_call_mode);
  1089. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptPropertySet::set_base_path);
  1090. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptPropertySet::get_base_path);
  1091. ClassDB::bind_method(D_METHOD("set_index", "index"), &VisualScriptPropertySet::set_index);
  1092. ClassDB::bind_method(D_METHOD("get_index"), &VisualScriptPropertySet::get_index);
  1093. ClassDB::bind_method(D_METHOD("set_assign_op", "assign_op"), &VisualScriptPropertySet::set_assign_op);
  1094. ClassDB::bind_method(D_METHOD("get_assign_op"), &VisualScriptPropertySet::get_assign_op);
  1095. String bt;
  1096. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1097. if (i > 0)
  1098. bt += ",";
  1099. bt += Variant::get_type_name(Variant::Type(i));
  1100. }
  1101. List<String> script_extensions;
  1102. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1103. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1104. }
  1105. String script_ext_hint;
  1106. for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
  1107. if (script_ext_hint != String())
  1108. script_ext_hint += ",";
  1109. script_ext_hint += "*." + E->get();
  1110. }
  1111. ADD_PROPERTY(PropertyInfo(Variant::INT, "set_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type"), "set_call_mode", "get_call_mode");
  1112. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1113. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1114. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_type_cache", "_get_type_cache");
  1115. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  1116. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  1117. ADD_PROPERTY(PropertyInfo(Variant::STRING, "property"), "set_property", "get_property");
  1118. ADD_PROPERTY(PropertyInfo(Variant::STRING, "index"), "set_index", "get_index");
  1119. ADD_PROPERTY(PropertyInfo(Variant::INT, "assign_op", PROPERTY_HINT_ENUM, "Assign,Add,Sub,Mul,Div,Mod,ShiftLeft,ShiftRight,BitAnd,BitOr,Bitxor"), "set_assign_op", "get_assign_op");
  1120. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  1121. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  1122. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  1123. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  1124. BIND_ENUM_CONSTANT(ASSIGN_OP_NONE);
  1125. BIND_ENUM_CONSTANT(ASSIGN_OP_ADD);
  1126. BIND_ENUM_CONSTANT(ASSIGN_OP_SUB);
  1127. BIND_ENUM_CONSTANT(ASSIGN_OP_MUL);
  1128. BIND_ENUM_CONSTANT(ASSIGN_OP_DIV);
  1129. BIND_ENUM_CONSTANT(ASSIGN_OP_MOD);
  1130. BIND_ENUM_CONSTANT(ASSIGN_OP_SHIFT_LEFT);
  1131. BIND_ENUM_CONSTANT(ASSIGN_OP_SHIFT_RIGHT);
  1132. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_AND);
  1133. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_OR);
  1134. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_XOR);
  1135. }
  1136. class VisualScriptNodeInstancePropertySet : public VisualScriptNodeInstance {
  1137. public:
  1138. VisualScriptPropertySet::CallMode call_mode;
  1139. NodePath node_path;
  1140. StringName property;
  1141. VisualScriptPropertySet *node;
  1142. VisualScriptInstance *instance;
  1143. VisualScriptPropertySet::AssignOp assign_op;
  1144. StringName index;
  1145. bool needs_get;
  1146. //virtual int get_working_memory_size() const { return 0; }
  1147. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1148. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1149. _FORCE_INLINE_ void _process_get(Variant &source, const Variant &p_argument, bool &valid) {
  1150. if (index != StringName() && assign_op == VisualScriptPropertySet::ASSIGN_OP_NONE) {
  1151. source.set_named(index, p_argument, &valid);
  1152. } else {
  1153. Variant value;
  1154. if (index != StringName()) {
  1155. value = source.get_named(index, &valid);
  1156. } else {
  1157. value = source;
  1158. }
  1159. switch (assign_op) {
  1160. case VisualScriptPropertySet::ASSIGN_OP_NONE: {
  1161. //should never get here
  1162. } break;
  1163. case VisualScriptPropertySet::ASSIGN_OP_ADD: {
  1164. value = Variant::evaluate(Variant::OP_ADD, value, p_argument);
  1165. } break;
  1166. case VisualScriptPropertySet::ASSIGN_OP_SUB: {
  1167. value = Variant::evaluate(Variant::OP_SUBTRACT, value, p_argument);
  1168. } break;
  1169. case VisualScriptPropertySet::ASSIGN_OP_MUL: {
  1170. value = Variant::evaluate(Variant::OP_MULTIPLY, value, p_argument);
  1171. } break;
  1172. case VisualScriptPropertySet::ASSIGN_OP_DIV: {
  1173. value = Variant::evaluate(Variant::OP_DIVIDE, value, p_argument);
  1174. } break;
  1175. case VisualScriptPropertySet::ASSIGN_OP_MOD: {
  1176. value = Variant::evaluate(Variant::OP_MODULE, value, p_argument);
  1177. } break;
  1178. case VisualScriptPropertySet::ASSIGN_OP_SHIFT_LEFT: {
  1179. value = Variant::evaluate(Variant::OP_SHIFT_LEFT, value, p_argument);
  1180. } break;
  1181. case VisualScriptPropertySet::ASSIGN_OP_SHIFT_RIGHT: {
  1182. value = Variant::evaluate(Variant::OP_SHIFT_RIGHT, value, p_argument);
  1183. } break;
  1184. case VisualScriptPropertySet::ASSIGN_OP_BIT_AND: {
  1185. value = Variant::evaluate(Variant::OP_BIT_AND, value, p_argument);
  1186. } break;
  1187. case VisualScriptPropertySet::ASSIGN_OP_BIT_OR: {
  1188. value = Variant::evaluate(Variant::OP_BIT_OR, value, p_argument);
  1189. } break;
  1190. case VisualScriptPropertySet::ASSIGN_OP_BIT_XOR: {
  1191. value = Variant::evaluate(Variant::OP_BIT_XOR, value, p_argument);
  1192. } break;
  1193. default: {}
  1194. }
  1195. if (index != StringName()) {
  1196. source.set_named(index, value, &valid);
  1197. } else {
  1198. source = value;
  1199. }
  1200. }
  1201. }
  1202. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1203. switch (call_mode) {
  1204. case VisualScriptPropertySet::CALL_MODE_SELF: {
  1205. Object *object = instance->get_owner_ptr();
  1206. bool valid;
  1207. if (needs_get) {
  1208. Variant value = object->get(property, &valid);
  1209. _process_get(value, *p_inputs[0], valid);
  1210. object->set(property, value, &valid);
  1211. } else {
  1212. object->set(property, *p_inputs[0], &valid);
  1213. }
  1214. if (!valid) {
  1215. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1216. r_error_str = "Invalid set value '" + String(*p_inputs[0]) + "' on property '" + String(property) + "' of type " + object->get_class();
  1217. }
  1218. } break;
  1219. case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
  1220. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1221. if (!node) {
  1222. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1223. r_error_str = "Base object is not a Node!";
  1224. return 0;
  1225. }
  1226. Node *another = node->get_node(node_path);
  1227. if (!another) {
  1228. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1229. r_error_str = "Path does not lead Node!";
  1230. return 0;
  1231. }
  1232. bool valid;
  1233. if (needs_get) {
  1234. Variant value = another->get(property, &valid);
  1235. _process_get(value, *p_inputs[0], valid);
  1236. another->set(property, value, &valid);
  1237. } else {
  1238. another->set(property, *p_inputs[0], &valid);
  1239. }
  1240. if (!valid) {
  1241. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1242. r_error_str = "Invalid set value '" + String(*p_inputs[0]) + "' on property '" + String(property) + "' of type " + another->get_class();
  1243. }
  1244. } break;
  1245. case VisualScriptPropertySet::CALL_MODE_INSTANCE:
  1246. case VisualScriptPropertySet::CALL_MODE_BASIC_TYPE: {
  1247. Variant v = *p_inputs[0];
  1248. bool valid;
  1249. if (needs_get) {
  1250. Variant value = v.get_named(property, &valid);
  1251. _process_get(value, *p_inputs[1], valid);
  1252. v.set_named(property, value, &valid);
  1253. } else {
  1254. v.set_named(property, *p_inputs[1], &valid);
  1255. }
  1256. if (!valid) {
  1257. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1258. r_error_str = "Invalid set value '" + String(*p_inputs[1]) + "' (" + Variant::get_type_name(p_inputs[1]->get_type()) + ") on property '" + String(property) + "' of type " + Variant::get_type_name(v.get_type());
  1259. }
  1260. *p_outputs[0] = v;
  1261. } break;
  1262. }
  1263. return 0;
  1264. }
  1265. };
  1266. VisualScriptNodeInstance *VisualScriptPropertySet::instance(VisualScriptInstance *p_instance) {
  1267. VisualScriptNodeInstancePropertySet *instance = memnew(VisualScriptNodeInstancePropertySet);
  1268. instance->node = this;
  1269. instance->instance = p_instance;
  1270. instance->property = property;
  1271. instance->call_mode = call_mode;
  1272. instance->node_path = base_path;
  1273. instance->assign_op = assign_op;
  1274. instance->index = index;
  1275. instance->needs_get = index != StringName() || assign_op != ASSIGN_OP_NONE;
  1276. return instance;
  1277. }
  1278. VisualScriptPropertySet::TypeGuess VisualScriptPropertySet::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1279. if (p_output == 0 && call_mode == CALL_MODE_INSTANCE) {
  1280. return p_inputs[0];
  1281. }
  1282. return VisualScriptNode::guess_output_type(p_inputs, p_output);
  1283. }
  1284. VisualScriptPropertySet::VisualScriptPropertySet() {
  1285. assign_op = ASSIGN_OP_NONE;
  1286. call_mode = CALL_MODE_SELF;
  1287. base_type = "Object";
  1288. basic_type = Variant::NIL;
  1289. }
  1290. template <VisualScriptPropertySet::CallMode cmode>
  1291. static Ref<VisualScriptNode> create_property_set_node(const String &p_name) {
  1292. Ref<VisualScriptPropertySet> node;
  1293. node.instance();
  1294. node->set_call_mode(cmode);
  1295. return node;
  1296. }
  1297. //////////////////////////////////////////
  1298. ////////////////GET//////////////////////
  1299. //////////////////////////////////////////
  1300. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  1301. return 0; // (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?0:1;
  1302. }
  1303. bool VisualScriptPropertyGet::has_input_sequence_port() const {
  1304. return false; //(call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?false:true;
  1305. }
  1306. void VisualScriptPropertyGet::_update_base_type() {
  1307. //cache it because this information may not be available on load
  1308. if (call_mode == CALL_MODE_NODE_PATH) {
  1309. Node *node = _get_base_node();
  1310. if (node) {
  1311. base_type = node->get_class();
  1312. }
  1313. } else if (call_mode == CALL_MODE_SELF) {
  1314. if (get_visual_script().is_valid()) {
  1315. base_type = get_visual_script()->get_instance_base_type();
  1316. }
  1317. }
  1318. }
  1319. Node *VisualScriptPropertyGet::_get_base_node() const {
  1320. #ifdef TOOLS_ENABLED
  1321. Ref<Script> script = get_visual_script();
  1322. if (!script.is_valid())
  1323. return NULL;
  1324. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  1325. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  1326. if (!scene_tree)
  1327. return NULL;
  1328. Node *edited_scene = scene_tree->get_edited_scene_root();
  1329. if (!edited_scene)
  1330. return NULL;
  1331. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  1332. if (!script_node)
  1333. return NULL;
  1334. if (!script_node->has_node(base_path))
  1335. return NULL;
  1336. Node *path_to = script_node->get_node(base_path);
  1337. return path_to;
  1338. #else
  1339. return NULL;
  1340. #endif
  1341. }
  1342. StringName VisualScriptPropertyGet::_get_base_type() const {
  1343. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid())
  1344. return get_visual_script()->get_instance_base_type();
  1345. else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  1346. Node *path = _get_base_node();
  1347. if (path)
  1348. return path->get_class();
  1349. }
  1350. return base_type;
  1351. }
  1352. int VisualScriptPropertyGet::get_input_value_port_count() const {
  1353. return (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0;
  1354. }
  1355. int VisualScriptPropertyGet::get_output_value_port_count() const {
  1356. return 1;
  1357. }
  1358. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  1359. return String();
  1360. }
  1361. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const {
  1362. if (call_mode == CALL_MODE_INSTANCE || call_mode == CALL_MODE_BASIC_TYPE) {
  1363. if (p_idx == 0) {
  1364. PropertyInfo pi;
  1365. pi.type = (call_mode == CALL_MODE_INSTANCE ? Variant::OBJECT : basic_type);
  1366. pi.name = (call_mode == CALL_MODE_INSTANCE ? String("instance") : Variant::get_type_name(basic_type).to_lower());
  1367. return pi;
  1368. } else {
  1369. p_idx--;
  1370. }
  1371. }
  1372. return PropertyInfo();
  1373. }
  1374. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const {
  1375. if (index != StringName()) {
  1376. Variant v;
  1377. Variant::CallError ce;
  1378. v = Variant::construct(type_cache, NULL, 0, ce);
  1379. Variant i = v.get(index);
  1380. return PropertyInfo(i.get_type(), "value." + String(index));
  1381. }
  1382. return PropertyInfo(type_cache, "value");
  1383. }
  1384. String VisualScriptPropertyGet::get_caption() const {
  1385. return String("Get ") + property;
  1386. }
  1387. String VisualScriptPropertyGet::get_text() const {
  1388. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1389. return String("On ") + Variant::get_type_name(basic_type);
  1390. }
  1391. static const char *cname[3] = {
  1392. "Self",
  1393. "Scene Node",
  1394. "Instance"
  1395. };
  1396. return String("On ") + cname[call_mode];
  1397. }
  1398. void VisualScriptPropertyGet::set_base_type(const StringName &p_type) {
  1399. if (base_type == p_type)
  1400. return;
  1401. base_type = p_type;
  1402. _change_notify();
  1403. ports_changed_notify();
  1404. }
  1405. StringName VisualScriptPropertyGet::get_base_type() const {
  1406. return base_type;
  1407. }
  1408. void VisualScriptPropertyGet::set_base_script(const String &p_path) {
  1409. if (base_script == p_path)
  1410. return;
  1411. base_script = p_path;
  1412. _change_notify();
  1413. ports_changed_notify();
  1414. }
  1415. String VisualScriptPropertyGet::get_base_script() const {
  1416. return base_script;
  1417. }
  1418. void VisualScriptPropertyGet::_update_cache() {
  1419. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1420. //not super efficient..
  1421. Variant v;
  1422. Variant::CallError ce;
  1423. v = Variant::construct(basic_type, NULL, 0, ce);
  1424. List<PropertyInfo> pinfo;
  1425. v.get_property_list(&pinfo);
  1426. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1427. if (E->get().name == property) {
  1428. type_cache = E->get().type;
  1429. return;
  1430. }
  1431. }
  1432. } else {
  1433. StringName type;
  1434. Ref<Script> script;
  1435. Node *node = NULL;
  1436. if (call_mode == CALL_MODE_NODE_PATH) {
  1437. node = _get_base_node();
  1438. if (node) {
  1439. type = node->get_class();
  1440. base_type = type; //cache, too
  1441. script = node->get_script();
  1442. }
  1443. } else if (call_mode == CALL_MODE_SELF) {
  1444. if (get_visual_script().is_valid()) {
  1445. type = get_visual_script()->get_instance_base_type();
  1446. base_type = type; //cache, too
  1447. script = get_visual_script();
  1448. }
  1449. } else if (call_mode == CALL_MODE_INSTANCE) {
  1450. type = base_type;
  1451. if (base_script != String()) {
  1452. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1453. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1454. }
  1455. if (ResourceCache::has(base_script)) {
  1456. script = Ref<Resource>(ResourceCache::get(base_script));
  1457. } else {
  1458. return;
  1459. }
  1460. }
  1461. }
  1462. bool valid = false;
  1463. Variant::Type type_ret;
  1464. type_ret = ClassDB::get_property_type(base_type, property, &valid);
  1465. if (valid) {
  1466. type_cache = type_ret;
  1467. return; //all dandy
  1468. }
  1469. if (node) {
  1470. Variant prop = node->get(property, &valid);
  1471. if (valid) {
  1472. type_cache = prop.get_type();
  1473. return; //all dandy again
  1474. }
  1475. }
  1476. if (script.is_valid()) {
  1477. type_ret = script->get_static_property_type(property, &valid);
  1478. if (valid) {
  1479. type_cache = type_ret;
  1480. return; //all dandy
  1481. }
  1482. }
  1483. }
  1484. }
  1485. void VisualScriptPropertyGet::set_property(const StringName &p_type) {
  1486. if (property == p_type)
  1487. return;
  1488. property = p_type;
  1489. _update_cache();
  1490. _change_notify();
  1491. ports_changed_notify();
  1492. }
  1493. StringName VisualScriptPropertyGet::get_property() const {
  1494. return property;
  1495. }
  1496. void VisualScriptPropertyGet::set_base_path(const NodePath &p_type) {
  1497. if (base_path == p_type)
  1498. return;
  1499. base_path = p_type;
  1500. _change_notify();
  1501. _update_base_type();
  1502. ports_changed_notify();
  1503. }
  1504. NodePath VisualScriptPropertyGet::get_base_path() const {
  1505. return base_path;
  1506. }
  1507. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  1508. if (call_mode == p_mode)
  1509. return;
  1510. call_mode = p_mode;
  1511. _change_notify();
  1512. _update_base_type();
  1513. ports_changed_notify();
  1514. }
  1515. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  1516. return call_mode;
  1517. }
  1518. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  1519. if (basic_type == p_type)
  1520. return;
  1521. basic_type = p_type;
  1522. _change_notify();
  1523. ports_changed_notify();
  1524. }
  1525. Variant::Type VisualScriptPropertyGet::get_basic_type() const {
  1526. return basic_type;
  1527. }
  1528. void VisualScriptPropertyGet::_set_type_cache(Variant::Type p_type) {
  1529. type_cache = p_type;
  1530. }
  1531. Variant::Type VisualScriptPropertyGet::_get_type_cache() const {
  1532. return type_cache;
  1533. }
  1534. void VisualScriptPropertyGet::set_index(const StringName &p_type) {
  1535. if (index == p_type)
  1536. return;
  1537. index = p_type;
  1538. _update_cache();
  1539. _change_notify();
  1540. ports_changed_notify();
  1541. }
  1542. StringName VisualScriptPropertyGet::get_index() const {
  1543. return index;
  1544. }
  1545. void VisualScriptPropertyGet::_validate_property(PropertyInfo &property) const {
  1546. if (property.name == "base_type") {
  1547. if (call_mode != CALL_MODE_INSTANCE) {
  1548. property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
  1549. }
  1550. }
  1551. if (property.name == "base_script") {
  1552. if (call_mode != CALL_MODE_INSTANCE) {
  1553. property.usage = 0;
  1554. }
  1555. }
  1556. if (property.name == "basic_type") {
  1557. if (call_mode != CALL_MODE_BASIC_TYPE) {
  1558. property.usage = 0;
  1559. }
  1560. }
  1561. if (property.name == "node_path") {
  1562. if (call_mode != CALL_MODE_NODE_PATH) {
  1563. property.usage = 0;
  1564. } else {
  1565. Node *bnode = _get_base_node();
  1566. if (bnode) {
  1567. property.hint_string = bnode->get_path(); //convert to loong string
  1568. } else {
  1569. }
  1570. }
  1571. }
  1572. if (property.name == "property") {
  1573. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1574. property.hint = PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1575. property.hint_string = Variant::get_type_name(basic_type);
  1576. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1577. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1578. property.hint_string = itos(get_visual_script()->get_instance_id());
  1579. } else if (call_mode == CALL_MODE_INSTANCE) {
  1580. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1581. property.hint_string = base_type;
  1582. if (base_script != String()) {
  1583. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1584. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1585. }
  1586. if (ResourceCache::has(base_script)) {
  1587. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  1588. if (script.is_valid()) {
  1589. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1590. property.hint_string = itos(script->get_instance_id());
  1591. }
  1592. }
  1593. }
  1594. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1595. Node *node = _get_base_node();
  1596. if (node) {
  1597. property.hint = PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1598. property.hint_string = itos(node->get_instance_id());
  1599. } else {
  1600. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1601. property.hint_string = get_base_type();
  1602. }
  1603. }
  1604. }
  1605. if (property.name == "index") {
  1606. Variant::CallError ce;
  1607. Variant v = Variant::construct(type_cache, NULL, 0, ce);
  1608. List<PropertyInfo> plist;
  1609. v.get_property_list(&plist);
  1610. String options = "";
  1611. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1612. options += "," + E->get().name;
  1613. }
  1614. property.hint = PROPERTY_HINT_ENUM;
  1615. property.hint_string = options;
  1616. property.type = Variant::STRING;
  1617. if (options == "")
  1618. property.usage = 0; //hide if type has no usable index
  1619. }
  1620. }
  1621. void VisualScriptPropertyGet::_bind_methods() {
  1622. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptPropertyGet::set_base_type);
  1623. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptPropertyGet::get_base_type);
  1624. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptPropertyGet::set_base_script);
  1625. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptPropertyGet::get_base_script);
  1626. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptPropertyGet::set_basic_type);
  1627. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptPropertyGet::get_basic_type);
  1628. ClassDB::bind_method(D_METHOD("_set_type_cache", "type_cache"), &VisualScriptPropertyGet::_set_type_cache);
  1629. ClassDB::bind_method(D_METHOD("_get_type_cache"), &VisualScriptPropertyGet::_get_type_cache);
  1630. ClassDB::bind_method(D_METHOD("set_property", "property"), &VisualScriptPropertyGet::set_property);
  1631. ClassDB::bind_method(D_METHOD("get_property"), &VisualScriptPropertyGet::get_property);
  1632. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptPropertyGet::set_call_mode);
  1633. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptPropertyGet::get_call_mode);
  1634. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptPropertyGet::set_base_path);
  1635. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptPropertyGet::get_base_path);
  1636. ClassDB::bind_method(D_METHOD("set_index", "index"), &VisualScriptPropertyGet::set_index);
  1637. ClassDB::bind_method(D_METHOD("get_index"), &VisualScriptPropertyGet::get_index);
  1638. String bt;
  1639. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1640. if (i > 0)
  1641. bt += ",";
  1642. bt += Variant::get_type_name(Variant::Type(i));
  1643. }
  1644. List<String> script_extensions;
  1645. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1646. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1647. }
  1648. String script_ext_hint;
  1649. for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
  1650. if (script_ext_hint != String())
  1651. script_ext_hint += ",";
  1652. script_ext_hint += "." + E->get();
  1653. }
  1654. ADD_PROPERTY(PropertyInfo(Variant::INT, "set_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type"), "set_call_mode", "get_call_mode");
  1655. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1656. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1657. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_type_cache", "_get_type_cache");
  1658. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  1659. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  1660. ADD_PROPERTY(PropertyInfo(Variant::STRING, "property"), "set_property", "get_property");
  1661. ADD_PROPERTY(PropertyInfo(Variant::STRING, "index", PROPERTY_HINT_ENUM), "set_index", "get_index");
  1662. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  1663. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  1664. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  1665. }
  1666. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1667. public:
  1668. VisualScriptPropertyGet::CallMode call_mode;
  1669. NodePath node_path;
  1670. StringName property;
  1671. StringName index;
  1672. VisualScriptPropertyGet *node;
  1673. VisualScriptInstance *instance;
  1674. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1675. switch (call_mode) {
  1676. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1677. Object *object = instance->get_owner_ptr();
  1678. bool valid;
  1679. *p_outputs[0] = object->get(property, &valid);
  1680. if (index != StringName()) {
  1681. *p_outputs[0] = p_outputs[0]->get_named(index);
  1682. }
  1683. if (!valid) {
  1684. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1685. r_error_str = RTR("Invalid index property name.");
  1686. return 0;
  1687. }
  1688. } break;
  1689. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1690. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1691. if (!node) {
  1692. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1693. r_error_str = RTR("Base object is not a Node!");
  1694. return 0;
  1695. }
  1696. Node *another = node->get_node(node_path);
  1697. if (!another) {
  1698. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1699. r_error_str = RTR("Path does not lead Node!");
  1700. return 0;
  1701. }
  1702. bool valid;
  1703. *p_outputs[0] = another->get(property, &valid);
  1704. if (index != StringName()) {
  1705. *p_outputs[0] = p_outputs[0]->get_named(index);
  1706. }
  1707. if (!valid) {
  1708. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1709. r_error_str = vformat(RTR("Invalid index property name '%s' in node %s."), String(property), another->get_name());
  1710. return 0;
  1711. }
  1712. } break;
  1713. default: {
  1714. bool valid;
  1715. Variant v = *p_inputs[0];
  1716. *p_outputs[0] = v.get(property, &valid);
  1717. if (index != StringName()) {
  1718. *p_outputs[0] = p_outputs[0]->get_named(index);
  1719. }
  1720. if (!valid) {
  1721. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1722. r_error_str = RTR("Invalid index property name.");
  1723. }
  1724. };
  1725. }
  1726. return 0;
  1727. }
  1728. };
  1729. VisualScriptNodeInstance *VisualScriptPropertyGet::instance(VisualScriptInstance *p_instance) {
  1730. VisualScriptNodeInstancePropertyGet *instance = memnew(VisualScriptNodeInstancePropertyGet);
  1731. instance->node = this;
  1732. instance->instance = p_instance;
  1733. instance->property = property;
  1734. instance->call_mode = call_mode;
  1735. instance->node_path = base_path;
  1736. instance->index = index;
  1737. return instance;
  1738. }
  1739. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1740. call_mode = CALL_MODE_SELF;
  1741. base_type = "Object";
  1742. basic_type = Variant::NIL;
  1743. type_cache = Variant::NIL;
  1744. }
  1745. template <VisualScriptPropertyGet::CallMode cmode>
  1746. static Ref<VisualScriptNode> create_property_get_node(const String &p_name) {
  1747. Ref<VisualScriptPropertyGet> node;
  1748. node.instance();
  1749. node->set_call_mode(cmode);
  1750. return node;
  1751. }
  1752. //////////////////////////////////////////
  1753. ////////////////EMIT//////////////////////
  1754. //////////////////////////////////////////
  1755. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1756. return 1;
  1757. }
  1758. bool VisualScriptEmitSignal::has_input_sequence_port() const {
  1759. return true;
  1760. }
  1761. int VisualScriptEmitSignal::get_input_value_port_count() const {
  1762. Ref<VisualScript> vs = get_visual_script();
  1763. if (vs.is_valid()) {
  1764. if (!vs->has_custom_signal(name))
  1765. return 0;
  1766. return vs->custom_signal_get_argument_count(name);
  1767. }
  1768. return 0;
  1769. }
  1770. int VisualScriptEmitSignal::get_output_value_port_count() const {
  1771. return 0;
  1772. }
  1773. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1774. return String();
  1775. }
  1776. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const {
  1777. Ref<VisualScript> vs = get_visual_script();
  1778. if (vs.is_valid()) {
  1779. if (!vs->has_custom_signal(name))
  1780. return PropertyInfo();
  1781. return PropertyInfo(vs->custom_signal_get_argument_type(name, p_idx), vs->custom_signal_get_argument_name(name, p_idx));
  1782. }
  1783. return PropertyInfo();
  1784. }
  1785. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const {
  1786. return PropertyInfo();
  1787. }
  1788. String VisualScriptEmitSignal::get_caption() const {
  1789. return "Emit " + String(name);
  1790. }
  1791. void VisualScriptEmitSignal::set_signal(const StringName &p_type) {
  1792. if (name == p_type)
  1793. return;
  1794. name = p_type;
  1795. _change_notify();
  1796. ports_changed_notify();
  1797. }
  1798. StringName VisualScriptEmitSignal::get_signal() const {
  1799. return name;
  1800. }
  1801. void VisualScriptEmitSignal::_validate_property(PropertyInfo &property) const {
  1802. if (property.name == "signal") {
  1803. property.hint = PROPERTY_HINT_ENUM;
  1804. List<StringName> sigs;
  1805. Ref<VisualScript> vs = get_visual_script();
  1806. if (vs.is_valid()) {
  1807. vs->get_custom_signal_list(&sigs);
  1808. }
  1809. String ml;
  1810. for (List<StringName>::Element *E = sigs.front(); E; E = E->next()) {
  1811. if (ml != String())
  1812. ml += ",";
  1813. ml += E->get();
  1814. }
  1815. property.hint_string = ml;
  1816. }
  1817. }
  1818. void VisualScriptEmitSignal::_bind_methods() {
  1819. ClassDB::bind_method(D_METHOD("set_signal", "name"), &VisualScriptEmitSignal::set_signal);
  1820. ClassDB::bind_method(D_METHOD("get_signal"), &VisualScriptEmitSignal::get_signal);
  1821. ADD_PROPERTY(PropertyInfo(Variant::STRING, "signal"), "set_signal", "get_signal");
  1822. }
  1823. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1824. public:
  1825. VisualScriptEmitSignal *node;
  1826. VisualScriptInstance *instance;
  1827. int argcount;
  1828. StringName name;
  1829. //virtual int get_working_memory_size() const { return 0; }
  1830. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1831. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1832. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1833. Object *obj = instance->get_owner_ptr();
  1834. obj->emit_signal(name, p_inputs, argcount);
  1835. return 0;
  1836. }
  1837. };
  1838. VisualScriptNodeInstance *VisualScriptEmitSignal::instance(VisualScriptInstance *p_instance) {
  1839. VisualScriptNodeInstanceEmitSignal *instance = memnew(VisualScriptNodeInstanceEmitSignal);
  1840. instance->node = this;
  1841. instance->instance = p_instance;
  1842. instance->name = name;
  1843. instance->argcount = get_input_value_port_count();
  1844. return instance;
  1845. }
  1846. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1847. }
  1848. static Ref<VisualScriptNode> create_basic_type_call_node(const String &p_name) {
  1849. Vector<String> path = p_name.split("/");
  1850. ERR_FAIL_COND_V(path.size() < 4, Ref<VisualScriptNode>());
  1851. String base_type = path[2];
  1852. String method = path[3];
  1853. Ref<VisualScriptFunctionCall> node;
  1854. node.instance();
  1855. Variant::Type type = Variant::VARIANT_MAX;
  1856. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1857. if (Variant::get_type_name(Variant::Type(i)) == base_type) {
  1858. type = Variant::Type(i);
  1859. break;
  1860. }
  1861. }
  1862. ERR_FAIL_COND_V(type == Variant::VARIANT_MAX, Ref<VisualScriptNode>());
  1863. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1864. node->set_basic_type(type);
  1865. node->set_function(method);
  1866. return node;
  1867. }
  1868. void register_visual_script_func_nodes() {
  1869. VisualScriptLanguage::singleton->add_register_func("functions/call", create_node_generic<VisualScriptFunctionCall>);
  1870. VisualScriptLanguage::singleton->add_register_func("functions/set", create_node_generic<VisualScriptPropertySet>);
  1871. VisualScriptLanguage::singleton->add_register_func("functions/get", create_node_generic<VisualScriptPropertyGet>);
  1872. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1873. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1874. VisualScriptLanguage::singleton->add_register_func("functions/emit_signal", create_node_generic<VisualScriptEmitSignal>);
  1875. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1876. Variant::Type t = Variant::Type(i);
  1877. String type_name = Variant::get_type_name(t);
  1878. Variant::CallError ce;
  1879. Variant vt = Variant::construct(t, NULL, 0, ce);
  1880. List<MethodInfo> ml;
  1881. vt.get_method_list(&ml);
  1882. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  1883. VisualScriptLanguage::singleton->add_register_func("functions/by_type/" + type_name + "/" + E->get().name, create_basic_type_call_node);
  1884. }
  1885. }
  1886. }