visual_script_func_nodes.cpp 71 KB

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