visual_script.cpp 82 KB

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