visual_script_func_nodes.cpp 72 KB

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