visual_script_func_nodes.cpp 70 KB

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