visual_script_func_nodes.cpp 71 KB

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