visual_script_func_nodes.cpp 70 KB

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