visual_script_func_nodes.cpp 72 KB

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