visual_script_func_nodes.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  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. 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, true);
  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", PROPERTY_HINT_TYPE_STRING, 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, true);
  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. }
  1667. class VisualScriptNodeInstancePropertyGet : public VisualScriptNodeInstance {
  1668. public:
  1669. VisualScriptPropertyGet::CallMode call_mode;
  1670. NodePath node_path;
  1671. StringName property;
  1672. StringName index;
  1673. VisualScriptPropertyGet *node;
  1674. VisualScriptInstance *instance;
  1675. 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) {
  1676. switch (call_mode) {
  1677. case VisualScriptPropertyGet::CALL_MODE_SELF: {
  1678. Object *object = instance->get_owner_ptr();
  1679. bool valid;
  1680. *p_outputs[0] = object->get(property, &valid);
  1681. if (index != StringName()) {
  1682. *p_outputs[0] = p_outputs[0]->get_named(index);
  1683. }
  1684. if (!valid) {
  1685. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1686. r_error_str = RTR("Invalid index property name.");
  1687. return 0;
  1688. }
  1689. } break;
  1690. case VisualScriptPropertyGet::CALL_MODE_NODE_PATH: {
  1691. Node *node = Object::cast_to<Node>(instance->get_owner_ptr());
  1692. if (!node) {
  1693. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1694. r_error_str = RTR("Base object is not a Node!");
  1695. return 0;
  1696. }
  1697. Node *another = node->get_node(node_path);
  1698. if (!another) {
  1699. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1700. r_error_str = RTR("Path does not lead Node!");
  1701. return 0;
  1702. }
  1703. bool valid;
  1704. *p_outputs[0] = another->get(property, &valid);
  1705. if (index != StringName()) {
  1706. *p_outputs[0] = p_outputs[0]->get_named(index);
  1707. }
  1708. if (!valid) {
  1709. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1710. r_error_str = vformat(RTR("Invalid index property name '%s' in node %s."), String(property), another->get_name());
  1711. return 0;
  1712. }
  1713. } break;
  1714. default: {
  1715. bool valid;
  1716. Variant v = *p_inputs[0];
  1717. *p_outputs[0] = v.get(property, &valid);
  1718. if (index != StringName()) {
  1719. *p_outputs[0] = p_outputs[0]->get_named(index);
  1720. }
  1721. if (!valid) {
  1722. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1723. r_error_str = RTR("Invalid index property name.");
  1724. }
  1725. };
  1726. }
  1727. return 0;
  1728. }
  1729. };
  1730. VisualScriptNodeInstance *VisualScriptPropertyGet::instance(VisualScriptInstance *p_instance) {
  1731. VisualScriptNodeInstancePropertyGet *instance = memnew(VisualScriptNodeInstancePropertyGet);
  1732. instance->node = this;
  1733. instance->instance = p_instance;
  1734. instance->property = property;
  1735. instance->call_mode = call_mode;
  1736. instance->node_path = base_path;
  1737. instance->index = index;
  1738. return instance;
  1739. }
  1740. VisualScriptPropertyGet::VisualScriptPropertyGet() {
  1741. call_mode = CALL_MODE_SELF;
  1742. base_type = "Object";
  1743. basic_type = Variant::NIL;
  1744. type_cache = Variant::NIL;
  1745. }
  1746. template <VisualScriptPropertyGet::CallMode cmode>
  1747. static Ref<VisualScriptNode> create_property_get_node(const String &p_name) {
  1748. Ref<VisualScriptPropertyGet> node;
  1749. node.instance();
  1750. node->set_call_mode(cmode);
  1751. return node;
  1752. }
  1753. //////////////////////////////////////////
  1754. ////////////////EMIT//////////////////////
  1755. //////////////////////////////////////////
  1756. int VisualScriptEmitSignal::get_output_sequence_port_count() const {
  1757. return 1;
  1758. }
  1759. bool VisualScriptEmitSignal::has_input_sequence_port() const {
  1760. return true;
  1761. }
  1762. int VisualScriptEmitSignal::get_input_value_port_count() const {
  1763. Ref<VisualScript> vs = get_visual_script();
  1764. if (vs.is_valid()) {
  1765. if (!vs->has_custom_signal(name))
  1766. return 0;
  1767. return vs->custom_signal_get_argument_count(name);
  1768. }
  1769. return 0;
  1770. }
  1771. int VisualScriptEmitSignal::get_output_value_port_count() const {
  1772. return 0;
  1773. }
  1774. String VisualScriptEmitSignal::get_output_sequence_port_text(int p_port) const {
  1775. return String();
  1776. }
  1777. PropertyInfo VisualScriptEmitSignal::get_input_value_port_info(int p_idx) const {
  1778. Ref<VisualScript> vs = get_visual_script();
  1779. if (vs.is_valid()) {
  1780. if (!vs->has_custom_signal(name))
  1781. return PropertyInfo();
  1782. return PropertyInfo(vs->custom_signal_get_argument_type(name, p_idx), vs->custom_signal_get_argument_name(name, p_idx));
  1783. }
  1784. return PropertyInfo();
  1785. }
  1786. PropertyInfo VisualScriptEmitSignal::get_output_value_port_info(int p_idx) const {
  1787. return PropertyInfo();
  1788. }
  1789. String VisualScriptEmitSignal::get_caption() const {
  1790. return "Emit " + String(name);
  1791. }
  1792. void VisualScriptEmitSignal::set_signal(const StringName &p_type) {
  1793. if (name == p_type)
  1794. return;
  1795. name = p_type;
  1796. _change_notify();
  1797. ports_changed_notify();
  1798. }
  1799. StringName VisualScriptEmitSignal::get_signal() const {
  1800. return name;
  1801. }
  1802. void VisualScriptEmitSignal::_validate_property(PropertyInfo &property) const {
  1803. if (property.name == "signal") {
  1804. property.hint = PROPERTY_HINT_ENUM;
  1805. List<StringName> sigs;
  1806. Ref<VisualScript> vs = get_visual_script();
  1807. if (vs.is_valid()) {
  1808. vs->get_custom_signal_list(&sigs);
  1809. }
  1810. String ml;
  1811. for (List<StringName>::Element *E = sigs.front(); E; E = E->next()) {
  1812. if (ml != String())
  1813. ml += ",";
  1814. ml += E->get();
  1815. }
  1816. property.hint_string = ml;
  1817. }
  1818. }
  1819. void VisualScriptEmitSignal::_bind_methods() {
  1820. ClassDB::bind_method(D_METHOD("set_signal", "name"), &VisualScriptEmitSignal::set_signal);
  1821. ClassDB::bind_method(D_METHOD("get_signal"), &VisualScriptEmitSignal::get_signal);
  1822. ADD_PROPERTY(PropertyInfo(Variant::STRING, "signal"), "set_signal", "get_signal");
  1823. }
  1824. class VisualScriptNodeInstanceEmitSignal : public VisualScriptNodeInstance {
  1825. public:
  1826. VisualScriptEmitSignal *node;
  1827. VisualScriptInstance *instance;
  1828. int argcount;
  1829. StringName name;
  1830. //virtual int get_working_memory_size() const { return 0; }
  1831. //virtual bool is_output_port_unsequenced(int p_idx) const { return false; }
  1832. //virtual bool get_output_port_unsequenced(int p_idx,Variant* r_value,Variant* p_working_mem,String &r_error) const { return true; }
  1833. 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) {
  1834. Object *obj = instance->get_owner_ptr();
  1835. obj->emit_signal(name, p_inputs, argcount);
  1836. return 0;
  1837. }
  1838. };
  1839. VisualScriptNodeInstance *VisualScriptEmitSignal::instance(VisualScriptInstance *p_instance) {
  1840. VisualScriptNodeInstanceEmitSignal *instance = memnew(VisualScriptNodeInstanceEmitSignal);
  1841. instance->node = this;
  1842. instance->instance = p_instance;
  1843. instance->name = name;
  1844. instance->argcount = get_input_value_port_count();
  1845. return instance;
  1846. }
  1847. VisualScriptEmitSignal::VisualScriptEmitSignal() {
  1848. }
  1849. static Ref<VisualScriptNode> create_basic_type_call_node(const String &p_name) {
  1850. Vector<String> path = p_name.split("/");
  1851. ERR_FAIL_COND_V(path.size() < 4, Ref<VisualScriptNode>());
  1852. String base_type = path[2];
  1853. String method = path[3];
  1854. Ref<VisualScriptFunctionCall> node;
  1855. node.instance();
  1856. Variant::Type type = Variant::VARIANT_MAX;
  1857. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1858. if (Variant::get_type_name(Variant::Type(i)) == base_type) {
  1859. type = Variant::Type(i);
  1860. break;
  1861. }
  1862. }
  1863. ERR_FAIL_COND_V(type == Variant::VARIANT_MAX, Ref<VisualScriptNode>());
  1864. node->set_call_mode(VisualScriptFunctionCall::CALL_MODE_BASIC_TYPE);
  1865. node->set_basic_type(type);
  1866. node->set_function(method);
  1867. return node;
  1868. }
  1869. void register_visual_script_func_nodes() {
  1870. VisualScriptLanguage::singleton->add_register_func("functions/call", create_node_generic<VisualScriptFunctionCall>);
  1871. VisualScriptLanguage::singleton->add_register_func("functions/set", create_node_generic<VisualScriptPropertySet>);
  1872. VisualScriptLanguage::singleton->add_register_func("functions/get", create_node_generic<VisualScriptPropertyGet>);
  1873. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_self",create_script_call_node<VisualScriptScriptCall::CALL_MODE_SELF>);
  1874. //VisualScriptLanguage::singleton->add_register_func("functions/call_script/call_node",create_script_call_node<VisualScriptScriptCall::CALL_MODE_NODE_PATH>);
  1875. VisualScriptLanguage::singleton->add_register_func("functions/emit_signal", create_node_generic<VisualScriptEmitSignal>);
  1876. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1877. Variant::Type t = Variant::Type(i);
  1878. String type_name = Variant::get_type_name(t);
  1879. Variant::CallError ce;
  1880. Variant vt = Variant::construct(t, NULL, 0, ce);
  1881. List<MethodInfo> ml;
  1882. vt.get_method_list(&ml);
  1883. for (List<MethodInfo>::Element *E = ml.front(); E; E = E->next()) {
  1884. VisualScriptLanguage::singleton->add_register_func("functions/by_type/" + type_name + "/" + E->get().name, create_basic_type_call_node);
  1885. }
  1886. }
  1887. }