visual_script_func_nodes.cpp 70 KB

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