visual_script.cpp 85 KB

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