visual_script_func_nodes.cpp 70 KB

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