visual_script_func_nodes.cpp 70 KB

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