visual_script_func_nodes.cpp 63 KB

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