visual_script.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  1. /*************************************************************************/
  2. /* visual_script.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.h"
  31. #include "os/os.h"
  32. #include "project_settings.h"
  33. #include "scene/main/node.h"
  34. #include "visual_script_nodes.h"
  35. //used by editor, this is not really saved
  36. void VisualScriptNode::set_breakpoint(bool p_breakpoint) {
  37. breakpoint = p_breakpoint;
  38. }
  39. bool VisualScriptNode::is_breakpoint() const {
  40. return breakpoint;
  41. }
  42. void VisualScriptNode::_notification(int p_what) {
  43. if (p_what == NOTIFICATION_POSTINITIALIZE) {
  44. int dvc = get_input_value_port_count();
  45. for (int i = 0; i < dvc; i++) {
  46. Variant::Type expected = get_input_value_port_info(i).type;
  47. Variant::CallError ce;
  48. default_input_values.push_back(Variant::construct(expected, NULL, 0, ce, false));
  49. }
  50. }
  51. }
  52. void VisualScriptNode::ports_changed_notify() {
  53. default_input_values.resize(MAX(default_input_values.size(), get_input_value_port_count())); //let it grow as big as possible, we don't want to lose values on resize
  54. emit_signal("ports_changed");
  55. }
  56. void VisualScriptNode::set_default_input_value(int p_port, const Variant &p_value) {
  57. ERR_FAIL_INDEX(p_port, default_input_values.size());
  58. default_input_values[p_port] = p_value;
  59. #ifdef TOOLS_ENABLED
  60. for (Set<VisualScript *>::Element *E = scripts_used.front(); E; E = E->next()) {
  61. E->get()->set_edited(true);
  62. }
  63. #endif
  64. }
  65. Variant VisualScriptNode::get_default_input_value(int p_port) const {
  66. ERR_FAIL_INDEX_V(p_port, default_input_values.size(), Variant());
  67. return default_input_values[p_port];
  68. }
  69. void VisualScriptNode::_set_default_input_values(Array p_values) {
  70. default_input_values = p_values;
  71. }
  72. void VisualScriptNode::validate_input_default_values() {
  73. default_input_values.resize(get_input_value_port_count());
  74. //actually validate on save
  75. for (int i = 0; i < get_input_value_port_count(); i++) {
  76. Variant::Type expected = get_input_value_port_info(i).type;
  77. if (expected == Variant::NIL || expected == default_input_values[i].get_type()) {
  78. continue;
  79. } else {
  80. //not the same, reconvert
  81. Variant::CallError ce;
  82. Variant existing = default_input_values[i];
  83. const Variant *existingp = &existing;
  84. default_input_values[i] = Variant::construct(expected, &existingp, 1, ce, false);
  85. if (ce.error != Variant::CallError::CALL_OK) {
  86. //could not convert? force..
  87. default_input_values[i] = Variant::construct(expected, NULL, 0, ce, false);
  88. }
  89. }
  90. }
  91. }
  92. Array VisualScriptNode::_get_default_input_values() const {
  93. //validate on save, since on load there is little info about this
  94. return default_input_values;
  95. }
  96. void VisualScriptNode::_bind_methods() {
  97. ClassDB::bind_method(D_METHOD("get_visual_script"), &VisualScriptNode::get_visual_script);
  98. ClassDB::bind_method(D_METHOD("set_default_input_value", "port_idx", "value"), &VisualScriptNode::set_default_input_value);
  99. ClassDB::bind_method(D_METHOD("get_default_input_value", "port_idx"), &VisualScriptNode::get_default_input_value);
  100. ClassDB::bind_method(D_METHOD("_set_default_input_values", "values"), &VisualScriptNode::_set_default_input_values);
  101. ClassDB::bind_method(D_METHOD("_get_default_input_values"), &VisualScriptNode::_get_default_input_values);
  102. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_default_input_values", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_default_input_values", "_get_default_input_values");
  103. ADD_SIGNAL(MethodInfo("ports_changed"));
  104. }
  105. VisualScriptNode::TypeGuess VisualScriptNode::guess_output_type(TypeGuess *p_inputs, int p_output) const {
  106. PropertyInfo pinfo = get_output_value_port_info(p_output);
  107. TypeGuess tg;
  108. tg.type = pinfo.type;
  109. if (pinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  110. tg.gdclass = pinfo.hint_string;
  111. }
  112. return tg;
  113. }
  114. Ref<VisualScript> VisualScriptNode::get_visual_script() const {
  115. if (scripts_used.size())
  116. return Ref<VisualScript>(scripts_used.front()->get());
  117. return Ref<VisualScript>();
  118. }
  119. VisualScriptNode::VisualScriptNode() {
  120. breakpoint = false;
  121. }
  122. ////////////////
  123. /////////////////////
  124. VisualScriptNodeInstance::VisualScriptNodeInstance() {
  125. sequence_outputs = NULL;
  126. input_ports = NULL;
  127. }
  128. VisualScriptNodeInstance::~VisualScriptNodeInstance() {
  129. if (sequence_outputs) {
  130. memdelete_arr(sequence_outputs);
  131. }
  132. if (input_ports) {
  133. memdelete_arr(input_ports);
  134. }
  135. if (output_ports) {
  136. memdelete_arr(output_ports);
  137. }
  138. }
  139. void VisualScript::add_function(const StringName &p_name) {
  140. ERR_FAIL_COND(instances.size());
  141. ERR_FAIL_COND(!String(p_name).is_valid_identifier());
  142. ERR_FAIL_COND(functions.has(p_name));
  143. functions[p_name] = Function();
  144. functions[p_name].scroll = Vector2(-50, -100);
  145. }
  146. bool VisualScript::has_function(const StringName &p_name) const {
  147. return functions.has(p_name);
  148. }
  149. void VisualScript::remove_function(const StringName &p_name) {
  150. ERR_FAIL_COND(instances.size());
  151. ERR_FAIL_COND(!functions.has(p_name));
  152. for (Map<int, Function::NodeData>::Element *E = functions[p_name].nodes.front(); E; E = E->next()) {
  153. E->get().node->disconnect("ports_changed", this, "_node_ports_changed");
  154. E->get().node->scripts_used.erase(this);
  155. }
  156. functions.erase(p_name);
  157. }
  158. void VisualScript::rename_function(const StringName &p_name, const StringName &p_new_name) {
  159. ERR_FAIL_COND(instances.size());
  160. ERR_FAIL_COND(!functions.has(p_name));
  161. if (p_new_name == p_name)
  162. return;
  163. ERR_FAIL_COND(!String(p_new_name).is_valid_identifier());
  164. ERR_FAIL_COND(functions.has(p_new_name));
  165. ERR_FAIL_COND(variables.has(p_new_name));
  166. ERR_FAIL_COND(custom_signals.has(p_new_name));
  167. functions[p_new_name] = functions[p_name];
  168. functions.erase(p_name);
  169. }
  170. void VisualScript::set_function_scroll(const StringName &p_name, const Vector2 &p_scroll) {
  171. ERR_FAIL_COND(!functions.has(p_name));
  172. functions[p_name].scroll = p_scroll;
  173. }
  174. Vector2 VisualScript::get_function_scroll(const StringName &p_name) const {
  175. ERR_FAIL_COND_V(!functions.has(p_name), Vector2());
  176. return functions[p_name].scroll;
  177. }
  178. void VisualScript::get_function_list(List<StringName> *r_functions) const {
  179. for (const Map<StringName, Function>::Element *E = functions.front(); E; E = E->next()) {
  180. r_functions->push_back(E->key());
  181. }
  182. r_functions->sort_custom<StringName::AlphCompare>();
  183. }
  184. int VisualScript::get_function_node_id(const StringName &p_name) const {
  185. ERR_FAIL_COND_V(!functions.has(p_name), -1);
  186. return functions[p_name].function_id;
  187. }
  188. void VisualScript::_node_ports_changed(int p_id) {
  189. StringName function;
  190. for (Map<StringName, Function>::Element *E = functions.front(); E; E = E->next()) {
  191. if (E->get().nodes.has(p_id)) {
  192. function = E->key();
  193. break;
  194. }
  195. }
  196. ERR_FAIL_COND(function == StringName());
  197. Function &func = functions[function];
  198. Ref<VisualScriptNode> vsn = func.nodes[p_id].node;
  199. if (OS::get_singleton()->get_main_loop() && OS::get_singleton()->get_main_loop()->cast_to<SceneTree>() && Engine::get_singleton()->is_editor_hint()) {
  200. vsn->validate_input_default_values(); //force validate default values when editing on editor
  201. }
  202. //must revalidate all the functions
  203. {
  204. List<SequenceConnection> to_remove;
  205. for (Set<SequenceConnection>::Element *E = func.sequence_connections.front(); E; E = E->next()) {
  206. if (E->get().from_node == p_id && E->get().from_output >= vsn->get_output_sequence_port_count()) {
  207. to_remove.push_back(E->get());
  208. }
  209. if (E->get().to_node == p_id && !vsn->has_input_sequence_port()) {
  210. to_remove.push_back(E->get());
  211. }
  212. }
  213. while (to_remove.size()) {
  214. func.sequence_connections.erase(to_remove.front()->get());
  215. to_remove.pop_front();
  216. }
  217. }
  218. {
  219. List<DataConnection> to_remove;
  220. for (Set<DataConnection>::Element *E = func.data_connections.front(); E; E = E->next()) {
  221. if (E->get().from_node == p_id && E->get().from_port >= vsn->get_output_value_port_count()) {
  222. to_remove.push_back(E->get());
  223. }
  224. if (E->get().to_node == p_id && E->get().to_port >= vsn->get_input_value_port_count()) {
  225. to_remove.push_back(E->get());
  226. }
  227. }
  228. while (to_remove.size()) {
  229. func.data_connections.erase(to_remove.front()->get());
  230. to_remove.pop_front();
  231. }
  232. }
  233. #ifdef TOOLS_ENABLED
  234. set_edited(true); //something changed, let's set as edited
  235. emit_signal("node_ports_changed", function, p_id);
  236. #endif
  237. }
  238. void VisualScript::add_node(const StringName &p_func, int p_id, const Ref<VisualScriptNode> &p_node, const Point2 &p_pos) {
  239. ERR_FAIL_COND(instances.size());
  240. ERR_FAIL_COND(!functions.has(p_func));
  241. for (Map<StringName, Function>::Element *E = functions.front(); E; E = E->next()) {
  242. ERR_FAIL_COND(E->get().nodes.has(p_id)); //id can exist only one in script, even for different functions
  243. }
  244. Function &func = functions[p_func];
  245. if (p_node->cast_to<VisualScriptFunction>()) {
  246. //the function indeed
  247. ERR_EXPLAIN("A function node already has been set here.");
  248. ERR_FAIL_COND(func.function_id >= 0);
  249. func.function_id = p_id;
  250. }
  251. Function::NodeData nd;
  252. nd.node = p_node;
  253. nd.pos = p_pos;
  254. Ref<VisualScriptNode> vsn = p_node;
  255. vsn->connect("ports_changed", this, "_node_ports_changed", varray(p_id));
  256. vsn->scripts_used.insert(this);
  257. func.nodes[p_id] = nd;
  258. }
  259. void VisualScript::remove_node(const StringName &p_func, int p_id) {
  260. ERR_FAIL_COND(instances.size());
  261. ERR_FAIL_COND(!functions.has(p_func));
  262. Function &func = functions[p_func];
  263. ERR_FAIL_COND(!func.nodes.has(p_id));
  264. {
  265. List<SequenceConnection> to_remove;
  266. for (Set<SequenceConnection>::Element *E = func.sequence_connections.front(); E; E = E->next()) {
  267. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  268. to_remove.push_back(E->get());
  269. }
  270. }
  271. while (to_remove.size()) {
  272. func.sequence_connections.erase(to_remove.front()->get());
  273. to_remove.pop_front();
  274. }
  275. }
  276. {
  277. List<DataConnection> to_remove;
  278. for (Set<DataConnection>::Element *E = func.data_connections.front(); E; E = E->next()) {
  279. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  280. to_remove.push_back(E->get());
  281. }
  282. }
  283. while (to_remove.size()) {
  284. func.data_connections.erase(to_remove.front()->get());
  285. to_remove.pop_front();
  286. }
  287. }
  288. if (func.nodes[p_id].node->cast_to<VisualScriptFunction>()) {
  289. func.function_id = -1; //revert to invalid
  290. }
  291. func.nodes[p_id].node->disconnect("ports_changed", this, "_node_ports_changed");
  292. func.nodes[p_id].node->scripts_used.erase(this);
  293. func.nodes.erase(p_id);
  294. }
  295. bool VisualScript::has_node(const StringName &p_func, int p_id) const {
  296. ERR_FAIL_COND_V(!functions.has(p_func), false);
  297. const Function &func = functions[p_func];
  298. return func.nodes.has(p_id);
  299. }
  300. Ref<VisualScriptNode> VisualScript::get_node(const StringName &p_func, int p_id) const {
  301. ERR_FAIL_COND_V(!functions.has(p_func), Ref<VisualScriptNode>());
  302. const Function &func = functions[p_func];
  303. ERR_FAIL_COND_V(!func.nodes.has(p_id), Ref<VisualScriptNode>());
  304. return func.nodes[p_id].node;
  305. }
  306. void VisualScript::set_node_pos(const StringName &p_func, int p_id, const Point2 &p_pos) {
  307. ERR_FAIL_COND(instances.size());
  308. ERR_FAIL_COND(!functions.has(p_func));
  309. Function &func = functions[p_func];
  310. ERR_FAIL_COND(!func.nodes.has(p_id));
  311. func.nodes[p_id].pos = p_pos;
  312. }
  313. Point2 VisualScript::get_node_pos(const StringName &p_func, int p_id) const {
  314. ERR_FAIL_COND_V(!functions.has(p_func), Point2());
  315. const Function &func = functions[p_func];
  316. ERR_FAIL_COND_V(!func.nodes.has(p_id), Point2());
  317. return func.nodes[p_id].pos;
  318. }
  319. void VisualScript::get_node_list(const StringName &p_func, List<int> *r_nodes) const {
  320. ERR_FAIL_COND(!functions.has(p_func));
  321. const Function &func = functions[p_func];
  322. for (const Map<int, Function::NodeData>::Element *E = func.nodes.front(); E; E = E->next()) {
  323. r_nodes->push_back(E->key());
  324. }
  325. }
  326. void VisualScript::sequence_connect(const StringName &p_func, int p_from_node, int p_from_output, int p_to_node) {
  327. ERR_FAIL_COND(instances.size());
  328. ERR_FAIL_COND(!functions.has(p_func));
  329. Function &func = functions[p_func];
  330. SequenceConnection sc;
  331. sc.from_node = p_from_node;
  332. sc.from_output = p_from_output;
  333. sc.to_node = p_to_node;
  334. ERR_FAIL_COND(func.sequence_connections.has(sc));
  335. func.sequence_connections.insert(sc);
  336. }
  337. void VisualScript::sequence_disconnect(const StringName &p_func, int p_from_node, int p_from_output, int p_to_node) {
  338. ERR_FAIL_COND(!functions.has(p_func));
  339. Function &func = functions[p_func];
  340. SequenceConnection sc;
  341. sc.from_node = p_from_node;
  342. sc.from_output = p_from_output;
  343. sc.to_node = p_to_node;
  344. ERR_FAIL_COND(!func.sequence_connections.has(sc));
  345. func.sequence_connections.erase(sc);
  346. }
  347. bool VisualScript::has_sequence_connection(const StringName &p_func, int p_from_node, int p_from_output, int p_to_node) const {
  348. ERR_FAIL_COND_V(!functions.has(p_func), false);
  349. const Function &func = functions[p_func];
  350. SequenceConnection sc;
  351. sc.from_node = p_from_node;
  352. sc.from_output = p_from_output;
  353. sc.to_node = p_to_node;
  354. return func.sequence_connections.has(sc);
  355. }
  356. void VisualScript::get_sequence_connection_list(const StringName &p_func, List<SequenceConnection> *r_connection) const {
  357. ERR_FAIL_COND(!functions.has(p_func));
  358. const Function &func = functions[p_func];
  359. for (const Set<SequenceConnection>::Element *E = func.sequence_connections.front(); E; E = E->next()) {
  360. r_connection->push_back(E->get());
  361. }
  362. }
  363. void VisualScript::data_connect(const StringName &p_func, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  364. ERR_FAIL_COND(instances.size());
  365. ERR_FAIL_COND(!functions.has(p_func));
  366. Function &func = functions[p_func];
  367. DataConnection dc;
  368. dc.from_node = p_from_node;
  369. dc.from_port = p_from_port;
  370. dc.to_node = p_to_node;
  371. dc.to_port = p_to_port;
  372. ERR_FAIL_COND(func.data_connections.has(dc));
  373. func.data_connections.insert(dc);
  374. }
  375. void VisualScript::data_disconnect(const StringName &p_func, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  376. ERR_FAIL_COND(!functions.has(p_func));
  377. Function &func = functions[p_func];
  378. DataConnection dc;
  379. dc.from_node = p_from_node;
  380. dc.from_port = p_from_port;
  381. dc.to_node = p_to_node;
  382. dc.to_port = p_to_port;
  383. ERR_FAIL_COND(!func.data_connections.has(dc));
  384. func.data_connections.erase(dc);
  385. }
  386. bool VisualScript::has_data_connection(const StringName &p_func, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const {
  387. ERR_FAIL_COND_V(!functions.has(p_func), false);
  388. const Function &func = functions[p_func];
  389. DataConnection dc;
  390. dc.from_node = p_from_node;
  391. dc.from_port = p_from_port;
  392. dc.to_node = p_to_node;
  393. dc.to_port = p_to_port;
  394. return func.data_connections.has(dc);
  395. }
  396. bool VisualScript::is_input_value_port_connected(const StringName &p_func, int p_node, int p_port) const {
  397. ERR_FAIL_COND_V(!functions.has(p_func), false);
  398. const Function &func = functions[p_func];
  399. for (const Set<DataConnection>::Element *E = func.data_connections.front(); E; E = E->next()) {
  400. if (E->get().to_node == p_node && E->get().to_port == p_port)
  401. return true;
  402. }
  403. return false;
  404. }
  405. bool VisualScript::get_input_value_port_connection_source(const StringName &p_func, int p_node, int p_port, int *r_node, int *r_port) const {
  406. ERR_FAIL_COND_V(!functions.has(p_func), false);
  407. const Function &func = functions[p_func];
  408. for (const Set<DataConnection>::Element *E = func.data_connections.front(); E; E = E->next()) {
  409. if (E->get().to_node == p_node && E->get().to_port == p_port) {
  410. *r_node = E->get().from_node;
  411. *r_port = E->get().from_port;
  412. return true;
  413. }
  414. }
  415. return false;
  416. }
  417. void VisualScript::get_data_connection_list(const StringName &p_func, List<DataConnection> *r_connection) const {
  418. ERR_FAIL_COND(!functions.has(p_func));
  419. const Function &func = functions[p_func];
  420. for (const Set<DataConnection>::Element *E = func.data_connections.front(); E; E = E->next()) {
  421. r_connection->push_back(E->get());
  422. }
  423. }
  424. void VisualScript::add_variable(const StringName &p_name, const Variant &p_default_value, bool p_export) {
  425. ERR_FAIL_COND(instances.size());
  426. ERR_FAIL_COND(!String(p_name).is_valid_identifier());
  427. ERR_FAIL_COND(variables.has(p_name));
  428. Variable v;
  429. v.default_value = p_default_value;
  430. v.info.type = p_default_value.get_type();
  431. v.info.name = p_name;
  432. v.info.hint = PROPERTY_HINT_NONE;
  433. v._export = p_export;
  434. variables[p_name] = v;
  435. #ifdef TOOLS_ENABLED
  436. _update_placeholders();
  437. #endif
  438. }
  439. bool VisualScript::has_variable(const StringName &p_name) const {
  440. return variables.has(p_name);
  441. }
  442. void VisualScript::remove_variable(const StringName &p_name) {
  443. ERR_FAIL_COND(!variables.has(p_name));
  444. variables.erase(p_name);
  445. #ifdef TOOLS_ENABLED
  446. _update_placeholders();
  447. #endif
  448. }
  449. void VisualScript::set_variable_default_value(const StringName &p_name, const Variant &p_value) {
  450. ERR_FAIL_COND(!variables.has(p_name));
  451. variables[p_name].default_value = p_value;
  452. #ifdef TOOLS_ENABLED
  453. _update_placeholders();
  454. #endif
  455. }
  456. Variant VisualScript::get_variable_default_value(const StringName &p_name) const {
  457. ERR_FAIL_COND_V(!variables.has(p_name), Variant());
  458. return variables[p_name].default_value;
  459. }
  460. void VisualScript::set_variable_info(const StringName &p_name, const PropertyInfo &p_info) {
  461. ERR_FAIL_COND(instances.size());
  462. ERR_FAIL_COND(!variables.has(p_name));
  463. variables[p_name].info = p_info;
  464. variables[p_name].info.name = p_name;
  465. #ifdef TOOLS_ENABLED
  466. _update_placeholders();
  467. #endif
  468. }
  469. PropertyInfo VisualScript::get_variable_info(const StringName &p_name) const {
  470. ERR_FAIL_COND_V(!variables.has(p_name), PropertyInfo());
  471. return variables[p_name].info;
  472. }
  473. void VisualScript::set_variable_export(const StringName &p_name, bool p_export) {
  474. ERR_FAIL_COND(!variables.has(p_name));
  475. variables[p_name]._export = p_export;
  476. #ifdef TOOLS_ENABLED
  477. _update_placeholders();
  478. #endif
  479. }
  480. bool VisualScript::get_variable_export(const StringName &p_name) const {
  481. ERR_FAIL_COND_V(!variables.has(p_name), false);
  482. return variables[p_name]._export;
  483. }
  484. void VisualScript::_set_variable_info(const StringName &p_name, const Dictionary &p_info) {
  485. PropertyInfo pinfo;
  486. if (p_info.has("type"))
  487. pinfo.type = Variant::Type(int(p_info["type"]));
  488. if (p_info.has("name"))
  489. pinfo.name = p_info["name"];
  490. if (p_info.has("hint"))
  491. pinfo.hint = PropertyHint(int(p_info["hint"]));
  492. if (p_info.has("hint_string"))
  493. pinfo.hint_string = p_info["hint_string"];
  494. if (p_info.has("usage"))
  495. pinfo.usage = p_info["usage"];
  496. set_variable_info(p_name, pinfo);
  497. }
  498. Dictionary VisualScript::_get_variable_info(const StringName &p_name) const {
  499. PropertyInfo pinfo = get_variable_info(p_name);
  500. Dictionary d;
  501. d["type"] = pinfo.type;
  502. d["name"] = pinfo.name;
  503. d["hint"] = pinfo.hint;
  504. d["hint_string"] = pinfo.hint_string;
  505. d["usage"] = pinfo.usage;
  506. return d;
  507. }
  508. void VisualScript::get_variable_list(List<StringName> *r_variables) const {
  509. for (Map<StringName, Variable>::Element *E = variables.front(); E; E = E->next()) {
  510. r_variables->push_back(E->key());
  511. }
  512. r_variables->sort_custom<StringName::AlphCompare>();
  513. }
  514. void VisualScript::set_instance_base_type(const StringName &p_type) {
  515. ERR_FAIL_COND(instances.size());
  516. base_type = p_type;
  517. }
  518. void VisualScript::rename_variable(const StringName &p_name, const StringName &p_new_name) {
  519. ERR_FAIL_COND(instances.size());
  520. ERR_FAIL_COND(!variables.has(p_name));
  521. if (p_new_name == p_name)
  522. return;
  523. ERR_FAIL_COND(!String(p_new_name).is_valid_identifier());
  524. ERR_FAIL_COND(functions.has(p_new_name));
  525. ERR_FAIL_COND(variables.has(p_new_name));
  526. ERR_FAIL_COND(custom_signals.has(p_new_name));
  527. variables[p_new_name] = variables[p_name];
  528. variables.erase(p_name);
  529. }
  530. void VisualScript::add_custom_signal(const StringName &p_name) {
  531. ERR_FAIL_COND(instances.size());
  532. ERR_FAIL_COND(!String(p_name).is_valid_identifier());
  533. ERR_FAIL_COND(custom_signals.has(p_name));
  534. custom_signals[p_name] = Vector<Argument>();
  535. }
  536. bool VisualScript::has_custom_signal(const StringName &p_name) const {
  537. return custom_signals.has(p_name);
  538. }
  539. void VisualScript::custom_signal_add_argument(const StringName &p_func, Variant::Type p_type, const String &p_name, int p_index) {
  540. ERR_FAIL_COND(instances.size());
  541. ERR_FAIL_COND(!custom_signals.has(p_func));
  542. Argument arg;
  543. arg.type = p_type;
  544. arg.name = p_name;
  545. if (p_index < 0)
  546. custom_signals[p_func].push_back(arg);
  547. else
  548. custom_signals[p_func].insert(0, arg);
  549. }
  550. void VisualScript::custom_signal_set_argument_type(const StringName &p_func, int p_argidx, Variant::Type p_type) {
  551. ERR_FAIL_COND(instances.size());
  552. ERR_FAIL_COND(!custom_signals.has(p_func));
  553. ERR_FAIL_INDEX(p_argidx, custom_signals[p_func].size());
  554. custom_signals[p_func][p_argidx].type = p_type;
  555. }
  556. Variant::Type VisualScript::custom_signal_get_argument_type(const StringName &p_func, int p_argidx) const {
  557. ERR_FAIL_COND_V(!custom_signals.has(p_func), Variant::NIL);
  558. ERR_FAIL_INDEX_V(p_argidx, custom_signals[p_func].size(), Variant::NIL);
  559. return custom_signals[p_func][p_argidx].type;
  560. }
  561. void VisualScript::custom_signal_set_argument_name(const StringName &p_func, int p_argidx, const String &p_name) {
  562. ERR_FAIL_COND(instances.size());
  563. ERR_FAIL_COND(!custom_signals.has(p_func));
  564. ERR_FAIL_INDEX(p_argidx, custom_signals[p_func].size());
  565. custom_signals[p_func][p_argidx].name = p_name;
  566. }
  567. String VisualScript::custom_signal_get_argument_name(const StringName &p_func, int p_argidx) const {
  568. ERR_FAIL_COND_V(!custom_signals.has(p_func), String());
  569. ERR_FAIL_INDEX_V(p_argidx, custom_signals[p_func].size(), String());
  570. return custom_signals[p_func][p_argidx].name;
  571. }
  572. void VisualScript::custom_signal_remove_argument(const StringName &p_func, int p_argidx) {
  573. ERR_FAIL_COND(instances.size());
  574. ERR_FAIL_COND(!custom_signals.has(p_func));
  575. ERR_FAIL_INDEX(p_argidx, custom_signals[p_func].size());
  576. custom_signals[p_func].remove(p_argidx);
  577. }
  578. int VisualScript::custom_signal_get_argument_count(const StringName &p_func) const {
  579. ERR_FAIL_COND_V(!custom_signals.has(p_func), 0);
  580. return custom_signals[p_func].size();
  581. }
  582. void VisualScript::custom_signal_swap_argument(const StringName &p_func, int p_argidx, int p_with_argidx) {
  583. ERR_FAIL_COND(instances.size());
  584. ERR_FAIL_COND(!custom_signals.has(p_func));
  585. ERR_FAIL_INDEX(p_argidx, custom_signals[p_func].size());
  586. ERR_FAIL_INDEX(p_with_argidx, custom_signals[p_func].size());
  587. SWAP(custom_signals[p_func][p_argidx], custom_signals[p_func][p_with_argidx]);
  588. }
  589. void VisualScript::remove_custom_signal(const StringName &p_name) {
  590. ERR_FAIL_COND(instances.size());
  591. ERR_FAIL_COND(!custom_signals.has(p_name));
  592. custom_signals.erase(p_name);
  593. }
  594. void VisualScript::rename_custom_signal(const StringName &p_name, const StringName &p_new_name) {
  595. ERR_FAIL_COND(instances.size());
  596. ERR_FAIL_COND(!custom_signals.has(p_name));
  597. if (p_new_name == p_name)
  598. return;
  599. ERR_FAIL_COND(!String(p_new_name).is_valid_identifier());
  600. ERR_FAIL_COND(functions.has(p_new_name));
  601. ERR_FAIL_COND(variables.has(p_new_name));
  602. ERR_FAIL_COND(custom_signals.has(p_new_name));
  603. custom_signals[p_new_name] = custom_signals[p_name];
  604. custom_signals.erase(p_name);
  605. }
  606. void VisualScript::get_custom_signal_list(List<StringName> *r_custom_signals) const {
  607. for (const Map<StringName, Vector<Argument> >::Element *E = custom_signals.front(); E; E = E->next()) {
  608. r_custom_signals->push_back(E->key());
  609. }
  610. r_custom_signals->sort_custom<StringName::AlphCompare>();
  611. }
  612. int VisualScript::get_available_id() const {
  613. int max_id = 0;
  614. for (Map<StringName, Function>::Element *E = functions.front(); E; E = E->next()) {
  615. if (E->get().nodes.empty())
  616. continue;
  617. int last_id = E->get().nodes.back()->key();
  618. max_id = MAX(max_id, last_id + 1);
  619. }
  620. return max_id;
  621. }
  622. /////////////////////////////////
  623. bool VisualScript::can_instance() const {
  624. return true; //ScriptServer::is_scripting_enabled();
  625. }
  626. StringName VisualScript::get_instance_base_type() const {
  627. return base_type;
  628. }
  629. Ref<Script> VisualScript::get_base_script() const {
  630. return Ref<Script>(); // no inheritance in visual script
  631. }
  632. #ifdef TOOLS_ENABLED
  633. void VisualScript::_placeholder_erased(PlaceHolderScriptInstance *p_placeholder) {
  634. placeholders.erase(p_placeholder);
  635. }
  636. void VisualScript::_update_placeholders() {
  637. if (placeholders.size() == 0)
  638. return; //no bother if no placeholders
  639. List<PropertyInfo> pinfo;
  640. Map<StringName, Variant> values;
  641. for (Map<StringName, Variable>::Element *E = variables.front(); E; E = E->next()) {
  642. if (!E->get()._export)
  643. continue;
  644. PropertyInfo p = E->get().info;
  645. p.name = String(E->key());
  646. pinfo.push_back(p);
  647. values[p.name] = E->get().default_value;
  648. }
  649. for (Set<PlaceHolderScriptInstance *>::Element *E = placeholders.front(); E; E = E->next()) {
  650. E->get()->update(pinfo, values);
  651. }
  652. }
  653. #endif
  654. ScriptInstance *VisualScript::instance_create(Object *p_this) {
  655. #ifdef TOOLS_ENABLED
  656. if (!ScriptServer::is_scripting_enabled()) {
  657. PlaceHolderScriptInstance *sins = memnew(PlaceHolderScriptInstance(VisualScriptLanguage::singleton, Ref<Script>((Script *)this), p_this));
  658. placeholders.insert(sins);
  659. List<PropertyInfo> pinfo;
  660. Map<StringName, Variant> values;
  661. for (Map<StringName, Variable>::Element *E = variables.front(); E; E = E->next()) {
  662. if (!E->get()._export)
  663. continue;
  664. PropertyInfo p = E->get().info;
  665. p.name = String(E->key());
  666. pinfo.push_back(p);
  667. values[p.name] = E->get().default_value;
  668. }
  669. sins->update(pinfo, values);
  670. return sins;
  671. }
  672. #endif
  673. VisualScriptInstance *instance = memnew(VisualScriptInstance);
  674. instance->create(Ref<VisualScript>(this), p_this);
  675. if (VisualScriptLanguage::singleton->lock)
  676. VisualScriptLanguage::singleton->lock->lock();
  677. instances[p_this] = instance;
  678. if (VisualScriptLanguage::singleton->lock)
  679. VisualScriptLanguage::singleton->lock->unlock();
  680. return instance;
  681. }
  682. bool VisualScript::instance_has(const Object *p_this) const {
  683. return instances.has((Object *)p_this);
  684. }
  685. bool VisualScript::has_source_code() const {
  686. return false;
  687. }
  688. String VisualScript::get_source_code() const {
  689. return String();
  690. }
  691. void VisualScript::set_source_code(const String &p_code) {
  692. }
  693. Error VisualScript::reload(bool p_keep_state) {
  694. return OK;
  695. }
  696. bool VisualScript::is_tool() const {
  697. return false;
  698. }
  699. String VisualScript::get_node_type() const {
  700. return String();
  701. }
  702. ScriptLanguage *VisualScript::get_language() const {
  703. return VisualScriptLanguage::singleton;
  704. }
  705. bool VisualScript::has_script_signal(const StringName &p_signal) const {
  706. return custom_signals.has(p_signal);
  707. }
  708. void VisualScript::get_script_signal_list(List<MethodInfo> *r_signals) const {
  709. for (const Map<StringName, Vector<Argument> >::Element *E = custom_signals.front(); E; E = E->next()) {
  710. MethodInfo mi;
  711. mi.name = E->key();
  712. for (int i = 0; i < E->get().size(); i++) {
  713. PropertyInfo arg;
  714. arg.type = E->get()[i].type;
  715. arg.name = E->get()[i].name;
  716. mi.arguments.push_back(arg);
  717. }
  718. r_signals->push_back(mi);
  719. }
  720. }
  721. bool VisualScript::get_property_default_value(const StringName &p_property, Variant &r_value) const {
  722. if (!variables.has(p_property))
  723. return false;
  724. r_value = variables[p_property].default_value;
  725. return true;
  726. }
  727. void VisualScript::get_script_method_list(List<MethodInfo> *p_list) const {
  728. for (Map<StringName, Function>::Element *E = functions.front(); E; E = E->next()) {
  729. MethodInfo mi;
  730. mi.name = E->key();
  731. if (E->get().function_id >= 0) {
  732. Ref<VisualScriptFunction> func = E->get().nodes[E->get().function_id].node;
  733. if (func.is_valid()) {
  734. for (int i = 0; i < func->get_argument_count(); i++) {
  735. PropertyInfo arg;
  736. arg.name = func->get_argument_name(i);
  737. arg.type = func->get_argument_type(i);
  738. mi.arguments.push_back(arg);
  739. }
  740. }
  741. }
  742. p_list->push_back(mi);
  743. }
  744. }
  745. bool VisualScript::has_method(const StringName &p_method) const {
  746. return functions.has(p_method);
  747. }
  748. MethodInfo VisualScript::get_method_info(const StringName &p_method) const {
  749. const Map<StringName, Function>::Element *E = functions.find(p_method);
  750. if (!E)
  751. return MethodInfo();
  752. MethodInfo mi;
  753. mi.name = E->key();
  754. if (E->get().function_id >= 0) {
  755. Ref<VisualScriptFunction> func = E->get().nodes[E->get().function_id].node;
  756. if (func.is_valid()) {
  757. for (int i = 0; i < func->get_argument_count(); i++) {
  758. PropertyInfo arg;
  759. arg.name = func->get_argument_name(i);
  760. arg.type = func->get_argument_type(i);
  761. mi.arguments.push_back(arg);
  762. }
  763. if (!func->is_sequenced()) {
  764. mi.flags |= METHOD_FLAG_CONST;
  765. }
  766. }
  767. }
  768. return mi;
  769. }
  770. void VisualScript::get_script_property_list(List<PropertyInfo> *p_list) const {
  771. List<StringName> vars;
  772. get_variable_list(&vars);
  773. for (List<StringName>::Element *E = vars.front(); E; E = E->next()) {
  774. //if (!variables[E->get()]._export)
  775. // continue;
  776. PropertyInfo pi = variables[E->get()].info;
  777. pi.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  778. p_list->push_back(pi);
  779. }
  780. }
  781. int VisualScript::get_member_line(const StringName &p_member) const {
  782. #ifdef TOOLS_ENABLED
  783. if (has_function(p_member)) {
  784. for (Map<int, Function::NodeData>::Element *E = functions[p_member].nodes.front(); E; E = E->next()) {
  785. if (E->get().node->cast_to<VisualScriptFunction>())
  786. return E->key();
  787. }
  788. }
  789. #endif
  790. return -1;
  791. }
  792. #ifdef TOOLS_ENABLED
  793. bool VisualScript::are_subnodes_edited() const {
  794. for (const Map<StringName, Function>::Element *E = functions.front(); E; E = E->next()) {
  795. for (const Map<int, Function::NodeData>::Element *F = E->get().nodes.front(); F; F = F->next()) {
  796. if (F->get().node->is_edited()) {
  797. return true;
  798. }
  799. }
  800. }
  801. return false;
  802. }
  803. #endif
  804. void VisualScript::_set_data(const Dictionary &p_data) {
  805. Dictionary d = p_data;
  806. if (d.has("base_type"))
  807. base_type = d["base_type"];
  808. variables.clear();
  809. Array vars = d["variables"];
  810. for (int i = 0; i < vars.size(); i++) {
  811. Dictionary v = vars[i];
  812. StringName name = v["name"];
  813. add_variable(name);
  814. _set_variable_info(name, v);
  815. set_variable_default_value(name, v["default_value"]);
  816. set_variable_export(name, v.has("export") && bool(v["export"]));
  817. }
  818. custom_signals.clear();
  819. Array sigs = d["signals"];
  820. for (int i = 0; i < sigs.size(); i++) {
  821. Dictionary cs = sigs[i];
  822. add_custom_signal(cs["name"]);
  823. Array args = cs["arguments"];
  824. for (int j = 0; j < args.size(); j += 2) {
  825. custom_signal_add_argument(cs["name"], Variant::Type(int(args[j + 1])), args[j]);
  826. }
  827. }
  828. Array funcs = d["functions"];
  829. functions.clear();
  830. for (int i = 0; i < funcs.size(); i++) {
  831. Dictionary func = funcs[i];
  832. StringName name = func["name"];
  833. //int id=func["function_id"];
  834. add_function(name);
  835. set_function_scroll(name, func["scroll"]);
  836. Array nodes = func["nodes"];
  837. for (int i = 0; i < nodes.size(); i += 3) {
  838. add_node(name, nodes[i], nodes[i + 2], nodes[i + 1]);
  839. }
  840. Array sequence_connections = func["sequence_connections"];
  841. for (int j = 0; j < sequence_connections.size(); j += 3) {
  842. sequence_connect(name, sequence_connections[j + 0], sequence_connections[j + 1], sequence_connections[j + 2]);
  843. }
  844. Array data_connections = func["data_connections"];
  845. for (int j = 0; j < data_connections.size(); j += 4) {
  846. data_connect(name, data_connections[j + 0], data_connections[j + 1], data_connections[j + 2], data_connections[j + 3]);
  847. }
  848. }
  849. }
  850. Dictionary VisualScript::_get_data() const {
  851. Dictionary d;
  852. d["base_type"] = base_type;
  853. Array vars;
  854. for (const Map<StringName, Variable>::Element *E = variables.front(); E; E = E->next()) {
  855. Dictionary var = _get_variable_info(E->key());
  856. var["name"] = E->key(); //make sure it's the right one
  857. var["default_value"] = E->get().default_value;
  858. var["export"] = E->get()._export;
  859. vars.push_back(var);
  860. }
  861. d["variables"] = vars;
  862. Array sigs;
  863. for (const Map<StringName, Vector<Argument> >::Element *E = custom_signals.front(); E; E = E->next()) {
  864. Dictionary cs;
  865. cs["name"] = E->key();
  866. Array args;
  867. for (int i = 0; i < E->get().size(); i++) {
  868. args.push_back(E->get()[i].name);
  869. args.push_back(E->get()[i].type);
  870. }
  871. cs["arguments"] = args;
  872. sigs.push_back(cs);
  873. }
  874. d["signals"] = sigs;
  875. Array funcs;
  876. for (const Map<StringName, Function>::Element *E = functions.front(); E; E = E->next()) {
  877. Dictionary func;
  878. func["name"] = E->key();
  879. func["function_id"] = E->get().function_id;
  880. func["scroll"] = E->get().scroll;
  881. Array nodes;
  882. for (const Map<int, Function::NodeData>::Element *F = E->get().nodes.front(); F; F = F->next()) {
  883. nodes.push_back(F->key());
  884. nodes.push_back(F->get().pos);
  885. nodes.push_back(F->get().node);
  886. }
  887. func["nodes"] = nodes;
  888. Array sequence_connections;
  889. for (const Set<SequenceConnection>::Element *F = E->get().sequence_connections.front(); F; F = F->next()) {
  890. sequence_connections.push_back(F->get().from_node);
  891. sequence_connections.push_back(F->get().from_output);
  892. sequence_connections.push_back(F->get().to_node);
  893. }
  894. func["sequence_connections"] = sequence_connections;
  895. Array data_connections;
  896. for (const Set<DataConnection>::Element *F = E->get().data_connections.front(); F; F = F->next()) {
  897. data_connections.push_back(F->get().from_node);
  898. data_connections.push_back(F->get().from_port);
  899. data_connections.push_back(F->get().to_node);
  900. data_connections.push_back(F->get().to_port);
  901. }
  902. func["data_connections"] = data_connections;
  903. funcs.push_back(func);
  904. }
  905. d["functions"] = funcs;
  906. return d;
  907. }
  908. void VisualScript::_bind_methods() {
  909. ClassDB::bind_method(D_METHOD("_node_ports_changed"), &VisualScript::_node_ports_changed);
  910. ClassDB::bind_method(D_METHOD("add_function", "name"), &VisualScript::add_function);
  911. ClassDB::bind_method(D_METHOD("has_function", "name"), &VisualScript::has_function);
  912. ClassDB::bind_method(D_METHOD("remove_function", "name"), &VisualScript::remove_function);
  913. ClassDB::bind_method(D_METHOD("rename_function", "name", "new_name"), &VisualScript::rename_function);
  914. ClassDB::bind_method(D_METHOD("set_function_scroll", "name", "ofs"), &VisualScript::set_function_scroll);
  915. ClassDB::bind_method(D_METHOD("get_function_scroll", "name"), &VisualScript::get_function_scroll);
  916. ClassDB::bind_method(D_METHOD("add_node", "func", "id", "node", "pos"), &VisualScript::add_node, DEFVAL(Point2()));
  917. ClassDB::bind_method(D_METHOD("remove_node", "func", "id"), &VisualScript::remove_node);
  918. ClassDB::bind_method(D_METHOD("get_function_node_id", "name"), &VisualScript::get_function_node_id);
  919. ClassDB::bind_method(D_METHOD("get_node", "func", "id"), &VisualScript::get_node);
  920. ClassDB::bind_method(D_METHOD("has_node", "func", "id"), &VisualScript::has_node);
  921. ClassDB::bind_method(D_METHOD("set_node_pos", "func", "id", "pos"), &VisualScript::set_node_pos);
  922. ClassDB::bind_method(D_METHOD("get_node_pos", "func", "id"), &VisualScript::get_node_pos);
  923. ClassDB::bind_method(D_METHOD("sequence_connect", "func", "from_node", "from_output", "to_node"), &VisualScript::sequence_connect);
  924. ClassDB::bind_method(D_METHOD("sequence_disconnect", "func", "from_node", "from_output", "to_node"), &VisualScript::sequence_disconnect);
  925. ClassDB::bind_method(D_METHOD("has_sequence_connection", "func", "from_node", "from_output", "to_node"), &VisualScript::has_sequence_connection);
  926. ClassDB::bind_method(D_METHOD("data_connect", "func", "from_node", "from_port", "to_node", "to_port"), &VisualScript::data_connect);
  927. ClassDB::bind_method(D_METHOD("data_disconnect", "func", "from_node", "from_port", "to_node", "to_port"), &VisualScript::data_disconnect);
  928. ClassDB::bind_method(D_METHOD("has_data_connection", "func", "from_node", "from_port", "to_node", "to_port"), &VisualScript::has_data_connection);
  929. ClassDB::bind_method(D_METHOD("add_variable", "name", "default_value", "export"), &VisualScript::add_variable, DEFVAL(Variant()), DEFVAL(false));
  930. ClassDB::bind_method(D_METHOD("has_variable", "name"), &VisualScript::has_variable);
  931. ClassDB::bind_method(D_METHOD("remove_variable", "name"), &VisualScript::remove_variable);
  932. ClassDB::bind_method(D_METHOD("set_variable_default_value", "name", "value"), &VisualScript::set_variable_default_value);
  933. ClassDB::bind_method(D_METHOD("get_variable_default_value", "name"), &VisualScript::get_variable_default_value);
  934. ClassDB::bind_method(D_METHOD("set_variable_info", "name", "value"), &VisualScript::_set_variable_info);
  935. ClassDB::bind_method(D_METHOD("get_variable_info", "name"), &VisualScript::_get_variable_info);
  936. ClassDB::bind_method(D_METHOD("set_variable_export", "name", "enable"), &VisualScript::set_variable_export);
  937. ClassDB::bind_method(D_METHOD("get_variable_export", "name"), &VisualScript::get_variable_export);
  938. ClassDB::bind_method(D_METHOD("rename_variable", "name", "new_name"), &VisualScript::rename_variable);
  939. ClassDB::bind_method(D_METHOD("add_custom_signal", "name"), &VisualScript::add_custom_signal);
  940. ClassDB::bind_method(D_METHOD("has_custom_signal", "name"), &VisualScript::has_custom_signal);
  941. ClassDB::bind_method(D_METHOD("custom_signal_add_argument", "name", "type", "argname", "index"), &VisualScript::custom_signal_add_argument, DEFVAL(-1));
  942. ClassDB::bind_method(D_METHOD("custom_signal_set_argument_type", "name", "argidx", "type"), &VisualScript::custom_signal_set_argument_type);
  943. ClassDB::bind_method(D_METHOD("custom_signal_get_argument_type", "name", "argidx"), &VisualScript::custom_signal_get_argument_type);
  944. ClassDB::bind_method(D_METHOD("custom_signal_set_argument_name", "name", "argidx", "argname"), &VisualScript::custom_signal_set_argument_name);
  945. ClassDB::bind_method(D_METHOD("custom_signal_get_argument_name", "name", "argidx"), &VisualScript::custom_signal_get_argument_name);
  946. ClassDB::bind_method(D_METHOD("custom_signal_remove_argument", "name", "argidx"), &VisualScript::custom_signal_remove_argument);
  947. ClassDB::bind_method(D_METHOD("custom_signal_get_argument_count", "name"), &VisualScript::custom_signal_get_argument_count);
  948. ClassDB::bind_method(D_METHOD("custom_signal_swap_argument", "name", "argidx", "withidx"), &VisualScript::custom_signal_swap_argument);
  949. ClassDB::bind_method(D_METHOD("remove_custom_signal", "name"), &VisualScript::remove_custom_signal);
  950. ClassDB::bind_method(D_METHOD("rename_custom_signal", "name", "new_name"), &VisualScript::rename_custom_signal);
  951. //ClassDB::bind_method(D_METHOD("set_variable_info","name","info"),&VScript::set_variable_info);
  952. //ClassDB::bind_method(D_METHOD("get_variable_info","name"),&VScript::set_variable_info);
  953. ClassDB::bind_method(D_METHOD("set_instance_base_type", "type"), &VisualScript::set_instance_base_type);
  954. ClassDB::bind_method(D_METHOD("_set_data", "data"), &VisualScript::_set_data);
  955. ClassDB::bind_method(D_METHOD("_get_data"), &VisualScript::_get_data);
  956. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_data", "_get_data");
  957. ADD_SIGNAL(MethodInfo("node_ports_changed", PropertyInfo(Variant::STRING, "function"), PropertyInfo(Variant::INT, "id")));
  958. }
  959. VisualScript::VisualScript() {
  960. base_type = "Object";
  961. }
  962. VisualScript::~VisualScript() {
  963. while (!functions.empty()) {
  964. remove_function(functions.front()->key());
  965. }
  966. }
  967. ////////////////////////////////////////////
  968. bool VisualScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  969. Map<StringName, Variant>::Element *E = variables.find(p_name);
  970. if (!E)
  971. return false;
  972. E->get() = p_value;
  973. return true;
  974. }
  975. bool VisualScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  976. const Map<StringName, Variant>::Element *E = variables.find(p_name);
  977. if (!E)
  978. return false;
  979. r_ret = E->get();
  980. return true;
  981. }
  982. void VisualScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  983. for (const Map<StringName, VisualScript::Variable>::Element *E = script->variables.front(); E; E = E->next()) {
  984. if (!E->get()._export)
  985. continue;
  986. PropertyInfo p = E->get().info;
  987. p.name = String(E->key());
  988. p.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  989. p_properties->push_back(p);
  990. }
  991. }
  992. Variant::Type VisualScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  993. const Map<StringName, VisualScript::Variable>::Element *E = script->variables.find(p_name);
  994. if (!E) {
  995. if (r_is_valid)
  996. *r_is_valid = false;
  997. ERR_FAIL_V(Variant::NIL);
  998. }
  999. if (r_is_valid)
  1000. *r_is_valid = true;
  1001. return E->get().info.type;
  1002. }
  1003. void VisualScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  1004. for (const Map<StringName, VisualScript::Function>::Element *E = script->functions.front(); E; E = E->next()) {
  1005. MethodInfo mi;
  1006. mi.name = E->key();
  1007. if (E->get().function_id >= 0 && E->get().nodes.has(E->get().function_id)) {
  1008. Ref<VisualScriptFunction> vsf = E->get().nodes[E->get().function_id].node;
  1009. if (vsf.is_valid()) {
  1010. for (int i = 0; i < vsf->get_argument_count(); i++) {
  1011. PropertyInfo arg;
  1012. arg.name = vsf->get_argument_name(i);
  1013. arg.type = vsf->get_argument_type(i);
  1014. mi.arguments.push_back(arg);
  1015. }
  1016. if (!vsf->is_sequenced()) { //assumed constant if not sequenced
  1017. mi.flags |= METHOD_FLAG_CONST;
  1018. }
  1019. //vsf->Get_ for now at least it does not return..
  1020. }
  1021. }
  1022. p_list->push_back(mi);
  1023. }
  1024. }
  1025. bool VisualScriptInstance::has_method(const StringName &p_method) const {
  1026. return script->functions.has(p_method);
  1027. }
  1028. //#define VSDEBUG(m_text) print_line(m_text)
  1029. #define VSDEBUG(m_text)
  1030. void VisualScriptInstance::_dependency_step(VisualScriptNodeInstance *node, int p_pass, int *pass_stack, const Variant **input_args, Variant **output_args, Variant *variant_stack, Variant::CallError &r_error, String &error_str, VisualScriptNodeInstance **r_error_node) {
  1031. ERR_FAIL_COND(node->pass_idx == -1);
  1032. if (pass_stack[node->pass_idx] == p_pass)
  1033. return;
  1034. pass_stack[node->pass_idx] = p_pass;
  1035. if (!node->dependencies.empty()) {
  1036. int dc = node->dependencies.size();
  1037. VisualScriptNodeInstance **deps = node->dependencies.ptr();
  1038. for (int i = 0; i < dc; i++) {
  1039. _dependency_step(deps[i], p_pass, pass_stack, input_args, output_args, variant_stack, r_error, error_str, r_error_node);
  1040. if (r_error.error != Variant::CallError::CALL_OK)
  1041. return;
  1042. }
  1043. }
  1044. for (int i = 0; i < node->input_port_count; i++) {
  1045. int index = node->input_ports[i] & VisualScriptNodeInstance::INPUT_MASK;
  1046. if (node->input_ports[i] & VisualScriptNodeInstance::INPUT_DEFAULT_VALUE_BIT) {
  1047. //is a default value (unassigned input port)
  1048. input_args[i] = &default_values[index];
  1049. } else {
  1050. //regular temporary in stack
  1051. input_args[i] = &variant_stack[index];
  1052. }
  1053. }
  1054. for (int i = 0; i < node->output_port_count; i++) {
  1055. output_args[i] = &variant_stack[node->output_ports[i]];
  1056. }
  1057. Variant *working_mem = node->working_mem_idx >= 0 ? &variant_stack[node->working_mem_idx] : (Variant *)NULL;
  1058. node->step(input_args, output_args, VisualScriptNodeInstance::START_MODE_BEGIN_SEQUENCE, working_mem, r_error, error_str);
  1059. //ignore return
  1060. if (r_error.error != Variant::CallError::CALL_OK) {
  1061. *r_error_node = node;
  1062. }
  1063. }
  1064. Variant VisualScriptInstance::_call_internal(const StringName &p_method, void *p_stack, int p_stack_size, VisualScriptNodeInstance *p_node, int p_flow_stack_pos, int p_pass, bool p_resuming_yield, Variant::CallError &r_error) {
  1065. Map<StringName, Function>::Element *F = functions.find(p_method);
  1066. ERR_FAIL_COND_V(!F, Variant());
  1067. Function *f = &F->get();
  1068. //this call goes separate, so it can e yielded and suspended
  1069. Variant *variant_stack = (Variant *)p_stack;
  1070. bool *sequence_bits = (bool *)(variant_stack + f->max_stack);
  1071. const Variant **input_args = (const Variant **)(sequence_bits + f->node_count);
  1072. Variant **output_args = (Variant **)(input_args + max_input_args);
  1073. int flow_max = f->flow_stack_size;
  1074. int *flow_stack = flow_max ? (int *)(output_args + max_output_args) : (int *)NULL;
  1075. int *pass_stack = flow_stack + flow_max;
  1076. String error_str;
  1077. VisualScriptNodeInstance *node = p_node;
  1078. bool error = false;
  1079. int current_node_id = f->node;
  1080. Variant return_value;
  1081. Variant *working_mem = NULL;
  1082. int flow_stack_pos = p_flow_stack_pos;
  1083. #ifdef DEBUG_ENABLED
  1084. if (ScriptDebugger::get_singleton()) {
  1085. VisualScriptLanguage::singleton->enter_function(this, &p_method, variant_stack, &working_mem, &current_node_id);
  1086. }
  1087. #endif
  1088. while (true) {
  1089. p_pass++; //increment pass
  1090. current_node_id = node->get_id();
  1091. VSDEBUG("==========AT NODE: " + itos(current_node_id) + " base: " + node->get_base_node()->get_class_name());
  1092. VSDEBUG("AT STACK POS: " + itos(flow_stack_pos));
  1093. //setup working mem
  1094. working_mem = node->working_mem_idx >= 0 ? &variant_stack[node->working_mem_idx] : (Variant *)NULL;
  1095. VSDEBUG("WORKING MEM: " + itos(node->working_mem_idx));
  1096. if (current_node_id == f->node) {
  1097. //if function node, set up function arguments from beginning of stack
  1098. for (int i = 0; i < f->argument_count; i++) {
  1099. input_args[i] = &variant_stack[i];
  1100. }
  1101. } else {
  1102. //run dependencies first
  1103. if (!node->dependencies.empty()) {
  1104. int dc = node->dependencies.size();
  1105. VisualScriptNodeInstance **deps = node->dependencies.ptr();
  1106. for (int i = 0; i < dc; i++) {
  1107. _dependency_step(deps[i], p_pass, pass_stack, input_args, output_args, variant_stack, r_error, error_str, &node);
  1108. if (r_error.error != Variant::CallError::CALL_OK) {
  1109. error = true;
  1110. current_node_id = node->id;
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. if (!error) {
  1116. //setup input pointers normally
  1117. VSDEBUG("INPUT PORTS: " + itos(node->input_port_count));
  1118. for (int i = 0; i < node->input_port_count; i++) {
  1119. int index = node->input_ports[i] & VisualScriptNodeInstance::INPUT_MASK;
  1120. if (node->input_ports[i] & VisualScriptNodeInstance::INPUT_DEFAULT_VALUE_BIT) {
  1121. //is a default value (unassigned input port)
  1122. input_args[i] = &default_values[index];
  1123. VSDEBUG("\tPORT " + itos(i) + " DEFAULT VAL");
  1124. } else {
  1125. //regular temporary in stack
  1126. input_args[i] = &variant_stack[index];
  1127. VSDEBUG("PORT " + itos(i) + " AT STACK " + itos(index));
  1128. }
  1129. }
  1130. }
  1131. }
  1132. if (error)
  1133. break;
  1134. //setup output pointers
  1135. VSDEBUG("OUTPUT PORTS: " + itos(node->output_port_count));
  1136. for (int i = 0; i < node->output_port_count; i++) {
  1137. output_args[i] = &variant_stack[node->output_ports[i]];
  1138. VSDEBUG("PORT " + itos(i) + " AT STACK " + itos(node->output_ports[i]));
  1139. }
  1140. //do step
  1141. VisualScriptNodeInstance::StartMode start_mode;
  1142. {
  1143. if (p_resuming_yield)
  1144. start_mode = VisualScriptNodeInstance::START_MODE_RESUME_YIELD;
  1145. else if (!flow_stack || !(flow_stack[flow_stack_pos] & VisualScriptNodeInstance::FLOW_STACK_PUSHED_BIT)) //if there is a push bit, it means we are continuing a sequence
  1146. start_mode = VisualScriptNodeInstance::START_MODE_BEGIN_SEQUENCE;
  1147. else
  1148. start_mode = VisualScriptNodeInstance::START_MODE_CONTINUE_SEQUENCE;
  1149. }
  1150. VSDEBUG("STEP - STARTSEQ: " + itos(start_mode));
  1151. int ret = node->step(input_args, output_args, start_mode, working_mem, r_error, error_str);
  1152. if (r_error.error != Variant::CallError::CALL_OK) {
  1153. //use error from step
  1154. error = true;
  1155. break;
  1156. }
  1157. if (ret & VisualScriptNodeInstance::STEP_YIELD_BIT) {
  1158. //yielded!
  1159. if (node->get_working_memory_size() == 0) {
  1160. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1161. error_str = RTR("A node yielded without working memory, please read the docs on how to yield properly!");
  1162. error = true;
  1163. break;
  1164. } else {
  1165. Ref<VisualScriptFunctionState> state = *working_mem;
  1166. if (!state.is_valid()) {
  1167. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1168. error_str = RTR("Node yielded, but did not return a function state in the first working memory.");
  1169. error = true;
  1170. break;
  1171. }
  1172. //step 1, capture all state
  1173. state->instance_id = get_owner_ptr()->get_instance_id();
  1174. state->script_id = get_script()->get_instance_id();
  1175. state->instance = this;
  1176. state->function = p_method;
  1177. state->working_mem_index = node->working_mem_idx;
  1178. state->variant_stack_size = f->max_stack;
  1179. state->node = node;
  1180. state->flow_stack_pos = flow_stack_pos;
  1181. state->stack.resize(p_stack_size);
  1182. state->pass = p_pass;
  1183. copymem(state->stack.ptr(), p_stack, p_stack_size);
  1184. //step 2, run away, return directly
  1185. r_error.error = Variant::CallError::CALL_OK;
  1186. #ifdef DEBUG_ENABLED
  1187. //will re-enter later, so exiting
  1188. if (ScriptDebugger::get_singleton()) {
  1189. VisualScriptLanguage::singleton->exit_function();
  1190. }
  1191. #endif
  1192. return state;
  1193. }
  1194. }
  1195. #ifdef DEBUG_ENABLED
  1196. if (ScriptDebugger::get_singleton()) {
  1197. // line
  1198. bool do_break = false;
  1199. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  1200. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  1201. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  1202. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  1203. do_break = true;
  1204. }
  1205. if (ScriptDebugger::get_singleton()->is_breakpoint(current_node_id, source))
  1206. do_break = true;
  1207. if (do_break) {
  1208. VisualScriptLanguage::singleton->debug_break("Breakpoint", true);
  1209. }
  1210. ScriptDebugger::get_singleton()->line_poll();
  1211. }
  1212. #endif
  1213. int output = ret & VisualScriptNodeInstance::STEP_MASK;
  1214. VSDEBUG("STEP RETURN: " + itos(ret));
  1215. if (ret & VisualScriptNodeInstance::STEP_EXIT_FUNCTION_BIT) {
  1216. if (node->get_working_memory_size() == 0) {
  1217. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1218. error_str = RTR("Return value must be assigned to first element of node working memory! Fix your node please.");
  1219. error = true;
  1220. } else {
  1221. //assign from working memory, first element
  1222. return_value = *working_mem;
  1223. }
  1224. VSDEBUG("EXITING FUNCTION - VALUE " + String(return_value));
  1225. break; //exit function requested, bye
  1226. }
  1227. VisualScriptNodeInstance *next = NULL; //next node
  1228. if ((ret == output || ret & VisualScriptNodeInstance::STEP_FLAG_PUSH_STACK_BIT) && node->sequence_output_count) {
  1229. //if no exit bit was set, and has sequence outputs, guess next node
  1230. if (output < 0 || output >= node->sequence_output_count) {
  1231. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1232. error_str = RTR("Node returned an invalid sequence output: ") + itos(output);
  1233. error = true;
  1234. break;
  1235. }
  1236. next = node->sequence_outputs[output];
  1237. if (next) {
  1238. VSDEBUG("GOT NEXT NODE - " + itos(next->get_id()));
  1239. } else {
  1240. VSDEBUG("GOT NEXT NODE - NULL");
  1241. }
  1242. }
  1243. if (flow_stack) {
  1244. //update flow stack pos (may have changed)
  1245. flow_stack[flow_stack_pos] = current_node_id;
  1246. //add stack push bit if requested
  1247. if (ret & VisualScriptNodeInstance::STEP_FLAG_PUSH_STACK_BIT) {
  1248. flow_stack[flow_stack_pos] |= VisualScriptNodeInstance::FLOW_STACK_PUSHED_BIT;
  1249. sequence_bits[node->sequence_index] = true; //remember sequence bit
  1250. VSDEBUG("NEXT SEQ - FLAG BIT");
  1251. } else {
  1252. sequence_bits[node->sequence_index] = false; //forget sequence bit
  1253. VSDEBUG("NEXT SEQ - NORMAL");
  1254. }
  1255. if (ret & VisualScriptNodeInstance::STEP_FLAG_GO_BACK_BIT) {
  1256. //go back request
  1257. if (flow_stack_pos > 0) {
  1258. flow_stack_pos--;
  1259. node = instances[flow_stack[flow_stack_pos] & VisualScriptNodeInstance::FLOW_STACK_MASK];
  1260. VSDEBUG("NEXT IS GO BACK");
  1261. } else {
  1262. VSDEBUG("NEXT IS GO BACK, BUT NO NEXT SO EXIT");
  1263. break; //simply exit without value or error
  1264. }
  1265. } else if (next) {
  1266. if (sequence_bits[next->sequence_index]) {
  1267. // what happened here is that we are entering a node that is in the middle of doing a sequence (pushed stack) from the front
  1268. // because each node has a working memory, we can't really do a sub-sequence
  1269. // as a result, the sequence will be restarted and the stack will roll back to find where this node
  1270. // started the sequence
  1271. bool found = false;
  1272. for (int i = flow_stack_pos; i >= 0; i--) {
  1273. if ((flow_stack[i] & VisualScriptNodeInstance::FLOW_STACK_MASK) == next->get_id()) {
  1274. flow_stack_pos = i; //roll back and remove bit
  1275. flow_stack[i] = next->get_id();
  1276. sequence_bits[next->sequence_index] = false;
  1277. found = true;
  1278. }
  1279. }
  1280. if (!found) {
  1281. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1282. error_str = RTR("Found sequence bit but not the node in the stack, report bug!");
  1283. error = true;
  1284. break;
  1285. }
  1286. node = next;
  1287. VSDEBUG("RE-ENTERED A LOOP, RETURNED STACK POS TO - " + itos(flow_stack_pos));
  1288. } else {
  1289. // check for stack overflow
  1290. if (flow_stack_pos + 1 >= flow_max) {
  1291. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1292. error_str = RTR("Stack overflow with stack depth: ") + itos(output);
  1293. error = true;
  1294. break;
  1295. }
  1296. node = next;
  1297. flow_stack_pos++;
  1298. flow_stack[flow_stack_pos] = node->get_id();
  1299. VSDEBUG("INCREASE FLOW STACK");
  1300. }
  1301. } else {
  1302. //no next node, try to go back in stack to pushed bit
  1303. bool found = false;
  1304. for (int i = flow_stack_pos; i >= 0; i--) {
  1305. VSDEBUG("FS " + itos(i) + " - " + itos(flow_stack[i]));
  1306. if (flow_stack[i] & VisualScriptNodeInstance::FLOW_STACK_PUSHED_BIT) {
  1307. node = instances[flow_stack[i] & VisualScriptNodeInstance::FLOW_STACK_MASK];
  1308. flow_stack_pos = i;
  1309. found = true;
  1310. break;
  1311. }
  1312. }
  1313. if (!found) {
  1314. VSDEBUG("NO NEXT NODE, NO GO BACK, EXITING");
  1315. break; //done, couldn't find a push stack bit
  1316. }
  1317. VSDEBUG("NO NEXT NODE, GO BACK TO: " + itos(flow_stack_pos));
  1318. }
  1319. } else {
  1320. node = next; //stackless mode, simply assign next node
  1321. }
  1322. }
  1323. if (error) {
  1324. //error
  1325. // function, file, line, error, explanation
  1326. String err_file = script->get_path();
  1327. String err_func = p_method;
  1328. int err_line = current_node_id; //not a line but it works as one
  1329. if (node && (r_error.error != Variant::CallError::CALL_ERROR_INVALID_METHOD || error_str == String())) {
  1330. if (error_str != String()) {
  1331. error_str += " ";
  1332. }
  1333. if (r_error.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1334. int errorarg = r_error.argument;
  1335. error_str += "Cannot convert argument " + itos(errorarg + 1) + " to " + Variant::get_type_name(r_error.expected) + ".";
  1336. } else if (r_error.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  1337. error_str += "Expected " + itos(r_error.argument) + " arguments.";
  1338. } else if (r_error.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  1339. error_str += "Expected " + itos(r_error.argument) + " arguments.";
  1340. } else if (r_error.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  1341. error_str += "Invalid Call.";
  1342. } else if (r_error.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  1343. error_str += "Base Instance is null";
  1344. }
  1345. }
  1346. //if (!GDScriptLanguage::get_singleton()->debug_break(err_text,false)) {
  1347. // debugger break did not happen
  1348. if (!VisualScriptLanguage::singleton->debug_break(error_str, false)) {
  1349. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, error_str.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1350. }
  1351. //}
  1352. } else {
  1353. //return_value=
  1354. }
  1355. #ifdef DEBUG_ENABLED
  1356. if (ScriptDebugger::get_singleton()) {
  1357. VisualScriptLanguage::singleton->exit_function();
  1358. }
  1359. #endif
  1360. //clean up variant stack
  1361. for (int i = 0; i < f->max_stack; i++) {
  1362. variant_stack[i].~Variant();
  1363. }
  1364. return return_value;
  1365. }
  1366. Variant VisualScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1367. r_error.error = Variant::CallError::CALL_OK; //ok by default
  1368. Map<StringName, Function>::Element *F = functions.find(p_method);
  1369. if (!F) {
  1370. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1371. return Variant();
  1372. }
  1373. VSDEBUG("CALLING: " + String(p_method));
  1374. Function *f = &F->get();
  1375. int total_stack_size = 0;
  1376. total_stack_size += f->max_stack * sizeof(Variant); //variants
  1377. total_stack_size += f->node_count * sizeof(bool);
  1378. total_stack_size += (max_input_args + max_output_args) * sizeof(Variant *); //arguments
  1379. total_stack_size += f->flow_stack_size * sizeof(int); //flow
  1380. total_stack_size += f->pass_stack_size * sizeof(int);
  1381. VSDEBUG("STACK SIZE: " + itos(total_stack_size));
  1382. VSDEBUG("STACK VARIANTS: : " + itos(f->max_stack));
  1383. VSDEBUG("SEQBITS: : " + itos(f->node_count));
  1384. VSDEBUG("MAX INPUT: " + itos(max_input_args));
  1385. VSDEBUG("MAX OUTPUT: " + itos(max_output_args));
  1386. VSDEBUG("FLOW STACK SIZE: " + itos(f->flow_stack_size));
  1387. VSDEBUG("PASS STACK SIZE: " + itos(f->pass_stack_size));
  1388. void *stack = alloca(total_stack_size);
  1389. Variant *variant_stack = (Variant *)stack;
  1390. bool *sequence_bits = (bool *)(variant_stack + f->max_stack);
  1391. const Variant **input_args = (const Variant **)(sequence_bits + f->node_count);
  1392. Variant **output_args = (Variant **)(input_args + max_input_args);
  1393. int flow_max = f->flow_stack_size;
  1394. int *flow_stack = flow_max ? (int *)(output_args + max_output_args) : (int *)NULL;
  1395. int *pass_stack = flow_stack + flow_max;
  1396. for (int i = 0; i < f->node_count; i++) {
  1397. sequence_bits[i] = false; //all starts as false
  1398. }
  1399. zeromem(pass_stack, f->pass_stack_size * sizeof(int));
  1400. Map<int, VisualScriptNodeInstance *>::Element *E = instances.find(f->node);
  1401. if (!E) {
  1402. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1403. ERR_EXPLAIN("No VisualScriptFunction node in function!");
  1404. ERR_FAIL_V(Variant());
  1405. }
  1406. VisualScriptNodeInstance *node = E->get();
  1407. if (flow_stack) {
  1408. flow_stack[0] = node->get_id();
  1409. }
  1410. VSDEBUG("ARGUMENTS: " + itos(f->argument_count) = " RECEIVED: " + itos(p_argcount));
  1411. if (p_argcount < f->argument_count) {
  1412. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1413. r_error.argument = node->get_input_port_count();
  1414. return Variant();
  1415. }
  1416. if (p_argcount > f->argument_count) {
  1417. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  1418. r_error.argument = node->get_input_port_count();
  1419. return Variant();
  1420. }
  1421. //allocate variant stack
  1422. for (int i = 0; i < f->max_stack; i++) {
  1423. memnew_placement(&variant_stack[i], Variant);
  1424. }
  1425. //allocate function arguments (must be copied for yield to work properly)
  1426. for (int i = 0; i < p_argcount; i++) {
  1427. variant_stack[i] = *p_args[i];
  1428. }
  1429. return _call_internal(p_method, stack, total_stack_size, node, 0, 0, false, r_error);
  1430. }
  1431. void VisualScriptInstance::notification(int p_notification) {
  1432. //do nothing as this is called using virtual
  1433. Variant what = p_notification;
  1434. const Variant *whatp = &what;
  1435. Variant::CallError ce;
  1436. call(VisualScriptLanguage::singleton->notification, &whatp, 1, ce); //do as call
  1437. }
  1438. Ref<Script> VisualScriptInstance::get_script() const {
  1439. return script;
  1440. }
  1441. ScriptInstance::RPCMode VisualScriptInstance::get_rpc_mode(const StringName &p_method) const {
  1442. const Map<StringName, VisualScript::Function>::Element *E = script->functions.find(p_method);
  1443. if (!E) {
  1444. return RPC_MODE_DISABLED;
  1445. }
  1446. if (E->get().function_id >= 0 && E->get().nodes.has(E->get().function_id)) {
  1447. Ref<VisualScriptFunction> vsf = E->get().nodes[E->get().function_id].node;
  1448. if (vsf.is_valid()) {
  1449. return vsf->get_rpc_mode();
  1450. }
  1451. }
  1452. return RPC_MODE_DISABLED;
  1453. }
  1454. ScriptInstance::RPCMode VisualScriptInstance::get_rset_mode(const StringName &p_variable) const {
  1455. return RPC_MODE_DISABLED;
  1456. }
  1457. void VisualScriptInstance::create(const Ref<VisualScript> &p_script, Object *p_owner) {
  1458. script = p_script;
  1459. owner = p_owner;
  1460. source = p_script->get_path();
  1461. max_input_args = 0;
  1462. max_output_args = 0;
  1463. if (p_owner->cast_to<Node>()) {
  1464. //turn on these if they exist and base is a node
  1465. Node *node = p_owner->cast_to<Node>();
  1466. if (p_script->functions.has("_process"))
  1467. node->set_process(true);
  1468. if (p_script->functions.has("_fixed_process"))
  1469. node->set_fixed_process(true);
  1470. if (p_script->functions.has("_input"))
  1471. node->set_process_input(true);
  1472. if (p_script->functions.has("_unhandled_input"))
  1473. node->set_process_unhandled_input(true);
  1474. if (p_script->functions.has("_unhandled_key_input"))
  1475. node->set_process_unhandled_key_input(true);
  1476. }
  1477. for (const Map<StringName, VisualScript::Variable>::Element *E = script->variables.front(); E; E = E->next()) {
  1478. variables[E->key()] = E->get().default_value;
  1479. //no hacer que todo exporte, solo las que queres!
  1480. }
  1481. for (const Map<StringName, VisualScript::Function>::Element *E = script->functions.front(); E; E = E->next()) {
  1482. Function function;
  1483. function.node = E->get().function_id;
  1484. function.max_stack = 0;
  1485. function.flow_stack_size = 0;
  1486. function.pass_stack_size = 0;
  1487. function.node_count = 0;
  1488. Map<StringName, int> local_var_indices;
  1489. if (function.node < 0) {
  1490. VisualScriptLanguage::singleton->debug_break_parse(get_script()->get_path(), 0, "No start node in function: " + String(E->key()));
  1491. ERR_CONTINUE(function.node < 0);
  1492. }
  1493. {
  1494. Ref<VisualScriptFunction> func_node = script->get_node(E->key(), E->get().function_id);
  1495. if (func_node.is_null()) {
  1496. VisualScriptLanguage::singleton->debug_break_parse(get_script()->get_path(), 0, "No VisualScriptFunction typed start node in function: " + String(E->key()));
  1497. }
  1498. ERR_CONTINUE(!func_node.is_valid());
  1499. function.argument_count = func_node->get_argument_count();
  1500. function.max_stack += function.argument_count;
  1501. function.flow_stack_size = func_node->is_stack_less() ? 0 : func_node->get_stack_size();
  1502. }
  1503. //multiple passes are required to set up this complex thing..
  1504. //first create the nodes
  1505. for (const Map<int, VisualScript::Function::NodeData>::Element *F = E->get().nodes.front(); F; F = F->next()) {
  1506. Ref<VisualScriptNode> node = F->get().node;
  1507. VisualScriptNodeInstance *instance = node->instance(this); //create instance
  1508. ERR_FAIL_COND(!instance);
  1509. instance->base = node.ptr();
  1510. instance->id = F->key();
  1511. instance->input_port_count = node->get_input_value_port_count();
  1512. instance->input_ports = NULL;
  1513. instance->output_port_count = node->get_output_value_port_count();
  1514. instance->output_ports = NULL;
  1515. instance->sequence_output_count = node->get_output_sequence_port_count();
  1516. instance->sequence_index = function.node_count++;
  1517. instance->sequence_outputs = NULL;
  1518. instance->pass_idx = -1;
  1519. if (instance->input_port_count) {
  1520. instance->input_ports = memnew_arr(int, instance->input_port_count);
  1521. for (int i = 0; i < instance->input_port_count; i++) {
  1522. instance->input_ports[i] = -1; //if not assigned, will become default value
  1523. }
  1524. }
  1525. if (instance->output_port_count) {
  1526. instance->output_ports = memnew_arr(int, instance->output_port_count);
  1527. for (int i = 0; i < instance->output_port_count; i++) {
  1528. instance->output_ports[i] = -1; //if not assigned, will output to trash
  1529. }
  1530. }
  1531. if (instance->sequence_output_count) {
  1532. instance->sequence_outputs = memnew_arr(VisualScriptNodeInstance *, instance->sequence_output_count);
  1533. for (int i = 0; i < instance->sequence_output_count; i++) {
  1534. instance->sequence_outputs[i] = NULL; //if it remains null, flow ends here
  1535. }
  1536. }
  1537. if (node->cast_to<VisualScriptLocalVar>() || node->cast_to<VisualScriptLocalVarSet>()) {
  1538. //working memory is shared only for this node, for the same variables
  1539. Ref<VisualScriptLocalVar> vslv = node;
  1540. StringName var_name;
  1541. if (node->cast_to<VisualScriptLocalVar>())
  1542. var_name = String(node->cast_to<VisualScriptLocalVar>()->get_var_name()).strip_edges();
  1543. else
  1544. var_name = String(node->cast_to<VisualScriptLocalVarSet>()->get_var_name()).strip_edges();
  1545. if (!local_var_indices.has(var_name)) {
  1546. local_var_indices[var_name] = function.max_stack;
  1547. function.max_stack++;
  1548. }
  1549. instance->working_mem_idx = local_var_indices[var_name];
  1550. } else if (instance->get_working_memory_size()) {
  1551. instance->working_mem_idx = function.max_stack;
  1552. function.max_stack += instance->get_working_memory_size();
  1553. } else {
  1554. instance->working_mem_idx = -1; //no working mem
  1555. }
  1556. max_input_args = MAX(max_input_args, instance->input_port_count);
  1557. max_output_args = MAX(max_output_args, instance->output_port_count);
  1558. instances[F->key()] = instance;
  1559. }
  1560. function.trash_pos = function.max_stack++; //create pos for trash
  1561. //second pass, do data connections
  1562. for (const Set<VisualScript::DataConnection>::Element *F = E->get().data_connections.front(); F; F = F->next()) {
  1563. VisualScript::DataConnection dc = F->get();
  1564. ERR_CONTINUE(!instances.has(dc.from_node));
  1565. VisualScriptNodeInstance *from = instances[dc.from_node];
  1566. ERR_CONTINUE(!instances.has(dc.to_node));
  1567. VisualScriptNodeInstance *to = instances[dc.to_node];
  1568. ERR_CONTINUE(dc.from_port >= from->output_port_count);
  1569. ERR_CONTINUE(dc.to_port >= to->input_port_count);
  1570. if (from->output_ports[dc.from_port] == -1) {
  1571. int stack_pos = function.max_stack++;
  1572. from->output_ports[dc.from_port] = stack_pos;
  1573. }
  1574. if (from->get_sequence_output_count() == 0 && to->dependencies.find(from) == -1) {
  1575. //if the node we are reading from has no output sequence, we must call step() before reading from it.
  1576. if (from->pass_idx == -1) {
  1577. from->pass_idx = function.pass_stack_size;
  1578. function.pass_stack_size++;
  1579. }
  1580. to->dependencies.push_back(from);
  1581. }
  1582. to->input_ports[dc.to_port] = from->output_ports[dc.from_port]; //read from wherever the stack is
  1583. }
  1584. //third pass, do sequence connections
  1585. for (const Set<VisualScript::SequenceConnection>::Element *F = E->get().sequence_connections.front(); F; F = F->next()) {
  1586. VisualScript::SequenceConnection sc = F->get();
  1587. ERR_CONTINUE(!instances.has(sc.from_node));
  1588. VisualScriptNodeInstance *from = instances[sc.from_node];
  1589. ERR_CONTINUE(!instances.has(sc.to_node));
  1590. VisualScriptNodeInstance *to = instances[sc.to_node];
  1591. ERR_CONTINUE(sc.from_output >= from->sequence_output_count);
  1592. from->sequence_outputs[sc.from_output] = to;
  1593. }
  1594. //fourth pass:
  1595. // 1) unassigned input ports to default values
  1596. // 2) connect unassigned output ports to trash
  1597. for (const Map<int, VisualScript::Function::NodeData>::Element *F = E->get().nodes.front(); F; F = F->next()) {
  1598. ERR_CONTINUE(!instances.has(F->key()));
  1599. Ref<VisualScriptNode> node = F->get().node;
  1600. VisualScriptNodeInstance *instance = instances[F->key()];
  1601. // conect to default values
  1602. for (int i = 0; i < instance->input_port_count; i++) {
  1603. if (instance->input_ports[i] == -1) {
  1604. //unassigned, connect to default val
  1605. instance->input_ports[i] = default_values.size() | VisualScriptNodeInstance::INPUT_DEFAULT_VALUE_BIT;
  1606. default_values.push_back(node->get_default_input_value(i));
  1607. }
  1608. }
  1609. // conect to trash
  1610. for (int i = 0; i < instance->output_port_count; i++) {
  1611. if (instance->output_ports[i] == -1) {
  1612. instance->output_ports[i] = function.trash_pos; //trash is same for all
  1613. }
  1614. }
  1615. }
  1616. functions[E->key()] = function;
  1617. }
  1618. }
  1619. ScriptLanguage *VisualScriptInstance::get_language() {
  1620. return VisualScriptLanguage::singleton;
  1621. }
  1622. VisualScriptInstance::VisualScriptInstance() {
  1623. }
  1624. VisualScriptInstance::~VisualScriptInstance() {
  1625. if (VisualScriptLanguage::singleton->lock)
  1626. VisualScriptLanguage::singleton->lock->lock();
  1627. script->instances.erase(owner);
  1628. if (VisualScriptLanguage::singleton->lock)
  1629. VisualScriptLanguage::singleton->lock->unlock();
  1630. for (Map<int, VisualScriptNodeInstance *>::Element *E = instances.front(); E; E = E->next()) {
  1631. memdelete(E->get());
  1632. }
  1633. }
  1634. /////////////////////////////////////////////
  1635. /////////////////////
  1636. Variant VisualScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1637. ERR_FAIL_COND_V(function == StringName(), Variant());
  1638. #ifdef DEBUG_ENABLED
  1639. if (instance_id && !ObjectDB::get_instance(instance_id)) {
  1640. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1641. ERR_FAIL_V(Variant());
  1642. }
  1643. if (script_id && !ObjectDB::get_instance(script_id)) {
  1644. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1645. ERR_FAIL_V(Variant());
  1646. }
  1647. #endif
  1648. r_error.error = Variant::CallError::CALL_OK;
  1649. Array args;
  1650. if (p_argcount == 0) {
  1651. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1652. r_error.argument = 1;
  1653. return Variant();
  1654. } else if (p_argcount == 1) {
  1655. //noooneee, reserved for me, me and only me.
  1656. } else {
  1657. for (int i = 0; i < p_argcount - 1; i++) {
  1658. args.push_back(*p_args[i]);
  1659. }
  1660. }
  1661. Ref<VisualScriptFunctionState> self = *p_args[p_argcount - 1]; //hi, I'm myself, needed this to remain alive.
  1662. if (self.is_null()) {
  1663. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1664. r_error.argument = p_argcount - 1;
  1665. r_error.expected = Variant::OBJECT;
  1666. return Variant();
  1667. }
  1668. r_error.error = Variant::CallError::CALL_OK;
  1669. Variant *working_mem = ((Variant *)stack.ptr()) + working_mem_index;
  1670. *working_mem = args; //arguments go to working mem.
  1671. Variant ret = instance->_call_internal(function, stack.ptr(), stack.size(), node, flow_stack_pos, pass, true, r_error);
  1672. function = StringName(); //invalidate
  1673. return ret;
  1674. }
  1675. void VisualScriptFunctionState::connect_to_signal(Object *p_obj, const String &p_signal, Array p_binds) {
  1676. Vector<Variant> binds;
  1677. for (int i = 0; i < p_binds.size(); i++) {
  1678. binds.push_back(p_binds[i]);
  1679. }
  1680. binds.push_back(Ref<VisualScriptFunctionState>(this)); //add myself on the back to avoid dying from unreferencing
  1681. p_obj->connect(p_signal, this, "_signal_callback", binds);
  1682. }
  1683. bool VisualScriptFunctionState::is_valid() const {
  1684. return function != StringName();
  1685. }
  1686. Variant VisualScriptFunctionState::resume(Array p_args) {
  1687. ERR_FAIL_COND_V(function == StringName(), Variant());
  1688. #ifdef DEBUG_ENABLED
  1689. if (instance_id && !ObjectDB::get_instance(instance_id)) {
  1690. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1691. ERR_FAIL_V(Variant());
  1692. }
  1693. if (script_id && !ObjectDB::get_instance(script_id)) {
  1694. ERR_EXPLAIN("Resumed after yield, but script is gone");
  1695. ERR_FAIL_V(Variant());
  1696. }
  1697. #endif
  1698. Variant::CallError r_error;
  1699. r_error.error = Variant::CallError::CALL_OK;
  1700. Variant *working_mem = ((Variant *)stack.ptr()) + working_mem_index;
  1701. *working_mem = p_args; //arguments go to working mem.
  1702. Variant ret = instance->_call_internal(function, stack.ptr(), stack.size(), node, flow_stack_pos, pass, true, r_error);
  1703. function = StringName(); //invalidate
  1704. return ret;
  1705. }
  1706. void VisualScriptFunctionState::_bind_methods() {
  1707. ClassDB::bind_method(D_METHOD("connect_to_signal", "obj", "signals", "args"), &VisualScriptFunctionState::connect_to_signal);
  1708. ClassDB::bind_method(D_METHOD("resume", "args"), &VisualScriptFunctionState::resume, DEFVAL(Variant()));
  1709. ClassDB::bind_method(D_METHOD("is_valid"), &VisualScriptFunctionState::is_valid);
  1710. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &VisualScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1711. }
  1712. VisualScriptFunctionState::VisualScriptFunctionState() {
  1713. }
  1714. VisualScriptFunctionState::~VisualScriptFunctionState() {
  1715. if (function != StringName()) {
  1716. Variant *s = ((Variant *)stack.ptr());
  1717. for (int i = 0; i < variant_stack_size; i++) {
  1718. s[i].~Variant();
  1719. }
  1720. }
  1721. }
  1722. ///////////////////////////////////////////////
  1723. String VisualScriptLanguage::get_name() const {
  1724. return "VisualScript";
  1725. }
  1726. /* LANGUAGE FUNCTIONS */
  1727. void VisualScriptLanguage::init() {
  1728. }
  1729. String VisualScriptLanguage::get_type() const {
  1730. return "VisualScript";
  1731. }
  1732. String VisualScriptLanguage::get_extension() const {
  1733. return "vs";
  1734. }
  1735. Error VisualScriptLanguage::execute_file(const String &p_path) {
  1736. return OK;
  1737. }
  1738. void VisualScriptLanguage::finish() {
  1739. }
  1740. /* EDITOR FUNCTIONS */
  1741. void VisualScriptLanguage::get_reserved_words(List<String> *p_words) const {
  1742. }
  1743. void VisualScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  1744. }
  1745. void VisualScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  1746. }
  1747. Ref<Script> VisualScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  1748. Ref<VisualScript> script;
  1749. script.instance();
  1750. script->set_instance_base_type(p_base_class_name);
  1751. return script;
  1752. }
  1753. bool VisualScriptLanguage::is_using_templates() {
  1754. return true;
  1755. }
  1756. void VisualScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  1757. Ref<VisualScript> script = p_script;
  1758. script->set_instance_base_type(p_base_class_name);
  1759. }
  1760. bool VisualScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions) const {
  1761. return false;
  1762. }
  1763. Script *VisualScriptLanguage::create_script() const {
  1764. return memnew(VisualScript);
  1765. }
  1766. bool VisualScriptLanguage::has_named_classes() const {
  1767. return false;
  1768. }
  1769. int VisualScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  1770. return -1;
  1771. }
  1772. String VisualScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  1773. return String();
  1774. }
  1775. void VisualScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  1776. }
  1777. void VisualScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  1778. }
  1779. /* DEBUGGER FUNCTIONS */
  1780. bool VisualScriptLanguage::debug_break_parse(const String &p_file, int p_node, const String &p_error) {
  1781. //break because of parse error
  1782. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  1783. _debug_parse_err_node = p_node;
  1784. _debug_parse_err_file = p_file;
  1785. _debug_error = p_error;
  1786. ScriptDebugger::get_singleton()->debug(this, false);
  1787. return true;
  1788. } else {
  1789. return false;
  1790. }
  1791. }
  1792. bool VisualScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  1793. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  1794. _debug_parse_err_node = -1;
  1795. _debug_parse_err_file = "";
  1796. _debug_error = p_error;
  1797. ScriptDebugger::get_singleton()->debug(this, p_allow_continue);
  1798. return true;
  1799. } else {
  1800. return false;
  1801. }
  1802. }
  1803. String VisualScriptLanguage::debug_get_error() const {
  1804. return _debug_error;
  1805. }
  1806. int VisualScriptLanguage::debug_get_stack_level_count() const {
  1807. if (_debug_parse_err_node >= 0)
  1808. return 1;
  1809. return _debug_call_stack_pos;
  1810. }
  1811. int VisualScriptLanguage::debug_get_stack_level_line(int p_level) const {
  1812. if (_debug_parse_err_node >= 0)
  1813. return _debug_parse_err_node;
  1814. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  1815. int l = _debug_call_stack_pos - p_level - 1;
  1816. return *(_call_stack[l].current_id);
  1817. }
  1818. String VisualScriptLanguage::debug_get_stack_level_function(int p_level) const {
  1819. if (_debug_parse_err_node >= 0)
  1820. return "";
  1821. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  1822. int l = _debug_call_stack_pos - p_level - 1;
  1823. return *_call_stack[l].function;
  1824. }
  1825. String VisualScriptLanguage::debug_get_stack_level_source(int p_level) const {
  1826. if (_debug_parse_err_node >= 0)
  1827. return _debug_parse_err_file;
  1828. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  1829. int l = _debug_call_stack_pos - p_level - 1;
  1830. return _call_stack[l].instance->get_script_ptr()->get_path();
  1831. }
  1832. void VisualScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  1833. if (_debug_parse_err_node >= 0)
  1834. return;
  1835. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  1836. int l = _debug_call_stack_pos - p_level - 1;
  1837. const StringName *f = _call_stack[l].function;
  1838. ERR_FAIL_COND(!_call_stack[l].instance->functions.has(*f));
  1839. //VisualScriptInstance::Function *func = &_call_stack[l].instance->functions[*f];
  1840. VisualScriptNodeInstance *node = _call_stack[l].instance->instances[*_call_stack[l].current_id];
  1841. ERR_FAIL_COND(!node);
  1842. p_locals->push_back("node_name");
  1843. p_values->push_back(node->get_base_node()->get_text());
  1844. for (int i = 0; i < node->input_port_count; i++) {
  1845. String name = node->get_base_node()->get_input_value_port_info(i).name;
  1846. if (name == String()) {
  1847. name = "in_" + itos(i);
  1848. }
  1849. p_locals->push_back("input/" + name);
  1850. //value is trickier
  1851. int in_from = node->input_ports[i];
  1852. int in_value = in_from & VisualScriptNodeInstance::INPUT_MASK;
  1853. if (in_from & VisualScriptNodeInstance::INPUT_DEFAULT_VALUE_BIT) {
  1854. p_values->push_back(_call_stack[l].instance->default_values[in_value]);
  1855. } else {
  1856. p_values->push_back(_call_stack[l].stack[in_value]);
  1857. }
  1858. }
  1859. for (int i = 0; i < node->output_port_count; i++) {
  1860. String name = node->get_base_node()->get_output_value_port_info(i).name;
  1861. if (name == String()) {
  1862. name = "out_" + itos(i);
  1863. }
  1864. p_locals->push_back("output/" + name);
  1865. //value is trickier
  1866. int in_from = node->output_ports[i];
  1867. p_values->push_back(_call_stack[l].stack[in_from]);
  1868. }
  1869. for (int i = 0; i < node->get_working_memory_size(); i++) {
  1870. p_locals->push_back("working_mem/mem_" + itos(i));
  1871. p_values->push_back((*_call_stack[l].work_mem)[i]);
  1872. }
  1873. /*
  1874. ERR_FAIL_INDEX(p_level,_debug_call_stack_pos);
  1875. VisualFunction *f = _call_stack[l].function;
  1876. List<Pair<StringName,int> > locals;
  1877. f->debug_get_stack_member_state(*_call_stack[l].line,&locals);
  1878. for( List<Pair<StringName,int> >::Element *E = locals.front();E;E=E->next() ) {
  1879. p_locals->push_back(E->get().first);
  1880. p_values->push_back(_call_stack[l].stack[E->get().second]);
  1881. }
  1882. */
  1883. }
  1884. void VisualScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  1885. if (_debug_parse_err_node >= 0)
  1886. return;
  1887. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  1888. int l = _debug_call_stack_pos - p_level - 1;
  1889. Ref<VisualScript> vs = _call_stack[l].instance->get_script();
  1890. if (vs.is_null())
  1891. return;
  1892. List<StringName> vars;
  1893. vs->get_variable_list(&vars);
  1894. for (List<StringName>::Element *E = vars.front(); E; E = E->next()) {
  1895. Variant v;
  1896. if (_call_stack[l].instance->get_variable(E->get(), &v)) {
  1897. p_members->push_back("variables/" + E->get());
  1898. p_values->push_back(v);
  1899. }
  1900. }
  1901. }
  1902. void VisualScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  1903. //no globals are really reachable in gdscript
  1904. }
  1905. String VisualScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  1906. if (_debug_parse_err_node >= 0)
  1907. return "";
  1908. return "";
  1909. }
  1910. void VisualScriptLanguage::reload_all_scripts() {
  1911. }
  1912. void VisualScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  1913. }
  1914. /* LOADER FUNCTIONS */
  1915. void VisualScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  1916. p_extensions->push_back("vs");
  1917. }
  1918. void VisualScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  1919. }
  1920. void VisualScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  1921. }
  1922. void VisualScriptLanguage::profiling_start() {
  1923. }
  1924. void VisualScriptLanguage::profiling_stop() {
  1925. }
  1926. int VisualScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  1927. return 0;
  1928. }
  1929. int VisualScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  1930. return 0;
  1931. }
  1932. VisualScriptLanguage *VisualScriptLanguage::singleton = NULL;
  1933. void VisualScriptLanguage::add_register_func(const String &p_name, VisualScriptNodeRegisterFunc p_func) {
  1934. ERR_FAIL_COND(register_funcs.has(p_name));
  1935. register_funcs[p_name] = p_func;
  1936. }
  1937. Ref<VisualScriptNode> VisualScriptLanguage::create_node_from_name(const String &p_name) {
  1938. ERR_FAIL_COND_V(!register_funcs.has(p_name), Ref<VisualScriptNode>());
  1939. return register_funcs[p_name](p_name);
  1940. }
  1941. void VisualScriptLanguage::get_registered_node_names(List<String> *r_names) {
  1942. for (Map<String, VisualScriptNodeRegisterFunc>::Element *E = register_funcs.front(); E; E = E->next()) {
  1943. r_names->push_back(E->key());
  1944. }
  1945. }
  1946. VisualScriptLanguage::VisualScriptLanguage() {
  1947. notification = "_notification";
  1948. _step = "_step";
  1949. _subcall = "_subcall";
  1950. singleton = this;
  1951. #ifndef NO_THREADS
  1952. lock = Mutex::create();
  1953. #endif
  1954. _debug_parse_err_node = -1;
  1955. _debug_parse_err_file = "";
  1956. _debug_call_stack_pos = 0;
  1957. int dmcs = GLOBAL_DEF("debug/settings/visual_script/max_call_stack", 1024);
  1958. if (ScriptDebugger::get_singleton()) {
  1959. //debugging enabled!
  1960. _debug_max_call_stack = dmcs;
  1961. if (_debug_max_call_stack < 1024)
  1962. _debug_max_call_stack = 1024;
  1963. _call_stack = memnew_arr(CallLevel, _debug_max_call_stack + 1);
  1964. } else {
  1965. _debug_max_call_stack = 0;
  1966. _call_stack = NULL;
  1967. }
  1968. }
  1969. VisualScriptLanguage::~VisualScriptLanguage() {
  1970. if (lock)
  1971. memdelete(lock);
  1972. if (_call_stack) {
  1973. memdelete_arr(_call_stack);
  1974. }
  1975. singleton = NULL;
  1976. }