visual_script.cpp 87 KB

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