visual_script_func_nodes.cpp 70 KB

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