visual_script_func_nodes.cpp 72 KB

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