visual_script_func_nodes.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "io/resource_loader.h"
  32. #include "os/os.h"
  33. #include "project_settings.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_BASIC_TYPE)
  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_BASIC_TYPE)
  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");
  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. static const char *cname[5] = {
  202. "CallSelf",
  203. "CallNode",
  204. "CallInstance",
  205. "CallBasic",
  206. "CallSingleton"
  207. };
  208. String caption = cname[call_mode];
  209. if (rpc_call_mode) {
  210. caption += " (RPC)";
  211. }
  212. return caption;
  213. }
  214. String VisualScriptFunctionCall::get_text() const {
  215. if (call_mode == CALL_MODE_SELF)
  216. return " " + String(function) + "()";
  217. if (call_mode == CALL_MODE_SINGLETON)
  218. return String(singleton) + ":" + String(function) + "()";
  219. else if (call_mode == CALL_MODE_BASIC_TYPE)
  220. return Variant::get_type_name(basic_type) + "." + String(function) + "()";
  221. else if (call_mode == CALL_MODE_NODE_PATH)
  222. return " [" + String(base_path.simplified()) + "]." + String(function) + "()";
  223. else
  224. return " " + base_type + "." + String(function) + "()";
  225. }
  226. void VisualScriptFunctionCall::set_basic_type(Variant::Type p_type) {
  227. if (basic_type == p_type)
  228. return;
  229. basic_type = p_type;
  230. _change_notify();
  231. ports_changed_notify();
  232. }
  233. Variant::Type VisualScriptFunctionCall::get_basic_type() const {
  234. return basic_type;
  235. }
  236. void VisualScriptFunctionCall::set_base_type(const StringName &p_type) {
  237. if (base_type == p_type)
  238. return;
  239. base_type = p_type;
  240. _change_notify();
  241. ports_changed_notify();
  242. }
  243. StringName VisualScriptFunctionCall::get_base_type() const {
  244. return base_type;
  245. }
  246. void VisualScriptFunctionCall::set_base_script(const String &p_path) {
  247. if (base_script == p_path)
  248. return;
  249. base_script = p_path;
  250. _change_notify();
  251. ports_changed_notify();
  252. }
  253. String VisualScriptFunctionCall::get_base_script() const {
  254. return base_script;
  255. }
  256. void VisualScriptFunctionCall::set_singleton(const StringName &p_type) {
  257. if (singleton == p_type)
  258. return;
  259. singleton = p_type;
  260. Object *obj = ProjectSettings::get_singleton()->get_singleton_object(singleton);
  261. if (obj) {
  262. base_type = obj->get_class();
  263. }
  264. _change_notify();
  265. ports_changed_notify();
  266. }
  267. StringName VisualScriptFunctionCall::get_singleton() const {
  268. return singleton;
  269. }
  270. void VisualScriptFunctionCall::_update_method_cache() {
  271. StringName type;
  272. Ref<Script> script;
  273. if (call_mode == CALL_MODE_NODE_PATH) {
  274. Node *node = _get_base_node();
  275. if (node) {
  276. type = node->get_class();
  277. base_type = type; //cache, too
  278. script = node->get_script();
  279. }
  280. } else if (call_mode == CALL_MODE_SELF) {
  281. if (get_visual_script().is_valid()) {
  282. type = get_visual_script()->get_instance_base_type();
  283. base_type = type; //cache, too
  284. script = get_visual_script();
  285. }
  286. } else if (call_mode == CALL_MODE_SINGLETON) {
  287. Object *obj = ProjectSettings::get_singleton()->get_singleton_object(singleton);
  288. if (obj) {
  289. type = obj->get_class();
  290. script = obj->get_script();
  291. }
  292. } else if (call_mode == CALL_MODE_INSTANCE) {
  293. type = base_type;
  294. if (base_script != String()) {
  295. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  296. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  297. }
  298. if (ResourceCache::has(base_script)) {
  299. script = Ref<Resource>(ResourceCache::get(base_script));
  300. } else {
  301. return;
  302. }
  303. }
  304. }
  305. //print_line("BASE: "+String(type)+" FUNC: "+String(function));
  306. MethodBind *mb = ClassDB::get_method(type, function);
  307. if (mb) {
  308. use_default_args = mb->get_default_argument_count();
  309. method_cache = MethodInfo();
  310. for (int i = 0; i < mb->get_argument_count(); i++) {
  311. #ifdef DEBUG_METHODS_ENABLED
  312. method_cache.arguments.push_back(mb->get_argument_info(i));
  313. #else
  314. method_cache.arguments.push_back(PropertyInfo());
  315. #endif
  316. }
  317. if (mb->is_const()) {
  318. method_cache.flags |= METHOD_FLAG_CONST;
  319. }
  320. #ifdef DEBUG_METHODS_ENABLED
  321. method_cache.return_val = mb->get_argument_info(-1);
  322. #endif
  323. if (mb->is_vararg()) {
  324. //for vararg just give it 10 arguments (should be enough for most use cases)
  325. for (int i = 0; i < 10; i++) {
  326. method_cache.arguments.push_back(PropertyInfo(Variant::NIL, "arg" + itos(i)));
  327. use_default_args++;
  328. }
  329. }
  330. } else if (script.is_valid() && script->has_method(function)) {
  331. method_cache = script->get_method_info(function);
  332. use_default_args = method_cache.default_arguments.size();
  333. }
  334. }
  335. void VisualScriptFunctionCall::set_function(const StringName &p_type) {
  336. if (function == p_type)
  337. return;
  338. function = p_type;
  339. if (call_mode == CALL_MODE_BASIC_TYPE) {
  340. use_default_args = Variant::get_method_default_arguments(basic_type, function).size();
  341. } else {
  342. //update all caches
  343. _update_method_cache();
  344. }
  345. _change_notify();
  346. ports_changed_notify();
  347. }
  348. StringName VisualScriptFunctionCall::get_function() const {
  349. return function;
  350. }
  351. void VisualScriptFunctionCall::set_base_path(const NodePath &p_type) {
  352. if (base_path == p_type)
  353. return;
  354. base_path = p_type;
  355. _change_notify();
  356. ports_changed_notify();
  357. }
  358. NodePath VisualScriptFunctionCall::get_base_path() const {
  359. return base_path;
  360. }
  361. void VisualScriptFunctionCall::set_call_mode(CallMode p_mode) {
  362. if (call_mode == p_mode)
  363. return;
  364. call_mode = p_mode;
  365. _change_notify();
  366. ports_changed_notify();
  367. }
  368. VisualScriptFunctionCall::CallMode VisualScriptFunctionCall::get_call_mode() const {
  369. return call_mode;
  370. }
  371. void VisualScriptFunctionCall::set_use_default_args(int p_amount) {
  372. if (use_default_args == p_amount)
  373. return;
  374. use_default_args = p_amount;
  375. ports_changed_notify();
  376. }
  377. void VisualScriptFunctionCall::set_rpc_call_mode(VisualScriptFunctionCall::RPCCallMode p_mode) {
  378. if (rpc_call_mode == p_mode)
  379. return;
  380. rpc_call_mode = p_mode;
  381. ports_changed_notify();
  382. _change_notify();
  383. }
  384. VisualScriptFunctionCall::RPCCallMode VisualScriptFunctionCall::get_rpc_call_mode() const {
  385. return rpc_call_mode;
  386. }
  387. int VisualScriptFunctionCall::get_use_default_args() const {
  388. return use_default_args;
  389. }
  390. void VisualScriptFunctionCall::set_validate(bool p_amount) {
  391. validate = p_amount;
  392. }
  393. bool VisualScriptFunctionCall::get_validate() const {
  394. return validate;
  395. }
  396. void VisualScriptFunctionCall::_set_argument_cache(const Dictionary &p_cache) {
  397. //so everything works in case all else fails
  398. method_cache = MethodInfo::from_dict(p_cache);
  399. }
  400. Dictionary VisualScriptFunctionCall::_get_argument_cache() const {
  401. return method_cache;
  402. }
  403. void VisualScriptFunctionCall::_validate_property(PropertyInfo &property) const {
  404. if (property.name == "base_type") {
  405. if (call_mode != CALL_MODE_INSTANCE) {
  406. property.usage = PROPERTY_USAGE_NOEDITOR;
  407. }
  408. }
  409. if (property.name == "base_script") {
  410. if (call_mode != CALL_MODE_INSTANCE) {
  411. property.usage = 0;
  412. }
  413. }
  414. if (property.name == "basic_type") {
  415. if (call_mode != CALL_MODE_BASIC_TYPE) {
  416. property.usage = 0;
  417. }
  418. }
  419. if (property.name == "singleton") {
  420. if (call_mode != CALL_MODE_SINGLETON) {
  421. property.usage = 0;
  422. } else {
  423. List<ProjectSettings::Singleton> names;
  424. ProjectSettings::get_singleton()->get_singletons(&names);
  425. property.hint = PROPERTY_HINT_ENUM;
  426. String sl;
  427. for (List<ProjectSettings::Singleton>::Element *E = names.front(); E; E = E->next()) {
  428. if (sl != String())
  429. sl += ",";
  430. sl += E->get().name;
  431. }
  432. property.hint_string = sl;
  433. }
  434. }
  435. if (property.name == "node_path") {
  436. if (call_mode != CALL_MODE_NODE_PATH) {
  437. property.usage = 0;
  438. } else {
  439. Node *bnode = _get_base_node();
  440. if (bnode) {
  441. property.hint_string = bnode->get_path(); //convert to loong string
  442. } else {
  443. }
  444. }
  445. }
  446. if (property.name == "function") {
  447. if (call_mode == CALL_MODE_BASIC_TYPE) {
  448. property.hint = PROPERTY_HINT_METHOD_OF_VARIANT_TYPE;
  449. property.hint_string = Variant::get_type_name(basic_type);
  450. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  451. property.hint = PROPERTY_HINT_METHOD_OF_SCRIPT;
  452. property.hint_string = itos(get_visual_script()->get_instance_id());
  453. } else if (call_mode == CALL_MODE_SINGLETON) {
  454. Object *obj = ProjectSettings::get_singleton()->get_singleton_object(singleton);
  455. if (obj) {
  456. property.hint = PROPERTY_HINT_METHOD_OF_INSTANCE;
  457. property.hint_string = itos(obj->get_instance_id());
  458. } else {
  459. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  460. property.hint_string = base_type; //should be cached
  461. }
  462. } else if (call_mode == CALL_MODE_INSTANCE) {
  463. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  464. property.hint_string = base_type;
  465. if (base_script != String()) {
  466. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  467. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  468. }
  469. if (ResourceCache::has(base_script)) {
  470. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  471. if (script.is_valid()) {
  472. property.hint = PROPERTY_HINT_METHOD_OF_SCRIPT;
  473. property.hint_string = itos(script->get_instance_id());
  474. }
  475. }
  476. }
  477. } else if (call_mode == CALL_MODE_NODE_PATH) {
  478. Node *node = _get_base_node();
  479. if (node) {
  480. property.hint = PROPERTY_HINT_METHOD_OF_INSTANCE;
  481. property.hint_string = itos(node->get_instance_id());
  482. } else {
  483. property.hint = PROPERTY_HINT_METHOD_OF_BASE_TYPE;
  484. property.hint_string = get_base_type();
  485. }
  486. }
  487. }
  488. if (property.name == "use_default_args") {
  489. property.hint = PROPERTY_HINT_RANGE;
  490. int mc = 0;
  491. if (call_mode == CALL_MODE_BASIC_TYPE) {
  492. mc = Variant::get_method_default_arguments(basic_type, function).size();
  493. } else {
  494. MethodBind *mb = ClassDB::get_method(_get_base_type(), function);
  495. if (mb) {
  496. mc = mb->get_default_argument_count();
  497. }
  498. }
  499. if (mc == 0) {
  500. property.usage = 0; //do not show
  501. } else {
  502. property.hint_string = "0," + itos(mc) + ",1";
  503. }
  504. }
  505. if (property.name == "rpc_call_mode") {
  506. if (call_mode == CALL_MODE_BASIC_TYPE) {
  507. property.usage = 0;
  508. }
  509. }
  510. }
  511. void VisualScriptFunctionCall::_bind_methods() {
  512. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptFunctionCall::set_base_type);
  513. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptFunctionCall::get_base_type);
  514. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptFunctionCall::set_base_script);
  515. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptFunctionCall::get_base_script);
  516. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptFunctionCall::set_basic_type);
  517. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptFunctionCall::get_basic_type);
  518. ClassDB::bind_method(D_METHOD("set_singleton", "singleton"), &VisualScriptFunctionCall::set_singleton);
  519. ClassDB::bind_method(D_METHOD("get_singleton"), &VisualScriptFunctionCall::get_singleton);
  520. ClassDB::bind_method(D_METHOD("set_function", "function"), &VisualScriptFunctionCall::set_function);
  521. ClassDB::bind_method(D_METHOD("get_function"), &VisualScriptFunctionCall::get_function);
  522. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptFunctionCall::set_call_mode);
  523. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptFunctionCall::get_call_mode);
  524. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptFunctionCall::set_base_path);
  525. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptFunctionCall::get_base_path);
  526. ClassDB::bind_method(D_METHOD("set_use_default_args", "amount"), &VisualScriptFunctionCall::set_use_default_args);
  527. ClassDB::bind_method(D_METHOD("get_use_default_args"), &VisualScriptFunctionCall::get_use_default_args);
  528. ClassDB::bind_method(D_METHOD("_set_argument_cache", "argument_cache"), &VisualScriptFunctionCall::_set_argument_cache);
  529. ClassDB::bind_method(D_METHOD("_get_argument_cache"), &VisualScriptFunctionCall::_get_argument_cache);
  530. ClassDB::bind_method(D_METHOD("set_rpc_call_mode", "mode"), &VisualScriptFunctionCall::set_rpc_call_mode);
  531. ClassDB::bind_method(D_METHOD("get_rpc_call_mode"), &VisualScriptFunctionCall::get_rpc_call_mode);
  532. ClassDB::bind_method(D_METHOD("set_validate", "enable"), &VisualScriptFunctionCall::set_validate);
  533. ClassDB::bind_method(D_METHOD("get_validate"), &VisualScriptFunctionCall::get_validate);
  534. String bt;
  535. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  536. if (i > 0)
  537. bt += ",";
  538. bt += Variant::get_type_name(Variant::Type(i));
  539. }
  540. List<String> script_extensions;
  541. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  542. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  543. }
  544. String script_ext_hint;
  545. for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
  546. if (script_ext_hint != String())
  547. script_ext_hint += ",";
  548. script_ext_hint += "*." + E->get();
  549. }
  550. ADD_PROPERTY(PropertyInfo(Variant::INT, "call_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type,Singleton"), "set_call_mode", "get_call_mode");
  551. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  552. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  553. ADD_PROPERTY(PropertyInfo(Variant::STRING, "singleton"), "set_singleton", "get_singleton");
  554. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  555. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  556. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "argument_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_argument_cache", "_get_argument_cache");
  557. ADD_PROPERTY(PropertyInfo(Variant::STRING, "function"), "set_function", "get_function"); //when set, if loaded properly, will override argument count.
  558. ADD_PROPERTY(PropertyInfo(Variant::INT, "use_default_args"), "set_use_default_args", "get_use_default_args");
  559. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "validate"), "set_validate", "get_validate");
  560. 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.
  561. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  562. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  563. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  564. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  565. BIND_ENUM_CONSTANT(CALL_MODE_SINGLETON);
  566. BIND_ENUM_CONSTANT(RPC_DISABLED);
  567. BIND_ENUM_CONSTANT(RPC_RELIABLE);
  568. BIND_ENUM_CONSTANT(RPC_UNRELIABLE);
  569. BIND_ENUM_CONSTANT(RPC_RELIABLE_TO_ID);
  570. BIND_ENUM_CONSTANT(RPC_UNRELIABLE_TO_ID);
  571. }
  572. class VisualScriptNodeInstanceFunctionCall : public VisualScriptNodeInstance {
  573. public:
  574. VisualScriptFunctionCall::CallMode call_mode;
  575. NodePath node_path;
  576. int input_args;
  577. bool validate;
  578. bool returns;
  579. VisualScriptFunctionCall::RPCCallMode rpc_mode;
  580. StringName function;
  581. StringName singleton;
  582. VisualScriptFunctionCall *node;
  583. VisualScriptInstance *instance;
  584. //virtual int get_working_memory_size() const { return 0; }
  585. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  586. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  587. _FORCE_INLINE_ bool call_rpc(Object *p_base, const Variant **p_args, int p_argcount) {
  588. if (!p_base)
  589. return false;
  590. Node *node = Object::cast_to<Node>(p_base);
  591. if (!node)
  592. return false;
  593. int to_id = 0;
  594. bool reliable = true;
  595. if (rpc_mode >= VisualScriptFunctionCall::RPC_RELIABLE_TO_ID) {
  596. to_id = *p_args[0];
  597. p_args += 1;
  598. p_argcount -= 1;
  599. if (rpc_mode == VisualScriptFunctionCall::RPC_UNRELIABLE_TO_ID) {
  600. reliable = false;
  601. }
  602. } else if (rpc_mode == VisualScriptFunctionCall::RPC_UNRELIABLE) {
  603. reliable = false;
  604. }
  605. node->rpcp(to_id, !reliable, function, p_args, p_argcount);
  606. return true;
  607. }
  608. 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) {
  609. switch (call_mode) {
  610. case VisualScriptFunctionCall::CALL_MODE_SELF: {
  611. Object *object = instance->get_owner_ptr();
  612. if (rpc_mode) {
  613. call_rpc(object, p_inputs, input_args);
  614. } else if (returns) {
  615. *p_outputs[0] = object->call(function, p_inputs, input_args, r_error);
  616. } else {
  617. object->call(function, p_inputs, input_args, r_error);
  618. }
  619. } break;
  620. case VisualScriptFunctionCall::CALL_MODE_NODE_PATH: {
  621. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  622. if (!node) {
  623. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  624. r_error_str = "Base object is not a Node!";
  625. return 0;
  626. }
  627. Node *another = node->get_node(node_path);
  628. if (!another) {
  629. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  630. r_error_str = "Path does not lead Node!";
  631. return 0;
  632. }
  633. if (rpc_mode) {
  634. call_rpc(node, p_inputs, input_args);
  635. } else if (returns) {
  636. *p_outputs[0] = another->call(function, p_inputs, input_args, r_error);
  637. } else {
  638. another->call(function, p_inputs, input_args, r_error);
  639. }
  640. } break;
  641. case VisualScriptFunctionCall::CALL_MODE_INSTANCE:
  642. case VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE: {
  643. Variant v = *p_inputs[0];
  644. if (rpc_mode) {
  645. Object *obj = v;
  646. if (obj) {
  647. call_rpc(obj, p_inputs + 1, input_args - 1);
  648. }
  649. } else if (returns) {
  650. if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) {
  651. *p_outputs[1] = v.call(function, p_inputs + 1, input_args, r_error);
  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 = ProjectSettings::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");
  804. } else {
  805. return PropertyInfo();
  806. }
  807. }
  808. String VisualScriptPropertySet::get_caption() const {
  809. static const char *cname[4] = {
  810. "Self",
  811. "Node",
  812. "Instance",
  813. "Basic"
  814. };
  815. static const char *opname[ASSIGN_OP_MAX] = {
  816. "Set", "Add", "Sub", "Mul", "Div", "Mod", "ShiftLeft", "ShiftRight", "BitAnd", "BitOr", "BitXor"
  817. };
  818. return String(cname[call_mode]) + opname[assign_op];
  819. }
  820. String VisualScriptPropertySet::get_text() const {
  821. String prop;
  822. if (call_mode == CALL_MODE_BASIC_TYPE)
  823. prop = Variant::get_type_name(basic_type) + "." + property;
  824. else if (call_mode == CALL_MODE_NODE_PATH)
  825. prop = String(base_path) + ":" + property;
  826. else if (call_mode == CALL_MODE_SELF)
  827. prop = property;
  828. else if (call_mode == CALL_MODE_INSTANCE)
  829. prop = String(base_type) + ":" + property;
  830. if (index != StringName()) {
  831. prop += "." + String(index);
  832. }
  833. return prop;
  834. }
  835. void VisualScriptPropertySet::_update_base_type() {
  836. //cache it because this information may not be available on load
  837. if (call_mode == CALL_MODE_NODE_PATH) {
  838. Node *node = _get_base_node();
  839. if (node) {
  840. base_type = node->get_class();
  841. }
  842. } else if (call_mode == CALL_MODE_SELF) {
  843. if (get_visual_script().is_valid()) {
  844. base_type = get_visual_script()->get_instance_base_type();
  845. }
  846. }
  847. }
  848. void VisualScriptPropertySet::set_basic_type(Variant::Type p_type) {
  849. if (basic_type == p_type)
  850. return;
  851. basic_type = p_type;
  852. _change_notify();
  853. _update_base_type();
  854. ports_changed_notify();
  855. }
  856. Variant::Type VisualScriptPropertySet::get_basic_type() const {
  857. return basic_type;
  858. }
  859. void VisualScriptPropertySet::set_base_type(const StringName &p_type) {
  860. if (base_type == p_type)
  861. return;
  862. base_type = p_type;
  863. _change_notify();
  864. ports_changed_notify();
  865. }
  866. StringName VisualScriptPropertySet::get_base_type() const {
  867. return base_type;
  868. }
  869. void VisualScriptPropertySet::set_base_script(const String &p_path) {
  870. if (base_script == p_path)
  871. return;
  872. base_script = p_path;
  873. _change_notify();
  874. ports_changed_notify();
  875. }
  876. String VisualScriptPropertySet::get_base_script() const {
  877. return base_script;
  878. }
  879. void VisualScriptPropertySet::_update_cache() {
  880. if (!Object::cast_to<SceneTree>(OS::get_singleton()->get_main_loop()))
  881. return;
  882. if (!Engine::get_singleton()->is_editor_hint()) //only update cache if editor exists, it's pointless otherwise
  883. return;
  884. if (call_mode == CALL_MODE_BASIC_TYPE) {
  885. //not super efficient..
  886. Variant v;
  887. Variant::CallError ce;
  888. v = Variant::construct(basic_type, NULL, 0, ce);
  889. List<PropertyInfo> pinfo;
  890. v.get_property_list(&pinfo);
  891. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  892. if (E->get().name == property) {
  893. type_cache = E->get();
  894. }
  895. }
  896. } else {
  897. StringName type;
  898. Ref<Script> script;
  899. Node *node = NULL;
  900. if (call_mode == CALL_MODE_NODE_PATH) {
  901. node = _get_base_node();
  902. if (node) {
  903. type = node->get_class();
  904. base_type = type; //cache, too
  905. script = node->get_script();
  906. }
  907. } else if (call_mode == CALL_MODE_SELF) {
  908. if (get_visual_script().is_valid()) {
  909. type = get_visual_script()->get_instance_base_type();
  910. base_type = type; //cache, too
  911. script = get_visual_script();
  912. }
  913. } else if (call_mode == CALL_MODE_INSTANCE) {
  914. type = base_type;
  915. if (base_script != String()) {
  916. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  917. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  918. }
  919. if (ResourceCache::has(base_script)) {
  920. script = Ref<Resource>(ResourceCache::get(base_script));
  921. } else {
  922. return;
  923. }
  924. }
  925. }
  926. List<PropertyInfo> pinfo;
  927. if (node) {
  928. node->get_property_list(&pinfo);
  929. } else {
  930. ClassDB::get_property_list(type, &pinfo);
  931. }
  932. if (script.is_valid()) {
  933. script->get_script_property_list(&pinfo);
  934. }
  935. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  936. if (E->get().name == property) {
  937. type_cache = E->get();
  938. return;
  939. }
  940. }
  941. }
  942. }
  943. void VisualScriptPropertySet::set_property(const StringName &p_type) {
  944. if (property == p_type)
  945. return;
  946. property = p_type;
  947. index = StringName();
  948. _update_cache();
  949. _change_notify();
  950. ports_changed_notify();
  951. }
  952. StringName VisualScriptPropertySet::get_property() const {
  953. return property;
  954. }
  955. void VisualScriptPropertySet::set_base_path(const NodePath &p_type) {
  956. if (base_path == p_type)
  957. return;
  958. base_path = p_type;
  959. _update_base_type();
  960. _change_notify();
  961. ports_changed_notify();
  962. }
  963. NodePath VisualScriptPropertySet::get_base_path() const {
  964. return base_path;
  965. }
  966. void VisualScriptPropertySet::set_call_mode(CallMode p_mode) {
  967. if (call_mode == p_mode)
  968. return;
  969. call_mode = p_mode;
  970. _update_base_type();
  971. _change_notify();
  972. ports_changed_notify();
  973. }
  974. VisualScriptPropertySet::CallMode VisualScriptPropertySet::get_call_mode() const {
  975. return call_mode;
  976. }
  977. void VisualScriptPropertySet::_set_type_cache(const Dictionary &p_type) {
  978. type_cache = PropertyInfo::from_dict(p_type);
  979. }
  980. Dictionary VisualScriptPropertySet::_get_type_cache() const {
  981. return type_cache;
  982. }
  983. void VisualScriptPropertySet::set_index(const StringName &p_type) {
  984. if (index == p_type)
  985. return;
  986. index = p_type;
  987. _update_cache();
  988. _change_notify();
  989. ports_changed_notify();
  990. }
  991. StringName VisualScriptPropertySet::get_index() const {
  992. return index;
  993. }
  994. void VisualScriptPropertySet::set_assign_op(AssignOp p_op) {
  995. ERR_FAIL_INDEX(p_op, ASSIGN_OP_MAX);
  996. if (assign_op == p_op)
  997. return;
  998. assign_op = p_op;
  999. _update_cache();
  1000. _change_notify();
  1001. ports_changed_notify();
  1002. }
  1003. VisualScriptPropertySet::AssignOp VisualScriptPropertySet::get_assign_op() const {
  1004. return assign_op;
  1005. }
  1006. void VisualScriptPropertySet::_validate_property(PropertyInfo &property) const {
  1007. if (property.name == "base_type") {
  1008. if (call_mode != CALL_MODE_INSTANCE) {
  1009. property.usage = PROPERTY_USAGE_NOEDITOR;
  1010. }
  1011. }
  1012. if (property.name == "base_script") {
  1013. if (call_mode != CALL_MODE_INSTANCE) {
  1014. property.usage = 0;
  1015. }
  1016. }
  1017. if (property.name == "basic_type") {
  1018. if (call_mode != CALL_MODE_BASIC_TYPE) {
  1019. property.usage = 0;
  1020. }
  1021. }
  1022. if (property.name == "node_path") {
  1023. if (call_mode != CALL_MODE_NODE_PATH) {
  1024. property.usage = 0;
  1025. } else {
  1026. Node *bnode = _get_base_node();
  1027. if (bnode) {
  1028. property.hint_string = bnode->get_path(); //convert to loong string
  1029. } else {
  1030. }
  1031. }
  1032. }
  1033. if (property.name == "property") {
  1034. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1035. property.hint = PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1036. property.hint_string = Variant::get_type_name(basic_type);
  1037. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1038. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1039. property.hint_string = itos(get_visual_script()->get_instance_id());
  1040. } else if (call_mode == CALL_MODE_INSTANCE) {
  1041. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1042. property.hint_string = base_type;
  1043. if (base_script != String()) {
  1044. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1045. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1046. }
  1047. if (ResourceCache::has(base_script)) {
  1048. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  1049. if (script.is_valid()) {
  1050. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1051. property.hint_string = itos(script->get_instance_id());
  1052. }
  1053. }
  1054. }
  1055. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1056. Node *node = _get_base_node();
  1057. if (node) {
  1058. property.hint = PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1059. property.hint_string = itos(node->get_instance_id());
  1060. } else {
  1061. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1062. property.hint_string = get_base_type();
  1063. }
  1064. }
  1065. }
  1066. if (property.name == "index") {
  1067. Variant::CallError ce;
  1068. Variant v = Variant::construct(type_cache.type, NULL, 0, ce);
  1069. List<PropertyInfo> plist;
  1070. v.get_property_list(&plist);
  1071. String options = "";
  1072. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1073. options += "," + E->get().name;
  1074. }
  1075. property.hint = PROPERTY_HINT_ENUM;
  1076. property.hint_string = options;
  1077. property.type = Variant::STRING;
  1078. if (options == "")
  1079. property.usage = 0; //hide if type has no usable index
  1080. }
  1081. }
  1082. void VisualScriptPropertySet::_bind_methods() {
  1083. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptPropertySet::set_base_type);
  1084. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptPropertySet::get_base_type);
  1085. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptPropertySet::set_base_script);
  1086. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptPropertySet::get_base_script);
  1087. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptPropertySet::set_basic_type);
  1088. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptPropertySet::get_basic_type);
  1089. ClassDB::bind_method(D_METHOD("_set_type_cache", "type_cache"), &VisualScriptPropertySet::_set_type_cache);
  1090. ClassDB::bind_method(D_METHOD("_get_type_cache"), &VisualScriptPropertySet::_get_type_cache);
  1091. ClassDB::bind_method(D_METHOD("set_property", "property"), &VisualScriptPropertySet::set_property);
  1092. ClassDB::bind_method(D_METHOD("get_property"), &VisualScriptPropertySet::get_property);
  1093. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptPropertySet::set_call_mode);
  1094. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptPropertySet::get_call_mode);
  1095. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptPropertySet::set_base_path);
  1096. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptPropertySet::get_base_path);
  1097. ClassDB::bind_method(D_METHOD("set_index", "index"), &VisualScriptPropertySet::set_index);
  1098. ClassDB::bind_method(D_METHOD("get_index"), &VisualScriptPropertySet::get_index);
  1099. ClassDB::bind_method(D_METHOD("set_assign_op", "assign_op"), &VisualScriptPropertySet::set_assign_op);
  1100. ClassDB::bind_method(D_METHOD("get_assign_op"), &VisualScriptPropertySet::get_assign_op);
  1101. String bt;
  1102. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1103. if (i > 0)
  1104. bt += ",";
  1105. bt += Variant::get_type_name(Variant::Type(i));
  1106. }
  1107. List<String> script_extensions;
  1108. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1109. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1110. }
  1111. String script_ext_hint;
  1112. for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
  1113. if (script_ext_hint != String())
  1114. script_ext_hint += ",";
  1115. script_ext_hint += "*." + E->get();
  1116. }
  1117. ADD_PROPERTY(PropertyInfo(Variant::INT, "set_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type"), "set_call_mode", "get_call_mode");
  1118. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1119. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1120. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_type_cache", "_get_type_cache");
  1121. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  1122. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  1123. ADD_PROPERTY(PropertyInfo(Variant::STRING, "property"), "set_property", "get_property");
  1124. ADD_PROPERTY(PropertyInfo(Variant::STRING, "index"), "set_index", "get_index");
  1125. 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");
  1126. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  1127. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  1128. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  1129. BIND_ENUM_CONSTANT(CALL_MODE_BASIC_TYPE);
  1130. BIND_ENUM_CONSTANT(ASSIGN_OP_NONE);
  1131. BIND_ENUM_CONSTANT(ASSIGN_OP_ADD);
  1132. BIND_ENUM_CONSTANT(ASSIGN_OP_SUB);
  1133. BIND_ENUM_CONSTANT(ASSIGN_OP_MUL);
  1134. BIND_ENUM_CONSTANT(ASSIGN_OP_DIV);
  1135. BIND_ENUM_CONSTANT(ASSIGN_OP_MOD);
  1136. BIND_ENUM_CONSTANT(ASSIGN_OP_SHIFT_LEFT);
  1137. BIND_ENUM_CONSTANT(ASSIGN_OP_SHIFT_RIGHT);
  1138. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_AND);
  1139. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_OR);
  1140. BIND_ENUM_CONSTANT(ASSIGN_OP_BIT_XOR);
  1141. }
  1142. class VisualScriptNodeInstancePropertySet : public VisualScriptNodeInstance {
  1143. public:
  1144. VisualScriptPropertySet::CallMode call_mode;
  1145. NodePath node_path;
  1146. StringName property;
  1147. VisualScriptPropertySet *node;
  1148. VisualScriptInstance *instance;
  1149. VisualScriptPropertySet::AssignOp assign_op;
  1150. StringName index;
  1151. bool needs_get;
  1152. //virtual int get_working_memory_size() const { return 0; }
  1153. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1154. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1155. _FORCE_INLINE_ void _process_get(Variant &source, const Variant &p_argument, bool &valid) {
  1156. if (index != StringName() && assign_op == VisualScriptPropertySet::ASSIGN_OP_NONE) {
  1157. source.set_named(index, p_argument, &valid);
  1158. } else {
  1159. Variant value;
  1160. if (index != StringName()) {
  1161. value = source.get_named(index, &valid);
  1162. } else {
  1163. value = source;
  1164. }
  1165. switch (assign_op) {
  1166. case VisualScriptPropertySet::ASSIGN_OP_NONE: {
  1167. //should never get here
  1168. } break;
  1169. case VisualScriptPropertySet::ASSIGN_OP_ADD: {
  1170. value = Variant::evaluate(Variant::OP_ADD, value, p_argument);
  1171. } break;
  1172. case VisualScriptPropertySet::ASSIGN_OP_SUB: {
  1173. value = Variant::evaluate(Variant::OP_SUBTRACT, value, p_argument);
  1174. } break;
  1175. case VisualScriptPropertySet::ASSIGN_OP_MUL: {
  1176. value = Variant::evaluate(Variant::OP_MULTIPLY, value, p_argument);
  1177. } break;
  1178. case VisualScriptPropertySet::ASSIGN_OP_DIV: {
  1179. value = Variant::evaluate(Variant::OP_DIVIDE, value, p_argument);
  1180. } break;
  1181. case VisualScriptPropertySet::ASSIGN_OP_MOD: {
  1182. value = Variant::evaluate(Variant::OP_MODULE, value, p_argument);
  1183. } break;
  1184. case VisualScriptPropertySet::ASSIGN_OP_SHIFT_LEFT: {
  1185. value = Variant::evaluate(Variant::OP_SHIFT_LEFT, value, p_argument);
  1186. } break;
  1187. case VisualScriptPropertySet::ASSIGN_OP_SHIFT_RIGHT: {
  1188. value = Variant::evaluate(Variant::OP_SHIFT_RIGHT, value, p_argument);
  1189. } break;
  1190. case VisualScriptPropertySet::ASSIGN_OP_BIT_AND: {
  1191. value = Variant::evaluate(Variant::OP_BIT_AND, value, p_argument);
  1192. } break;
  1193. case VisualScriptPropertySet::ASSIGN_OP_BIT_OR: {
  1194. value = Variant::evaluate(Variant::OP_BIT_OR, value, p_argument);
  1195. } break;
  1196. case VisualScriptPropertySet::ASSIGN_OP_BIT_XOR: {
  1197. value = Variant::evaluate(Variant::OP_BIT_XOR, value, p_argument);
  1198. } break;
  1199. default: {}
  1200. }
  1201. if (index != StringName()) {
  1202. source.set_named(index, value, &valid);
  1203. } else {
  1204. source = value;
  1205. }
  1206. }
  1207. }
  1208. 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) {
  1209. switch (call_mode) {
  1210. case VisualScriptPropertySet::CALL_MODE_SELF: {
  1211. Object *object = instance->get_owner_ptr();
  1212. bool valid;
  1213. if (needs_get) {
  1214. Variant value = object->get(property, &valid);
  1215. _process_get(value, *p_inputs[0], valid);
  1216. object->set(property, value, &valid);
  1217. } else {
  1218. object->set(property, *p_inputs[0], &valid);
  1219. }
  1220. if (!valid) {
  1221. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1222. r_error_str = "Invalid set value '" + String(*p_inputs[0]) + "' on property '" + String(property) + "' of type " + object->get_class();
  1223. }
  1224. } break;
  1225. case VisualScriptPropertySet::CALL_MODE_NODE_PATH: {
  1226. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1227. if (!node) {
  1228. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1229. r_error_str = "Base object is not a Node!";
  1230. return 0;
  1231. }
  1232. Node *another = node->get_node(node_path);
  1233. if (!another) {
  1234. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1235. r_error_str = "Path does not lead Node!";
  1236. return 0;
  1237. }
  1238. bool valid;
  1239. if (needs_get) {
  1240. Variant value = another->get(property, &valid);
  1241. _process_get(value, *p_inputs[0], valid);
  1242. another->set(property, value, &valid);
  1243. } else {
  1244. another->set(property, *p_inputs[0], &valid);
  1245. }
  1246. if (!valid) {
  1247. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1248. r_error_str = "Invalid set value '" + String(*p_inputs[0]) + "' on property '" + String(property) + "' of type " + another->get_class();
  1249. }
  1250. } break;
  1251. case VisualScriptPropertySet::CALL_MODE_INSTANCE:
  1252. case VisualScriptPropertySet::CALL_MODE_BASIC_TYPE: {
  1253. Variant v = *p_inputs[0];
  1254. bool valid;
  1255. if (needs_get) {
  1256. Variant value = v.get_named(property, &valid);
  1257. _process_get(value, *p_inputs[1], valid);
  1258. v.set_named(property, value, &valid);
  1259. } else {
  1260. v.set_named(property, *p_inputs[1], &valid);
  1261. }
  1262. if (!valid) {
  1263. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1264. 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());
  1265. }
  1266. *p_outputs[0] = v;
  1267. } break;
  1268. }
  1269. return 0;
  1270. }
  1271. };
  1272. VisualScriptNodeInstance *VisualScriptPropertySet::instance(VisualScriptInstance *p_instance) {
  1273. VisualScriptNodeInstancePropertySet *instance = memnew(VisualScriptNodeInstancePropertySet);
  1274. instance->node = this;
  1275. instance->instance = p_instance;
  1276. instance->property = property;
  1277. instance->call_mode = call_mode;
  1278. instance->node_path = base_path;
  1279. instance->assign_op = assign_op;
  1280. instance->index = index;
  1281. instance->needs_get = index != StringName() || assign_op != ASSIGN_OP_NONE;
  1282. return instance;
  1283. }
  1284. VisualScriptPropertySet::TypeGuess VisualScriptPropertySet::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  1285. if (p_output == 0 && call_mode == CALL_MODE_INSTANCE) {
  1286. return p_inputs[0];
  1287. }
  1288. return VisualScriptNode::guess_output_type(p_inputs, p_output);
  1289. }
  1290. VisualScriptPropertySet::VisualScriptPropertySet() {
  1291. assign_op = ASSIGN_OP_NONE;
  1292. call_mode = CALL_MODE_SELF;
  1293. base_type = "Object";
  1294. basic_type = Variant::NIL;
  1295. }
  1296. template <VisualScriptPropertySet::CallMode cmode>
  1297. static Ref<VisualScriptNode> create_property_set_node(const String &p_name) {
  1298. Ref<VisualScriptPropertySet> node;
  1299. node.instance();
  1300. node->set_call_mode(cmode);
  1301. return node;
  1302. }
  1303. //////////////////////////////////////////
  1304. ////////////////GET//////////////////////
  1305. //////////////////////////////////////////
  1306. int VisualScriptPropertyGet::get_output_sequence_port_count() const {
  1307. return 0; // (call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?0:1;
  1308. }
  1309. bool VisualScriptPropertyGet::has_input_sequence_port() const {
  1310. return false; //(call_mode==CALL_MODE_SELF || call_mode==CALL_MODE_NODE_PATH)?false:true;
  1311. }
  1312. void VisualScriptPropertyGet::_update_base_type() {
  1313. //cache it because this information may not be available on load
  1314. if (call_mode == CALL_MODE_NODE_PATH) {
  1315. Node *node = _get_base_node();
  1316. if (node) {
  1317. base_type = node->get_class();
  1318. }
  1319. } else if (call_mode == CALL_MODE_SELF) {
  1320. if (get_visual_script().is_valid()) {
  1321. base_type = get_visual_script()->get_instance_base_type();
  1322. }
  1323. }
  1324. }
  1325. Node *VisualScriptPropertyGet::_get_base_node() const {
  1326. #ifdef TOOLS_ENABLED
  1327. Ref<Script> script = get_visual_script();
  1328. if (!script.is_valid())
  1329. return NULL;
  1330. MainLoop *main_loop = OS::get_singleton()->get_main_loop();
  1331. SceneTree *scene_tree = Object::cast_to<SceneTree>(main_loop);
  1332. if (!scene_tree)
  1333. return NULL;
  1334. Node *edited_scene = scene_tree->get_edited_scene_root();
  1335. if (!edited_scene)
  1336. return NULL;
  1337. Node *script_node = _find_script_node(edited_scene, edited_scene, script);
  1338. if (!script_node)
  1339. return NULL;
  1340. if (!script_node->has_node(base_path))
  1341. return NULL;
  1342. Node *path_to = script_node->get_node(base_path);
  1343. return path_to;
  1344. #else
  1345. return NULL;
  1346. #endif
  1347. }
  1348. StringName VisualScriptPropertyGet::_get_base_type() const {
  1349. if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid())
  1350. return get_visual_script()->get_instance_base_type();
  1351. else if (call_mode == CALL_MODE_NODE_PATH && get_visual_script().is_valid()) {
  1352. Node *path = _get_base_node();
  1353. if (path)
  1354. return path->get_class();
  1355. }
  1356. return base_type;
  1357. }
  1358. int VisualScriptPropertyGet::get_input_value_port_count() const {
  1359. return (call_mode == CALL_MODE_BASIC_TYPE || call_mode == CALL_MODE_INSTANCE) ? 1 : 0;
  1360. }
  1361. int VisualScriptPropertyGet::get_output_value_port_count() const {
  1362. return 1;
  1363. }
  1364. String VisualScriptPropertyGet::get_output_sequence_port_text(int p_port) const {
  1365. return String();
  1366. }
  1367. PropertyInfo VisualScriptPropertyGet::get_input_value_port_info(int p_idx) const {
  1368. if (call_mode == CALL_MODE_INSTANCE || call_mode == CALL_MODE_BASIC_TYPE) {
  1369. if (p_idx == 0) {
  1370. PropertyInfo pi;
  1371. pi.type = (call_mode == CALL_MODE_INSTANCE ? Variant::OBJECT : basic_type);
  1372. pi.name = (call_mode == CALL_MODE_INSTANCE ? String("instance") : Variant::get_type_name(basic_type).to_lower());
  1373. return pi;
  1374. } else {
  1375. p_idx--;
  1376. }
  1377. }
  1378. return PropertyInfo();
  1379. }
  1380. PropertyInfo VisualScriptPropertyGet::get_output_value_port_info(int p_idx) const {
  1381. if (index != StringName()) {
  1382. Variant v;
  1383. Variant::CallError ce;
  1384. v = Variant::construct(type_cache, NULL, 0, ce);
  1385. Variant i = v.get(index);
  1386. return PropertyInfo(i.get_type(), "value." + String(index));
  1387. }
  1388. return PropertyInfo(type_cache, "value");
  1389. }
  1390. String VisualScriptPropertyGet::get_caption() const {
  1391. static const char *cname[4] = {
  1392. "SelfGet",
  1393. "NodeGet",
  1394. "InstanceGet",
  1395. "BasicGet"
  1396. };
  1397. return cname[call_mode];
  1398. }
  1399. String VisualScriptPropertyGet::get_text() const {
  1400. String prop;
  1401. if (call_mode == CALL_MODE_BASIC_TYPE)
  1402. prop = Variant::get_type_name(basic_type) + "." + property;
  1403. else if (call_mode == CALL_MODE_NODE_PATH)
  1404. prop = String(base_path) + ":" + property;
  1405. else if (call_mode == CALL_MODE_SELF)
  1406. prop = property;
  1407. else if (call_mode == CALL_MODE_INSTANCE)
  1408. prop = String(base_type) + ":" + property;
  1409. return prop;
  1410. }
  1411. void VisualScriptPropertyGet::set_base_type(const StringName &p_type) {
  1412. if (base_type == p_type)
  1413. return;
  1414. base_type = p_type;
  1415. _change_notify();
  1416. ports_changed_notify();
  1417. }
  1418. StringName VisualScriptPropertyGet::get_base_type() const {
  1419. return base_type;
  1420. }
  1421. void VisualScriptPropertyGet::set_base_script(const String &p_path) {
  1422. if (base_script == p_path)
  1423. return;
  1424. base_script = p_path;
  1425. _change_notify();
  1426. ports_changed_notify();
  1427. }
  1428. String VisualScriptPropertyGet::get_base_script() const {
  1429. return base_script;
  1430. }
  1431. void VisualScriptPropertyGet::_update_cache() {
  1432. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1433. //not super efficient..
  1434. Variant v;
  1435. Variant::CallError ce;
  1436. v = Variant::construct(basic_type, NULL, 0, ce);
  1437. List<PropertyInfo> pinfo;
  1438. v.get_property_list(&pinfo);
  1439. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  1440. if (E->get().name == property) {
  1441. type_cache = E->get().type;
  1442. return;
  1443. }
  1444. }
  1445. } else {
  1446. StringName type;
  1447. Ref<Script> script;
  1448. Node *node = NULL;
  1449. if (call_mode == CALL_MODE_NODE_PATH) {
  1450. node = _get_base_node();
  1451. if (node) {
  1452. type = node->get_class();
  1453. base_type = type; //cache, too
  1454. script = node->get_script();
  1455. }
  1456. } else if (call_mode == CALL_MODE_SELF) {
  1457. if (get_visual_script().is_valid()) {
  1458. type = get_visual_script()->get_instance_base_type();
  1459. base_type = type; //cache, too
  1460. script = get_visual_script();
  1461. }
  1462. } else if (call_mode == CALL_MODE_INSTANCE) {
  1463. type = base_type;
  1464. if (base_script != String()) {
  1465. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1466. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1467. }
  1468. if (ResourceCache::has(base_script)) {
  1469. script = Ref<Resource>(ResourceCache::get(base_script));
  1470. } else {
  1471. return;
  1472. }
  1473. }
  1474. }
  1475. bool valid = false;
  1476. Variant::Type type_ret;
  1477. type_ret = ClassDB::get_property_type(base_type, property, &valid);
  1478. if (valid) {
  1479. type_cache = type_ret;
  1480. return; //all dandy
  1481. }
  1482. if (node) {
  1483. Variant prop = node->get(property, &valid);
  1484. if (valid) {
  1485. type_cache = prop.get_type();
  1486. return; //all dandy again
  1487. }
  1488. }
  1489. if (script.is_valid()) {
  1490. type_ret = script->get_static_property_type(property, &valid);
  1491. if (valid) {
  1492. type_cache = type_ret;
  1493. return; //all dandy
  1494. }
  1495. }
  1496. }
  1497. }
  1498. void VisualScriptPropertyGet::set_property(const StringName &p_type) {
  1499. if (property == p_type)
  1500. return;
  1501. property = p_type;
  1502. _update_cache();
  1503. _change_notify();
  1504. ports_changed_notify();
  1505. }
  1506. StringName VisualScriptPropertyGet::get_property() const {
  1507. return property;
  1508. }
  1509. void VisualScriptPropertyGet::set_base_path(const NodePath &p_type) {
  1510. if (base_path == p_type)
  1511. return;
  1512. base_path = p_type;
  1513. _change_notify();
  1514. _update_base_type();
  1515. ports_changed_notify();
  1516. }
  1517. NodePath VisualScriptPropertyGet::get_base_path() const {
  1518. return base_path;
  1519. }
  1520. void VisualScriptPropertyGet::set_call_mode(CallMode p_mode) {
  1521. if (call_mode == p_mode)
  1522. return;
  1523. call_mode = p_mode;
  1524. _change_notify();
  1525. _update_base_type();
  1526. ports_changed_notify();
  1527. }
  1528. VisualScriptPropertyGet::CallMode VisualScriptPropertyGet::get_call_mode() const {
  1529. return call_mode;
  1530. }
  1531. void VisualScriptPropertyGet::set_basic_type(Variant::Type p_type) {
  1532. if (basic_type == p_type)
  1533. return;
  1534. basic_type = p_type;
  1535. _change_notify();
  1536. ports_changed_notify();
  1537. }
  1538. Variant::Type VisualScriptPropertyGet::get_basic_type() const {
  1539. return basic_type;
  1540. }
  1541. void VisualScriptPropertyGet::_set_type_cache(Variant::Type p_type) {
  1542. type_cache = p_type;
  1543. }
  1544. Variant::Type VisualScriptPropertyGet::_get_type_cache() const {
  1545. return type_cache;
  1546. }
  1547. void VisualScriptPropertyGet::set_index(const StringName &p_type) {
  1548. if (index == p_type)
  1549. return;
  1550. index = p_type;
  1551. _update_cache();
  1552. _change_notify();
  1553. ports_changed_notify();
  1554. }
  1555. StringName VisualScriptPropertyGet::get_index() const {
  1556. return index;
  1557. }
  1558. void VisualScriptPropertyGet::_validate_property(PropertyInfo &property) const {
  1559. if (property.name == "base_type") {
  1560. if (call_mode != CALL_MODE_INSTANCE) {
  1561. property.usage = PROPERTY_USAGE_NOEDITOR;
  1562. }
  1563. }
  1564. if (property.name == "base_script") {
  1565. if (call_mode != CALL_MODE_INSTANCE) {
  1566. property.usage = 0;
  1567. }
  1568. }
  1569. if (property.name == "basic_type") {
  1570. if (call_mode != CALL_MODE_BASIC_TYPE) {
  1571. property.usage = 0;
  1572. }
  1573. }
  1574. if (property.name == "node_path") {
  1575. if (call_mode != CALL_MODE_NODE_PATH) {
  1576. property.usage = 0;
  1577. } else {
  1578. Node *bnode = _get_base_node();
  1579. if (bnode) {
  1580. property.hint_string = bnode->get_path(); //convert to loong string
  1581. } else {
  1582. }
  1583. }
  1584. }
  1585. if (property.name == "property") {
  1586. if (call_mode == CALL_MODE_BASIC_TYPE) {
  1587. property.hint = PROPERTY_HINT_PROPERTY_OF_VARIANT_TYPE;
  1588. property.hint_string = Variant::get_type_name(basic_type);
  1589. } else if (call_mode == CALL_MODE_SELF && get_visual_script().is_valid()) {
  1590. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1591. property.hint_string = itos(get_visual_script()->get_instance_id());
  1592. } else if (call_mode == CALL_MODE_INSTANCE) {
  1593. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1594. property.hint_string = base_type;
  1595. if (base_script != String()) {
  1596. if (!ResourceCache::has(base_script) && ScriptServer::edit_request_func) {
  1597. ScriptServer::edit_request_func(base_script); //make sure it's loaded
  1598. }
  1599. if (ResourceCache::has(base_script)) {
  1600. Ref<Script> script = Ref<Resource>(ResourceCache::get(base_script));
  1601. if (script.is_valid()) {
  1602. property.hint = PROPERTY_HINT_PROPERTY_OF_SCRIPT;
  1603. property.hint_string = itos(script->get_instance_id());
  1604. }
  1605. }
  1606. }
  1607. } else if (call_mode == CALL_MODE_NODE_PATH) {
  1608. Node *node = _get_base_node();
  1609. if (node) {
  1610. property.hint = PROPERTY_HINT_PROPERTY_OF_INSTANCE;
  1611. property.hint_string = itos(node->get_instance_id());
  1612. } else {
  1613. property.hint = PROPERTY_HINT_PROPERTY_OF_BASE_TYPE;
  1614. property.hint_string = get_base_type();
  1615. }
  1616. }
  1617. }
  1618. if (property.name == "index") {
  1619. Variant::CallError ce;
  1620. Variant v = Variant::construct(type_cache, NULL, 0, ce);
  1621. List<PropertyInfo> plist;
  1622. v.get_property_list(&plist);
  1623. String options = "";
  1624. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1625. options += "," + E->get().name;
  1626. }
  1627. property.hint = PROPERTY_HINT_ENUM;
  1628. property.hint_string = options;
  1629. property.type = Variant::STRING;
  1630. if (options == "")
  1631. property.usage = 0; //hide if type has no usable index
  1632. }
  1633. }
  1634. void VisualScriptPropertyGet::_bind_methods() {
  1635. ClassDB::bind_method(D_METHOD("set_base_type", "base_type"), &VisualScriptPropertyGet::set_base_type);
  1636. ClassDB::bind_method(D_METHOD("get_base_type"), &VisualScriptPropertyGet::get_base_type);
  1637. ClassDB::bind_method(D_METHOD("set_base_script", "base_script"), &VisualScriptPropertyGet::set_base_script);
  1638. ClassDB::bind_method(D_METHOD("get_base_script"), &VisualScriptPropertyGet::get_base_script);
  1639. ClassDB::bind_method(D_METHOD("set_basic_type", "basic_type"), &VisualScriptPropertyGet::set_basic_type);
  1640. ClassDB::bind_method(D_METHOD("get_basic_type"), &VisualScriptPropertyGet::get_basic_type);
  1641. ClassDB::bind_method(D_METHOD("_set_type_cache", "type_cache"), &VisualScriptPropertyGet::_set_type_cache);
  1642. ClassDB::bind_method(D_METHOD("_get_type_cache"), &VisualScriptPropertyGet::_get_type_cache);
  1643. ClassDB::bind_method(D_METHOD("set_property", "property"), &VisualScriptPropertyGet::set_property);
  1644. ClassDB::bind_method(D_METHOD("get_property"), &VisualScriptPropertyGet::get_property);
  1645. ClassDB::bind_method(D_METHOD("set_call_mode", "mode"), &VisualScriptPropertyGet::set_call_mode);
  1646. ClassDB::bind_method(D_METHOD("get_call_mode"), &VisualScriptPropertyGet::get_call_mode);
  1647. ClassDB::bind_method(D_METHOD("set_base_path", "base_path"), &VisualScriptPropertyGet::set_base_path);
  1648. ClassDB::bind_method(D_METHOD("get_base_path"), &VisualScriptPropertyGet::get_base_path);
  1649. ClassDB::bind_method(D_METHOD("set_index", "index"), &VisualScriptPropertyGet::set_index);
  1650. ClassDB::bind_method(D_METHOD("get_index"), &VisualScriptPropertyGet::get_index);
  1651. String bt;
  1652. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1653. if (i > 0)
  1654. bt += ",";
  1655. bt += Variant::get_type_name(Variant::Type(i));
  1656. }
  1657. List<String> script_extensions;
  1658. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1659. ScriptServer::get_language(i)->get_recognized_extensions(&script_extensions);
  1660. }
  1661. String script_ext_hint;
  1662. for (List<String>::Element *E = script_extensions.front(); E; E = E->next()) {
  1663. if (script_ext_hint != String())
  1664. script_ext_hint += ",";
  1665. script_ext_hint += "." + E->get();
  1666. }
  1667. ADD_PROPERTY(PropertyInfo(Variant::INT, "set_mode", PROPERTY_HINT_ENUM, "Self,Node Path,Instance,Basic Type"), "set_call_mode", "get_call_mode");
  1668. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_type", PROPERTY_HINT_TYPE_STRING, "Object"), "set_base_type", "get_base_type");
  1669. ADD_PROPERTY(PropertyInfo(Variant::STRING, "base_script", PROPERTY_HINT_FILE, script_ext_hint), "set_base_script", "get_base_script");
  1670. ADD_PROPERTY(PropertyInfo(Variant::INT, "type_cache", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_type_cache", "_get_type_cache");
  1671. ADD_PROPERTY(PropertyInfo(Variant::INT, "basic_type", PROPERTY_HINT_ENUM, bt), "set_basic_type", "get_basic_type");
  1672. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_path", PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE), "set_base_path", "get_base_path");
  1673. ADD_PROPERTY(PropertyInfo(Variant::STRING, "property"), "set_property", "get_property");
  1674. ADD_PROPERTY(PropertyInfo(Variant::STRING, "index", PROPERTY_HINT_ENUM), "set_index", "get_index");
  1675. BIND_ENUM_CONSTANT(CALL_MODE_SELF);
  1676. BIND_ENUM_CONSTANT(CALL_MODE_NODE_PATH);
  1677. BIND_ENUM_CONSTANT(CALL_MODE_INSTANCE);
  1678. }
  1679. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1680. public:
  1681. VisualScriptPropertyGet::CallMode call_mode;
  1682. NodePath node_path;
  1683. StringName property;
  1684. StringName index;
  1685. VisualScriptPropertyGet *node;
  1686. VisualScriptInstance *instance;
  1687. 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) {
  1688. switch (call_mode) {
  1689. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1690. Object *object = instance->get_owner_ptr();
  1691. bool valid;
  1692. *p_outputs[0] = object->get(property, &valid);
  1693. if (index != StringName()) {
  1694. *p_outputs[0] = p_outputs[0]->get_named(index);
  1695. }
  1696. if (!valid) {
  1697. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1698. r_error_str = RTR("Invalid index property name.");
  1699. return 0;
  1700. }
  1701. } break;
  1702. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1703. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1704. if (!node) {
  1705. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1706. r_error_str = RTR("Base object is not a Node!");
  1707. return 0;
  1708. }
  1709. Node *another = node->get_node(node_path);
  1710. if (!another) {
  1711. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1712. r_error_str = RTR("Path does not lead Node!");
  1713. return 0;
  1714. }
  1715. bool valid;
  1716. *p_outputs[0] = another->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 = vformat(RTR("Invalid index property name '%s' in node %s."), String(property), another->get_name());
  1723. return 0;
  1724. }
  1725. } break;
  1726. default: {
  1727. bool valid;
  1728. Variant v = *p_inputs[0];
  1729. *p_outputs[0] = v.get(property, &valid);
  1730. if (index != StringName()) {
  1731. *p_outputs[0] = p_outputs[0]->get_named(index);
  1732. }
  1733. if (!valid) {
  1734. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1735. r_error_str = RTR("Invalid index property name.");
  1736. }
  1737. };
  1738. }
  1739. return 0;
  1740. }
  1741. };
  1742. VisualScriptNodeInstance *VisualScriptPropertyGet::instance(VisualScriptInstance *p_instance) {
  1743. VisualScriptNodeInstancePropertyGet *instance = memnew(VisualScriptNodeInstancePropertyGet);
  1744. instance->node = this;
  1745. instance->instance = p_instance;
  1746. instance->property = property;
  1747. instance->call_mode = call_mode;
  1748. instance->node_path = base_path;
  1749. instance->index = index;
  1750. return instance;
  1751. }
  1752. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1753. call_mode = CALL_MODE_SELF;
  1754. base_type = "Object";
  1755. basic_type = Variant::NIL;
  1756. type_cache = Variant::NIL;
  1757. }
  1758. template <VisualScriptPropertyGet::CallMode cmode>
  1759. static Ref<VisualScriptNode> create_property_get_node(const String &p_name) {
  1760. Ref<VisualScriptPropertyGet> node;
  1761. node.instance();
  1762. node->set_call_mode(cmode);
  1763. return node;
  1764. }
  1765. //////////////////////////////////////////
  1766. ////////////////EMIT//////////////////////
  1767. //////////////////////////////////////////
  1768. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1769. return 1;
  1770. }
  1771. bool VisualScriptEmitSignal::has_input_sequence_port() const {
  1772. return true;
  1773. }
  1774. int VisualScriptEmitSignal::get_input_value_port_count() const {
  1775. Ref<VisualScript> vs = get_visual_script();
  1776. if (vs.is_valid()) {
  1777. if (!vs->has_custom_signal(name))
  1778. return 0;
  1779. return vs->custom_signal_get_argument_count(name);
  1780. }
  1781. return 0;
  1782. }
  1783. int VisualScriptEmitSignal::get_output_value_port_count() const {
  1784. return 0;
  1785. }
  1786. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1787. return String();
  1788. }
  1789. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const {
  1790. Ref<VisualScript> vs = get_visual_script();
  1791. if (vs.is_valid()) {
  1792. if (!vs->has_custom_signal(name))
  1793. return PropertyInfo();
  1794. return PropertyInfo(vs->custom_signal_get_argument_type(name, p_idx), vs->custom_signal_get_argument_name(name, p_idx));
  1795. }
  1796. return PropertyInfo();
  1797. }
  1798. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const {
  1799. return PropertyInfo();
  1800. }
  1801. String VisualScriptEmitSignal::get_caption() const {
  1802. return "EmitSignal";
  1803. }
  1804. String VisualScriptEmitSignal::get_text() const {
  1805. return "emit " + String(name);
  1806. }
  1807. void VisualScriptEmitSignal::set_signal(const StringName &p_type) {
  1808. if (name == p_type)
  1809. return;
  1810. name = p_type;
  1811. _change_notify();
  1812. ports_changed_notify();
  1813. }
  1814. StringName VisualScriptEmitSignal::get_signal() const {
  1815. return name;
  1816. }
  1817. void VisualScriptEmitSignal::_validate_property(PropertyInfo &property) const {
  1818. if (property.name == "signal") {
  1819. property.hint = PROPERTY_HINT_ENUM;
  1820. List<StringName> sigs;
  1821. Ref<VisualScript> vs = get_visual_script();
  1822. if (vs.is_valid()) {
  1823. vs->get_custom_signal_list(&sigs);
  1824. }
  1825. String ml;
  1826. for (List<StringName>::Element *E = sigs.front(); E; E = E->next()) {
  1827. if (ml != String())
  1828. ml += ",";
  1829. ml += E->get();
  1830. }
  1831. property.hint_string = ml;
  1832. }
  1833. }
  1834. void VisualScriptEmitSignal::_bind_methods() {
  1835. ClassDB::bind_method(D_METHOD("set_signal", "name"), &VisualScriptEmitSignal::set_signal);
  1836. ClassDB::bind_method(D_METHOD("get_signal"), &VisualScriptEmitSignal::get_signal);
  1837. ADD_PROPERTY(PropertyInfo(Variant::STRING, "signal"), "set_signal", "get_signal");
  1838. }
  1839. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1840. public:
  1841. VisualScriptEmitSignal *node;
  1842. VisualScriptInstance *instance;
  1843. int argcount;
  1844. StringName name;
  1845. //virtual int get_working_memory_size() const { return 0; }
  1846. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1847. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1848. 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) {
  1849. Object *obj = instance->get_owner_ptr();
  1850. obj->emit_signal(name, p_inputs, argcount);
  1851. return 0;
  1852. }
  1853. };
  1854. VisualScriptNodeInstance *VisualScriptEmitSignal::instance(VisualScriptInstance *p_instance) {
  1855. VisualScriptNodeInstanceEmitSignal *instance = memnew(VisualScriptNodeInstanceEmitSignal);
  1856. instance->node = this;
  1857. instance->instance = p_instance;
  1858. instance->name = name;
  1859. instance->argcount = get_input_value_port_count();
  1860. return instance;
  1861. }
  1862. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1863. }
  1864. static Ref<VisualScriptNode> create_basic_type_call_node(const String &p_name) {
  1865. Vector<String> path = p_name.split("/");
  1866. ERR_FAIL_COND_V(path.size() < 4, Ref<VisualScriptNode>());
  1867. String base_type = path[2];
  1868. String method = path[3];
  1869. Ref<VisualScriptFunctionCall> node;
  1870. node.instance();
  1871. Variant::Type type = Variant::VARIANT_MAX;
  1872. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1873. if (Variant::get_type_name(Variant::Type(i)) == base_type) {
  1874. type = Variant::Type(i);
  1875. break;
  1876. }
  1877. }
  1878. ERR_FAIL_COND_V(type == Variant::VARIANT_MAX, Ref<VisualScriptNode>());
  1879. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1880. node->set_basic_type(type);
  1881. node->set_function(method);
  1882. return node;
  1883. }
  1884. void register_visual_script_func_nodes() {
  1885. VisualScriptLanguage::singleton->add_register_func("functions/call", create_node_generic<VisualScriptFunctionCall>);
  1886. VisualScriptLanguage::singleton->add_register_func("functions/set", create_node_generic<VisualScriptPropertySet>);
  1887. VisualScriptLanguage::singleton->add_register_func("functions/get", create_node_generic<VisualScriptPropertyGet>);
  1888. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1889. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1890. VisualScriptLanguage::singleton->add_register_func("functions/emit_signal", create_node_generic<VisualScriptEmitSignal>);
  1891. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1892. Variant::Type t = Variant::Type(i);
  1893. String type_name = Variant::get_type_name(t);
  1894. Variant::CallError ce;
  1895. Variant vt = Variant::construct(t, NULL, 0, ce);
  1896. List<MethodInfo> ml;
  1897. vt.get_method_list(&ml);
  1898. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  1899. VisualScriptLanguage::singleton->add_register_func("functions/by_type/" + type_name + "/" + E->get().name, create_basic_type_call_node);
  1900. }
  1901. }
  1902. }