2
0

visual_script_func_nodes.cpp 70 KB

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