visual_script.cpp 83 KB

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