visual_script_func_nodes.cpp 72 KB

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