visual_script.cpp 78 KB

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