visual_script.cpp 84 KB

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