visual_script_func_nodes.cpp 72 KB

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