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