visual_script_func_nodes.cpp 72 KB

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