visual_script.cpp 85 KB

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