visual_script.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  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. is_tool_script = false;
  994. }
  995. StringName VisualScript::get_default_func() const {
  996. return StringName("f_312843592");
  997. }
  998. Set<int> VisualScript::get_output_sequence_ports_connected(const String &edited_func, int from_node) {
  999. List<VisualScript::SequenceConnection> *sc = memnew(List<VisualScript::SequenceConnection>);
  1000. get_sequence_connection_list(edited_func, sc);
  1001. Set<int> connected;
  1002. for (List<VisualScript::SequenceConnection>::Element *E = sc->front(); E; E = E->next()) {
  1003. if (E->get().from_node == from_node) {
  1004. connected.insert(E->get().from_output);
  1005. }
  1006. }
  1007. memdelete(sc);
  1008. return connected;
  1009. }
  1010. VisualScript::~VisualScript() {
  1011. while (!functions.empty()) {
  1012. remove_function(functions.front()->key());
  1013. }
  1014. }
  1015. ////////////////////////////////////////////
  1016. bool VisualScriptInstance::set(const StringName &p_name, const Variant &p_value) {
  1017. Map<StringName, Variant>::Element *E = variables.find(p_name);
  1018. if (!E)
  1019. return false;
  1020. E->get() = p_value;
  1021. return true;
  1022. }
  1023. bool VisualScriptInstance::get(const StringName &p_name, Variant &r_ret) const {
  1024. const Map<StringName, Variant>::Element *E = variables.find(p_name);
  1025. if (!E)
  1026. return false;
  1027. r_ret = E->get();
  1028. return true;
  1029. }
  1030. void VisualScriptInstance::get_property_list(List<PropertyInfo> *p_properties) const {
  1031. for (const Map<StringName, VisualScript::Variable>::Element *E = script->variables.front(); E; E = E->next()) {
  1032. if (!E->get()._export)
  1033. continue;
  1034. PropertyInfo p = E->get().info;
  1035. p.name = String(E->key());
  1036. p.usage |= PROPERTY_USAGE_SCRIPT_VARIABLE;
  1037. p_properties->push_back(p);
  1038. }
  1039. }
  1040. Variant::Type VisualScriptInstance::get_property_type(const StringName &p_name, bool *r_is_valid) const {
  1041. const Map<StringName, VisualScript::Variable>::Element *E = script->variables.find(p_name);
  1042. if (!E) {
  1043. if (r_is_valid)
  1044. *r_is_valid = false;
  1045. ERR_FAIL_V(Variant::NIL);
  1046. }
  1047. if (r_is_valid)
  1048. *r_is_valid = true;
  1049. return E->get().info.type;
  1050. }
  1051. void VisualScriptInstance::get_method_list(List<MethodInfo> *p_list) const {
  1052. for (const Map<StringName, VisualScript::Function>::Element *E = script->functions.front(); E; E = E->next()) {
  1053. if (E->key() == script->get_default_func()) {
  1054. continue;
  1055. }
  1056. MethodInfo mi;
  1057. mi.name = E->key();
  1058. if (E->get().function_id >= 0 && E->get().nodes.has(E->get().function_id)) {
  1059. Ref<VisualScriptFunction> vsf = E->get().nodes[E->get().function_id].node;
  1060. if (vsf.is_valid()) {
  1061. for (int i = 0; i < vsf->get_argument_count(); i++) {
  1062. PropertyInfo arg;
  1063. arg.name = vsf->get_argument_name(i);
  1064. arg.type = vsf->get_argument_type(i);
  1065. mi.arguments.push_back(arg);
  1066. }
  1067. if (!vsf->is_sequenced()) { //assumed constant if not sequenced
  1068. mi.flags |= METHOD_FLAG_CONST;
  1069. }
  1070. }
  1071. }
  1072. p_list->push_back(mi);
  1073. }
  1074. }
  1075. bool VisualScriptInstance::has_method(const StringName &p_method) const {
  1076. if (p_method == script->get_default_func())
  1077. return false;
  1078. return script->functions.has(p_method);
  1079. }
  1080. //#define VSDEBUG(m_text) print_line(m_text)
  1081. #define VSDEBUG(m_text)
  1082. 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) {
  1083. ERR_FAIL_COND(node->pass_idx == -1);
  1084. if (pass_stack[node->pass_idx] == p_pass)
  1085. return;
  1086. pass_stack[node->pass_idx] = p_pass;
  1087. if (!node->dependencies.empty()) {
  1088. int dc = node->dependencies.size();
  1089. VisualScriptNodeInstance **deps = node->dependencies.ptrw();
  1090. for (int i = 0; i < dc; i++) {
  1091. _dependency_step(deps[i], p_pass, pass_stack, input_args, output_args, variant_stack, r_error, error_str, r_error_node);
  1092. if (r_error.error != Variant::CallError::CALL_OK)
  1093. return;
  1094. }
  1095. }
  1096. for (int i = 0; i < node->input_port_count; i++) {
  1097. int index = node->input_ports[i] & VisualScriptNodeInstance::INPUT_MASK;
  1098. if (node->input_ports[i] & VisualScriptNodeInstance::INPUT_DEFAULT_VALUE_BIT) {
  1099. //is a default value (unassigned input port)
  1100. input_args[i] = &default_values[index];
  1101. } else {
  1102. //regular temporary in stack
  1103. input_args[i] = &variant_stack[index];
  1104. }
  1105. }
  1106. for (int i = 0; i < node->output_port_count; i++) {
  1107. output_args[i] = &variant_stack[node->output_ports[i]];
  1108. }
  1109. Variant *working_mem = node->working_mem_idx >= 0 ? &variant_stack[node->working_mem_idx] : (Variant *)NULL;
  1110. node->step(input_args, output_args, VisualScriptNodeInstance::START_MODE_BEGIN_SEQUENCE, working_mem, r_error, error_str);
  1111. //ignore return
  1112. if (r_error.error != Variant::CallError::CALL_OK) {
  1113. *r_error_node = node;
  1114. }
  1115. }
  1116. 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) {
  1117. Map<StringName, Function>::Element *F = functions.find(p_method);
  1118. ERR_FAIL_COND_V(!F, Variant());
  1119. Function *f = &F->get();
  1120. //this call goes separate, so it can e yielded and suspended
  1121. Variant *variant_stack = (Variant *)p_stack;
  1122. bool *sequence_bits = (bool *)(variant_stack + f->max_stack);
  1123. const Variant **input_args = (const Variant **)(sequence_bits + f->node_count);
  1124. Variant **output_args = (Variant **)(input_args + max_input_args);
  1125. int flow_max = f->flow_stack_size;
  1126. int *flow_stack = flow_max ? (int *)(output_args + max_output_args) : (int *)NULL;
  1127. int *pass_stack = flow_stack ? (int *)(flow_stack + flow_max) : (int *)NULL;
  1128. String error_str;
  1129. VisualScriptNodeInstance *node = p_node;
  1130. bool error = false;
  1131. int current_node_id = f->node;
  1132. Variant return_value;
  1133. Variant *working_mem = NULL;
  1134. int flow_stack_pos = p_flow_stack_pos;
  1135. #ifdef DEBUG_ENABLED
  1136. if (ScriptDebugger::get_singleton()) {
  1137. VisualScriptLanguage::singleton->enter_function(this, &p_method, variant_stack, &working_mem, &current_node_id);
  1138. }
  1139. #endif
  1140. while (true) {
  1141. p_pass++; //increment pass
  1142. current_node_id = node->get_id();
  1143. VSDEBUG("==========AT NODE: " + itos(current_node_id) + " base: " + node->get_base_node()->get_class_name());
  1144. VSDEBUG("AT STACK POS: " + itos(flow_stack_pos));
  1145. //setup working mem
  1146. working_mem = node->working_mem_idx >= 0 ? &variant_stack[node->working_mem_idx] : (Variant *)NULL;
  1147. VSDEBUG("WORKING MEM: " + itos(node->working_mem_idx));
  1148. if (current_node_id == f->node) {
  1149. //if function node, set up function arguments from beginning of stack
  1150. for (int i = 0; i < f->argument_count; i++) {
  1151. input_args[i] = &variant_stack[i];
  1152. }
  1153. } else {
  1154. //run dependencies first
  1155. if (!node->dependencies.empty()) {
  1156. int dc = node->dependencies.size();
  1157. VisualScriptNodeInstance **deps = node->dependencies.ptrw();
  1158. for (int i = 0; i < dc; i++) {
  1159. _dependency_step(deps[i], p_pass, pass_stack, input_args, output_args, variant_stack, r_error, error_str, &node);
  1160. if (r_error.error != Variant::CallError::CALL_OK) {
  1161. error = true;
  1162. current_node_id = node->id;
  1163. break;
  1164. }
  1165. }
  1166. }
  1167. if (!error) {
  1168. //setup input pointers normally
  1169. VSDEBUG("INPUT PORTS: " + itos(node->input_port_count));
  1170. for (int i = 0; i < node->input_port_count; i++) {
  1171. int index = node->input_ports[i] & VisualScriptNodeInstance::INPUT_MASK;
  1172. if (node->input_ports[i] & VisualScriptNodeInstance::INPUT_DEFAULT_VALUE_BIT) {
  1173. //is a default value (unassigned input port)
  1174. input_args[i] = &default_values[index];
  1175. VSDEBUG("\tPORT " + itos(i) + " DEFAULT VAL");
  1176. } else {
  1177. //regular temporary in stack
  1178. input_args[i] = &variant_stack[index];
  1179. VSDEBUG("PORT " + itos(i) + " AT STACK " + itos(index));
  1180. }
  1181. }
  1182. }
  1183. }
  1184. if (error)
  1185. break;
  1186. //setup output pointers
  1187. VSDEBUG("OUTPUT PORTS: " + itos(node->output_port_count));
  1188. for (int i = 0; i < node->output_port_count; i++) {
  1189. output_args[i] = &variant_stack[node->output_ports[i]];
  1190. VSDEBUG("PORT " + itos(i) + " AT STACK " + itos(node->output_ports[i]));
  1191. }
  1192. //do step
  1193. VisualScriptNodeInstance::StartMode start_mode;
  1194. {
  1195. if (p_resuming_yield) {
  1196. start_mode = VisualScriptNodeInstance::START_MODE_RESUME_YIELD;
  1197. p_resuming_yield = false; // should resume only the first time
  1198. } else if (flow_stack && (flow_stack[flow_stack_pos] & VisualScriptNodeInstance::FLOW_STACK_PUSHED_BIT)) {
  1199. //if there is a push bit, it means we are continuing a sequence
  1200. start_mode = VisualScriptNodeInstance::START_MODE_CONTINUE_SEQUENCE;
  1201. } else {
  1202. start_mode = VisualScriptNodeInstance::START_MODE_BEGIN_SEQUENCE;
  1203. }
  1204. }
  1205. VSDEBUG("STEP - STARTSEQ: " + itos(start_mode));
  1206. int ret = node->step(input_args, output_args, start_mode, working_mem, r_error, error_str);
  1207. if (r_error.error != Variant::CallError::CALL_OK) {
  1208. //use error from step
  1209. error = true;
  1210. break;
  1211. }
  1212. if (ret & VisualScriptNodeInstance::STEP_YIELD_BIT) {
  1213. //yielded!
  1214. if (node->get_working_memory_size() == 0) {
  1215. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1216. error_str = RTR("A node yielded without working memory, please read the docs on how to yield properly!");
  1217. error = true;
  1218. break;
  1219. } else {
  1220. Ref<VisualScriptFunctionState> state = *working_mem;
  1221. if (!state.is_valid()) {
  1222. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1223. error_str = RTR("Node yielded, but did not return a function state in the first working memory.");
  1224. error = true;
  1225. break;
  1226. }
  1227. //step 1, capture all state
  1228. state->instance_id = get_owner_ptr()->get_instance_id();
  1229. state->script_id = get_script()->get_instance_id();
  1230. state->instance = this;
  1231. state->function = p_method;
  1232. state->working_mem_index = node->working_mem_idx;
  1233. state->variant_stack_size = f->max_stack;
  1234. state->node = node;
  1235. state->flow_stack_pos = flow_stack_pos;
  1236. state->stack.resize(p_stack_size);
  1237. state->pass = p_pass;
  1238. copymem(state->stack.ptrw(), p_stack, p_stack_size);
  1239. //step 2, run away, return directly
  1240. r_error.error = Variant::CallError::CALL_OK;
  1241. #ifdef DEBUG_ENABLED
  1242. //will re-enter later, so exiting
  1243. if (ScriptDebugger::get_singleton()) {
  1244. VisualScriptLanguage::singleton->exit_function();
  1245. }
  1246. #endif
  1247. return state;
  1248. }
  1249. }
  1250. #ifdef DEBUG_ENABLED
  1251. if (ScriptDebugger::get_singleton()) {
  1252. // line
  1253. bool do_break = false;
  1254. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  1255. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  1256. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  1257. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  1258. do_break = true;
  1259. }
  1260. if (ScriptDebugger::get_singleton()->is_breakpoint(current_node_id, source))
  1261. do_break = true;
  1262. if (do_break) {
  1263. VisualScriptLanguage::singleton->debug_break("Breakpoint", true);
  1264. }
  1265. ScriptDebugger::get_singleton()->line_poll();
  1266. }
  1267. #endif
  1268. int output = ret & VisualScriptNodeInstance::STEP_MASK;
  1269. VSDEBUG("STEP RETURN: " + itos(ret));
  1270. if (ret & VisualScriptNodeInstance::STEP_EXIT_FUNCTION_BIT) {
  1271. if (node->get_working_memory_size() == 0) {
  1272. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1273. error_str = RTR("Return value must be assigned to first element of node working memory! Fix your node please.");
  1274. error = true;
  1275. } else {
  1276. //assign from working memory, first element
  1277. return_value = *working_mem;
  1278. }
  1279. VSDEBUG("EXITING FUNCTION - VALUE " + String(return_value));
  1280. break; //exit function requested, bye
  1281. }
  1282. VisualScriptNodeInstance *next = NULL; //next node
  1283. if ((ret == output || ret & VisualScriptNodeInstance::STEP_FLAG_PUSH_STACK_BIT) && node->sequence_output_count) {
  1284. //if no exit bit was set, and has sequence outputs, guess next node
  1285. if (output >= node->sequence_output_count) {
  1286. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1287. error_str = RTR("Node returned an invalid sequence output: ") + itos(output);
  1288. error = true;
  1289. break;
  1290. }
  1291. next = node->sequence_outputs[output];
  1292. if (next) {
  1293. VSDEBUG("GOT NEXT NODE - " + itos(next->get_id()));
  1294. } else {
  1295. VSDEBUG("GOT NEXT NODE - NULL");
  1296. }
  1297. }
  1298. if (flow_stack) {
  1299. //update flow stack pos (may have changed)
  1300. flow_stack[flow_stack_pos] = current_node_id;
  1301. //add stack push bit if requested
  1302. if (ret & VisualScriptNodeInstance::STEP_FLAG_PUSH_STACK_BIT) {
  1303. flow_stack[flow_stack_pos] |= VisualScriptNodeInstance::FLOW_STACK_PUSHED_BIT;
  1304. sequence_bits[node->sequence_index] = true; //remember sequence bit
  1305. VSDEBUG("NEXT SEQ - FLAG BIT");
  1306. } else {
  1307. sequence_bits[node->sequence_index] = false; //forget sequence bit
  1308. VSDEBUG("NEXT SEQ - NORMAL");
  1309. }
  1310. if (ret & VisualScriptNodeInstance::STEP_FLAG_GO_BACK_BIT) {
  1311. //go back request
  1312. if (flow_stack_pos > 0) {
  1313. flow_stack_pos--;
  1314. node = instances[flow_stack[flow_stack_pos] & VisualScriptNodeInstance::FLOW_STACK_MASK];
  1315. VSDEBUG("NEXT IS GO BACK");
  1316. } else {
  1317. VSDEBUG("NEXT IS GO BACK, BUT NO NEXT SO EXIT");
  1318. break; //simply exit without value or error
  1319. }
  1320. } else if (next) {
  1321. if (sequence_bits[next->sequence_index]) {
  1322. // what happened here is that we are entering a node that is in the middle of doing a sequence (pushed stack) from the front
  1323. // because each node has a working memory, we can't really do a sub-sequence
  1324. // as a result, the sequence will be restarted and the stack will roll back to find where this node
  1325. // started the sequence
  1326. bool found = false;
  1327. for (int i = flow_stack_pos; i >= 0; i--) {
  1328. if ((flow_stack[i] & VisualScriptNodeInstance::FLOW_STACK_MASK) == next->get_id()) {
  1329. flow_stack_pos = i; //roll back and remove bit
  1330. flow_stack[i] = next->get_id();
  1331. sequence_bits[next->sequence_index] = false;
  1332. found = true;
  1333. }
  1334. }
  1335. if (!found) {
  1336. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1337. error_str = RTR("Found sequence bit but not the node in the stack, report bug!");
  1338. error = true;
  1339. break;
  1340. }
  1341. node = next;
  1342. VSDEBUG("RE-ENTERED A LOOP, RETURNED STACK POS TO - " + itos(flow_stack_pos));
  1343. } else {
  1344. // check for stack overflow
  1345. if (flow_stack_pos + 1 >= flow_max) {
  1346. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1347. error_str = RTR("Stack overflow with stack depth: ") + itos(output);
  1348. error = true;
  1349. break;
  1350. }
  1351. node = next;
  1352. flow_stack_pos++;
  1353. flow_stack[flow_stack_pos] = node->get_id();
  1354. VSDEBUG("INCREASE FLOW STACK");
  1355. }
  1356. } else {
  1357. //no next node, try to go back in stack to pushed bit
  1358. bool found = false;
  1359. for (int i = flow_stack_pos; i >= 0; i--) {
  1360. VSDEBUG("FS " + itos(i) + " - " + itos(flow_stack[i]));
  1361. if (flow_stack[i] & VisualScriptNodeInstance::FLOW_STACK_PUSHED_BIT) {
  1362. node = instances[flow_stack[i] & VisualScriptNodeInstance::FLOW_STACK_MASK];
  1363. flow_stack_pos = i;
  1364. found = true;
  1365. break;
  1366. }
  1367. }
  1368. if (!found) {
  1369. VSDEBUG("NO NEXT NODE, NO GO BACK, EXITING");
  1370. break; //done, couldn't find a push stack bit
  1371. }
  1372. VSDEBUG("NO NEXT NODE, GO BACK TO: " + itos(flow_stack_pos));
  1373. }
  1374. } else {
  1375. node = next; //stackless mode, simply assign next node
  1376. }
  1377. }
  1378. if (error) {
  1379. //error
  1380. // function, file, line, error, explanation
  1381. String err_file = script->get_path();
  1382. String err_func = p_method;
  1383. int err_line = current_node_id; //not a line but it works as one
  1384. if (node && (r_error.error != Variant::CallError::CALL_ERROR_INVALID_METHOD || error_str == String())) {
  1385. if (error_str != String()) {
  1386. error_str += " ";
  1387. }
  1388. if (r_error.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1389. int errorarg = r_error.argument;
  1390. error_str += "Cannot convert argument " + itos(errorarg + 1) + " to " + Variant::get_type_name(r_error.expected) + ".";
  1391. } else if (r_error.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  1392. error_str += "Expected " + itos(r_error.argument) + " arguments.";
  1393. } else if (r_error.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  1394. error_str += "Expected " + itos(r_error.argument) + " arguments.";
  1395. } else if (r_error.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  1396. error_str += "Invalid Call.";
  1397. } else if (r_error.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  1398. error_str += "Base Instance is null";
  1399. }
  1400. }
  1401. //if (!GDScriptLanguage::get_singleton()->debug_break(err_text,false)) {
  1402. // debugger break did not happen
  1403. if (!VisualScriptLanguage::singleton->debug_break(error_str, false)) {
  1404. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, error_str.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1405. }
  1406. //}
  1407. } else {
  1408. //return_value=
  1409. }
  1410. #ifdef DEBUG_ENABLED
  1411. if (ScriptDebugger::get_singleton()) {
  1412. VisualScriptLanguage::singleton->exit_function();
  1413. }
  1414. #endif
  1415. //clean up variant stack
  1416. for (int i = 0; i < f->max_stack; i++) {
  1417. variant_stack[i].~Variant();
  1418. }
  1419. return return_value;
  1420. }
  1421. Variant VisualScriptInstance::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1422. r_error.error = Variant::CallError::CALL_OK; //ok by default
  1423. Map<StringName, Function>::Element *F = functions.find(p_method);
  1424. if (!F) {
  1425. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1426. return Variant();
  1427. }
  1428. VSDEBUG("CALLING: " + String(p_method));
  1429. Function *f = &F->get();
  1430. int total_stack_size = 0;
  1431. total_stack_size += f->max_stack * sizeof(Variant); //variants
  1432. total_stack_size += f->node_count * sizeof(bool);
  1433. total_stack_size += (max_input_args + max_output_args) * sizeof(Variant *); //arguments
  1434. total_stack_size += f->flow_stack_size * sizeof(int); //flow
  1435. total_stack_size += f->pass_stack_size * sizeof(int);
  1436. VSDEBUG("STACK SIZE: " + itos(total_stack_size));
  1437. VSDEBUG("STACK VARIANTS: : " + itos(f->max_stack));
  1438. VSDEBUG("SEQBITS: : " + itos(f->node_count));
  1439. VSDEBUG("MAX INPUT: " + itos(max_input_args));
  1440. VSDEBUG("MAX OUTPUT: " + itos(max_output_args));
  1441. VSDEBUG("FLOW STACK SIZE: " + itos(f->flow_stack_size));
  1442. VSDEBUG("PASS STACK SIZE: " + itos(f->pass_stack_size));
  1443. void *stack = alloca(total_stack_size);
  1444. Variant *variant_stack = (Variant *)stack;
  1445. bool *sequence_bits = (bool *)(variant_stack + f->max_stack);
  1446. const Variant **input_args = (const Variant **)(sequence_bits + f->node_count);
  1447. Variant **output_args = (Variant **)(input_args + max_input_args);
  1448. int flow_max = f->flow_stack_size;
  1449. int *flow_stack = flow_max ? (int *)(output_args + max_output_args) : (int *)NULL;
  1450. int *pass_stack = flow_stack ? (int *)(flow_stack + flow_max) : (int *)NULL;
  1451. for (int i = 0; i < f->node_count; i++) {
  1452. sequence_bits[i] = false; //all starts as false
  1453. }
  1454. zeromem(pass_stack, f->pass_stack_size * sizeof(int));
  1455. Map<int, VisualScriptNodeInstance *>::Element *E = instances.find(f->node);
  1456. if (!E) {
  1457. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  1458. ERR_FAIL_V_MSG(Variant(), "No VisualScriptFunction node in function.");
  1459. }
  1460. VisualScriptNodeInstance *node = E->get();
  1461. if (flow_stack) {
  1462. flow_stack[0] = node->get_id();
  1463. }
  1464. VSDEBUG("ARGUMENTS: " + itos(f->argument_count) = " RECEIVED: " + itos(p_argcount));
  1465. if (p_argcount < f->argument_count) {
  1466. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1467. r_error.argument = node->get_input_port_count();
  1468. return Variant();
  1469. }
  1470. if (p_argcount > f->argument_count) {
  1471. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  1472. r_error.argument = node->get_input_port_count();
  1473. return Variant();
  1474. }
  1475. //allocate variant stack
  1476. for (int i = 0; i < f->max_stack; i++) {
  1477. memnew_placement(&variant_stack[i], Variant);
  1478. }
  1479. //allocate function arguments (must be copied for yield to work properly)
  1480. for (int i = 0; i < p_argcount; i++) {
  1481. variant_stack[i] = *p_args[i];
  1482. }
  1483. return _call_internal(p_method, stack, total_stack_size, node, 0, 0, false, r_error);
  1484. }
  1485. void VisualScriptInstance::notification(int p_notification) {
  1486. //do nothing as this is called using virtual
  1487. Variant what = p_notification;
  1488. const Variant *whatp = &what;
  1489. Variant::CallError ce;
  1490. call(VisualScriptLanguage::singleton->notification, &whatp, 1, ce); //do as call
  1491. }
  1492. String VisualScriptInstance::to_string(bool *r_valid) {
  1493. if (has_method(CoreStringNames::get_singleton()->_to_string)) {
  1494. Variant::CallError ce;
  1495. Variant ret = call(CoreStringNames::get_singleton()->_to_string, NULL, 0, ce);
  1496. if (ce.error == Variant::CallError::CALL_OK) {
  1497. if (ret.get_type() != Variant::STRING) {
  1498. if (r_valid)
  1499. *r_valid = false;
  1500. ERR_FAIL_V_MSG(String(), "Wrong type for " + CoreStringNames::get_singleton()->_to_string + ", must be a String.");
  1501. }
  1502. if (r_valid)
  1503. *r_valid = true;
  1504. return ret.operator String();
  1505. }
  1506. }
  1507. if (r_valid)
  1508. *r_valid = false;
  1509. return String();
  1510. }
  1511. Ref<Script> VisualScriptInstance::get_script() const {
  1512. return script;
  1513. }
  1514. MultiplayerAPI::RPCMode VisualScriptInstance::get_rpc_mode(const StringName &p_method) const {
  1515. if (p_method == script->get_default_func())
  1516. return MultiplayerAPI::RPC_MODE_DISABLED;
  1517. const Map<StringName, VisualScript::Function>::Element *E = script->functions.find(p_method);
  1518. if (!E) {
  1519. return MultiplayerAPI::RPC_MODE_DISABLED;
  1520. }
  1521. if (E->get().function_id >= 0 && E->get().nodes.has(E->get().function_id)) {
  1522. Ref<VisualScriptFunction> vsf = E->get().nodes[E->get().function_id].node;
  1523. if (vsf.is_valid()) {
  1524. return vsf->get_rpc_mode();
  1525. }
  1526. }
  1527. return MultiplayerAPI::RPC_MODE_DISABLED;
  1528. }
  1529. MultiplayerAPI::RPCMode VisualScriptInstance::get_rset_mode(const StringName &p_variable) const {
  1530. return MultiplayerAPI::RPC_MODE_DISABLED;
  1531. }
  1532. void VisualScriptInstance::create(const Ref<VisualScript> &p_script, Object *p_owner) {
  1533. script = p_script;
  1534. owner = p_owner;
  1535. source = p_script->get_path();
  1536. max_input_args = 0;
  1537. max_output_args = 0;
  1538. if (Object::cast_to<Node>(p_owner)) {
  1539. //turn on these if they exist and base is a node
  1540. Node *node = Object::cast_to<Node>(p_owner);
  1541. if (p_script->functions.has("_process"))
  1542. node->set_process(true);
  1543. if (p_script->functions.has("_physics_process"))
  1544. node->set_physics_process(true);
  1545. if (p_script->functions.has("_input"))
  1546. node->set_process_input(true);
  1547. if (p_script->functions.has("_unhandled_input"))
  1548. node->set_process_unhandled_input(true);
  1549. if (p_script->functions.has("_unhandled_key_input"))
  1550. node->set_process_unhandled_key_input(true);
  1551. }
  1552. for (const Map<StringName, VisualScript::Variable>::Element *E = script->variables.front(); E; E = E->next()) {
  1553. variables[E->key()] = E->get().default_value;
  1554. }
  1555. for (const Map<StringName, VisualScript::Function>::Element *E = script->functions.front(); E; E = E->next()) {
  1556. if (E->key() == script->get_default_func()) {
  1557. continue;
  1558. }
  1559. Function function;
  1560. function.node = E->get().function_id;
  1561. function.max_stack = 0;
  1562. function.flow_stack_size = 0;
  1563. function.pass_stack_size = 0;
  1564. function.node_count = 0;
  1565. Map<StringName, int> local_var_indices;
  1566. if (function.node < 0) {
  1567. VisualScriptLanguage::singleton->debug_break_parse(get_script()->get_path(), 0, "No start node in function: " + String(E->key()));
  1568. ERR_CONTINUE(function.node < 0);
  1569. }
  1570. {
  1571. Ref<VisualScriptFunction> func_node = script->get_node(E->key(), E->get().function_id);
  1572. if (func_node.is_null()) {
  1573. VisualScriptLanguage::singleton->debug_break_parse(get_script()->get_path(), 0, "No VisualScriptFunction typed start node in function: " + String(E->key()));
  1574. }
  1575. ERR_CONTINUE(!func_node.is_valid());
  1576. function.argument_count = func_node->get_argument_count();
  1577. function.max_stack += function.argument_count;
  1578. function.flow_stack_size = func_node->is_stack_less() ? 0 : func_node->get_stack_size();
  1579. max_input_args = MAX(max_input_args, function.argument_count);
  1580. }
  1581. //multiple passes are required to set up this complex thing..
  1582. //first create the nodes
  1583. for (const Map<int, VisualScript::Function::NodeData>::Element *F = E->get().nodes.front(); F; F = F->next()) {
  1584. Ref<VisualScriptNode> node = F->get().node;
  1585. VisualScriptNodeInstance *instance = node->instance(this); //create instance
  1586. ERR_FAIL_COND(!instance);
  1587. instance->base = node.ptr();
  1588. instance->id = F->key();
  1589. instance->input_port_count = node->get_input_value_port_count();
  1590. instance->input_ports = NULL;
  1591. instance->output_port_count = node->get_output_value_port_count();
  1592. instance->output_ports = NULL;
  1593. instance->sequence_output_count = node->get_output_sequence_port_count();
  1594. instance->sequence_index = function.node_count++;
  1595. instance->sequence_outputs = NULL;
  1596. instance->pass_idx = -1;
  1597. if (instance->input_port_count) {
  1598. instance->input_ports = memnew_arr(int, instance->input_port_count);
  1599. for (int i = 0; i < instance->input_port_count; i++) {
  1600. instance->input_ports[i] = -1; //if not assigned, will become default value
  1601. }
  1602. }
  1603. if (instance->output_port_count) {
  1604. instance->output_ports = memnew_arr(int, instance->output_port_count);
  1605. for (int i = 0; i < instance->output_port_count; i++) {
  1606. instance->output_ports[i] = -1; //if not assigned, will output to trash
  1607. }
  1608. }
  1609. if (instance->sequence_output_count) {
  1610. instance->sequence_outputs = memnew_arr(VisualScriptNodeInstance *, instance->sequence_output_count);
  1611. for (int i = 0; i < instance->sequence_output_count; i++) {
  1612. instance->sequence_outputs[i] = NULL; //if it remains null, flow ends here
  1613. }
  1614. }
  1615. if (Object::cast_to<VisualScriptLocalVar>(node.ptr()) || Object::cast_to<VisualScriptLocalVarSet>(*node)) {
  1616. //working memory is shared only for this node, for the same variables
  1617. Ref<VisualScriptLocalVar> vslv = node;
  1618. StringName var_name;
  1619. if (Object::cast_to<VisualScriptLocalVar>(*node))
  1620. var_name = String(Object::cast_to<VisualScriptLocalVar>(*node)->get_var_name()).strip_edges();
  1621. else
  1622. var_name = String(Object::cast_to<VisualScriptLocalVarSet>(*node)->get_var_name()).strip_edges();
  1623. if (!local_var_indices.has(var_name)) {
  1624. local_var_indices[var_name] = function.max_stack;
  1625. function.max_stack++;
  1626. }
  1627. instance->working_mem_idx = local_var_indices[var_name];
  1628. } else if (instance->get_working_memory_size()) {
  1629. instance->working_mem_idx = function.max_stack;
  1630. function.max_stack += instance->get_working_memory_size();
  1631. } else {
  1632. instance->working_mem_idx = -1; //no working mem
  1633. }
  1634. max_input_args = MAX(max_input_args, instance->input_port_count);
  1635. max_output_args = MAX(max_output_args, instance->output_port_count);
  1636. instances[F->key()] = instance;
  1637. }
  1638. function.trash_pos = function.max_stack++; //create pos for trash
  1639. //second pass, do data connections
  1640. for (const Set<VisualScript::DataConnection>::Element *F = E->get().data_connections.front(); F; F = F->next()) {
  1641. VisualScript::DataConnection dc = F->get();
  1642. ERR_CONTINUE(!instances.has(dc.from_node));
  1643. VisualScriptNodeInstance *from = instances[dc.from_node];
  1644. ERR_CONTINUE(!instances.has(dc.to_node));
  1645. VisualScriptNodeInstance *to = instances[dc.to_node];
  1646. ERR_CONTINUE(dc.from_port >= from->output_port_count);
  1647. ERR_CONTINUE(dc.to_port >= to->input_port_count);
  1648. if (from->output_ports[dc.from_port] == -1) {
  1649. int stack_pos = function.max_stack++;
  1650. from->output_ports[dc.from_port] = stack_pos;
  1651. }
  1652. if (from->get_sequence_output_count() == 0 && to->dependencies.find(from) == -1) {
  1653. //if the node we are reading from has no output sequence, we must call step() before reading from it.
  1654. if (from->pass_idx == -1) {
  1655. from->pass_idx = function.pass_stack_size;
  1656. function.pass_stack_size++;
  1657. }
  1658. to->dependencies.push_back(from);
  1659. }
  1660. to->input_ports[dc.to_port] = from->output_ports[dc.from_port]; //read from wherever the stack is
  1661. }
  1662. //third pass, do sequence connections
  1663. for (const Set<VisualScript::SequenceConnection>::Element *F = E->get().sequence_connections.front(); F; F = F->next()) {
  1664. VisualScript::SequenceConnection sc = F->get();
  1665. ERR_CONTINUE(!instances.has(sc.from_node));
  1666. VisualScriptNodeInstance *from = instances[sc.from_node];
  1667. ERR_CONTINUE(!instances.has(sc.to_node));
  1668. VisualScriptNodeInstance *to = instances[sc.to_node];
  1669. ERR_CONTINUE(sc.from_output >= from->sequence_output_count);
  1670. from->sequence_outputs[sc.from_output] = to;
  1671. }
  1672. //fourth pass:
  1673. // 1) unassigned input ports to default values
  1674. // 2) connect unassigned output ports to trash
  1675. for (const Map<int, VisualScript::Function::NodeData>::Element *F = E->get().nodes.front(); F; F = F->next()) {
  1676. ERR_CONTINUE(!instances.has(F->key()));
  1677. Ref<VisualScriptNode> node = F->get().node;
  1678. VisualScriptNodeInstance *instance = instances[F->key()];
  1679. // connect to default values
  1680. for (int i = 0; i < instance->input_port_count; i++) {
  1681. if (instance->input_ports[i] == -1) {
  1682. //unassigned, connect to default val
  1683. instance->input_ports[i] = default_values.size() | VisualScriptNodeInstance::INPUT_DEFAULT_VALUE_BIT;
  1684. default_values.push_back(node->get_default_input_value(i));
  1685. }
  1686. }
  1687. // connect to trash
  1688. for (int i = 0; i < instance->output_port_count; i++) {
  1689. if (instance->output_ports[i] == -1) {
  1690. instance->output_ports[i] = function.trash_pos; //trash is same for all
  1691. }
  1692. }
  1693. }
  1694. functions[E->key()] = function;
  1695. }
  1696. }
  1697. ScriptLanguage *VisualScriptInstance::get_language() {
  1698. return VisualScriptLanguage::singleton;
  1699. }
  1700. VisualScriptInstance::VisualScriptInstance() {
  1701. }
  1702. VisualScriptInstance::~VisualScriptInstance() {
  1703. if (VisualScriptLanguage::singleton->lock)
  1704. VisualScriptLanguage::singleton->lock->lock();
  1705. script->instances.erase(owner);
  1706. if (VisualScriptLanguage::singleton->lock)
  1707. VisualScriptLanguage::singleton->lock->unlock();
  1708. for (Map<int, VisualScriptNodeInstance *>::Element *E = instances.front(); E; E = E->next()) {
  1709. memdelete(E->get());
  1710. }
  1711. }
  1712. /////////////////////////////////////////////
  1713. /////////////////////
  1714. Variant VisualScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1715. ERR_FAIL_COND_V(function == StringName(), Variant());
  1716. #ifdef DEBUG_ENABLED
  1717. ERR_FAIL_COND_V_MSG(instance_id && !ObjectDB::get_instance(instance_id), Variant(), "Resumed after yield, but class instance is gone.");
  1718. ERR_FAIL_COND_V_MSG(script_id && !ObjectDB::get_instance(script_id), Variant(), "Resumed after yield, but script is gone.");
  1719. #endif
  1720. r_error.error = Variant::CallError::CALL_OK;
  1721. Array args;
  1722. if (p_argcount == 0) {
  1723. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1724. r_error.argument = 1;
  1725. return Variant();
  1726. } else if (p_argcount == 1) {
  1727. //noooneee, reserved for me, me and only me.
  1728. } else {
  1729. for (int i = 0; i < p_argcount - 1; i++) {
  1730. args.push_back(*p_args[i]);
  1731. }
  1732. }
  1733. Ref<VisualScriptFunctionState> self = *p_args[p_argcount - 1]; //hi, I'm myself, needed this to remain alive.
  1734. if (self.is_null()) {
  1735. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1736. r_error.argument = p_argcount - 1;
  1737. r_error.expected = Variant::OBJECT;
  1738. return Variant();
  1739. }
  1740. r_error.error = Variant::CallError::CALL_OK;
  1741. Variant *working_mem = ((Variant *)stack.ptr()) + working_mem_index;
  1742. *working_mem = args; //arguments go to working mem.
  1743. Variant ret = instance->_call_internal(function, stack.ptrw(), stack.size(), node, flow_stack_pos, pass, true, r_error);
  1744. function = StringName(); //invalidate
  1745. return ret;
  1746. }
  1747. void VisualScriptFunctionState::connect_to_signal(Object *p_obj, const String &p_signal, Array p_binds) {
  1748. Vector<Variant> binds;
  1749. for (int i = 0; i < p_binds.size(); i++) {
  1750. binds.push_back(p_binds[i]);
  1751. }
  1752. binds.push_back(Ref<VisualScriptFunctionState>(this)); //add myself on the back to avoid dying from unreferencing
  1753. p_obj->connect(p_signal, this, "_signal_callback", binds, CONNECT_ONESHOT);
  1754. }
  1755. bool VisualScriptFunctionState::is_valid() const {
  1756. return function != StringName();
  1757. }
  1758. Variant VisualScriptFunctionState::resume(Array p_args) {
  1759. ERR_FAIL_COND_V(function == StringName(), Variant());
  1760. #ifdef DEBUG_ENABLED
  1761. ERR_FAIL_COND_V_MSG(instance_id && !ObjectDB::get_instance(instance_id), Variant(), "Resumed after yield, but class instance is gone.");
  1762. ERR_FAIL_COND_V_MSG(script_id && !ObjectDB::get_instance(script_id), Variant(), "Resumed after yield, but script is gone.");
  1763. #endif
  1764. Variant::CallError r_error;
  1765. r_error.error = Variant::CallError::CALL_OK;
  1766. Variant *working_mem = ((Variant *)stack.ptr()) + working_mem_index;
  1767. *working_mem = p_args; //arguments go to working mem.
  1768. Variant ret = instance->_call_internal(function, stack.ptrw(), stack.size(), node, flow_stack_pos, pass, true, r_error);
  1769. function = StringName(); //invalidate
  1770. return ret;
  1771. }
  1772. void VisualScriptFunctionState::_bind_methods() {
  1773. ClassDB::bind_method(D_METHOD("connect_to_signal", "obj", "signals", "args"), &VisualScriptFunctionState::connect_to_signal);
  1774. ClassDB::bind_method(D_METHOD("resume", "args"), &VisualScriptFunctionState::resume, DEFVAL(Variant()));
  1775. ClassDB::bind_method(D_METHOD("is_valid"), &VisualScriptFunctionState::is_valid);
  1776. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &VisualScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1777. }
  1778. VisualScriptFunctionState::VisualScriptFunctionState() {
  1779. }
  1780. VisualScriptFunctionState::~VisualScriptFunctionState() {
  1781. if (function != StringName()) {
  1782. Variant *s = ((Variant *)stack.ptr());
  1783. for (int i = 0; i < variant_stack_size; i++) {
  1784. s[i].~Variant();
  1785. }
  1786. }
  1787. }
  1788. ///////////////////////////////////////////////
  1789. String VisualScriptLanguage::get_name() const {
  1790. return "VisualScript";
  1791. }
  1792. /* LANGUAGE FUNCTIONS */
  1793. void VisualScriptLanguage::init() {
  1794. }
  1795. String VisualScriptLanguage::get_type() const {
  1796. return "VisualScript";
  1797. }
  1798. String VisualScriptLanguage::get_extension() const {
  1799. return "vs";
  1800. }
  1801. Error VisualScriptLanguage::execute_file(const String &p_path) {
  1802. return OK;
  1803. }
  1804. void VisualScriptLanguage::finish() {
  1805. }
  1806. /* EDITOR FUNCTIONS */
  1807. void VisualScriptLanguage::get_reserved_words(List<String> *p_words) const {
  1808. }
  1809. void VisualScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  1810. }
  1811. void VisualScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  1812. }
  1813. Ref<Script> VisualScriptLanguage::get_template(const String &p_class_name, const String &p_base_class_name) const {
  1814. Ref<VisualScript> script;
  1815. script.instance();
  1816. script->set_instance_base_type(p_base_class_name);
  1817. return script;
  1818. }
  1819. bool VisualScriptLanguage::is_using_templates() {
  1820. return true;
  1821. }
  1822. void VisualScriptLanguage::make_template(const String &p_class_name, const String &p_base_class_name, Ref<Script> &p_script) {
  1823. Ref<VisualScript> script = p_script;
  1824. script->set_instance_base_type(p_base_class_name);
  1825. }
  1826. 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 {
  1827. return false;
  1828. }
  1829. Script *VisualScriptLanguage::create_script() const {
  1830. return memnew(VisualScript);
  1831. }
  1832. bool VisualScriptLanguage::has_named_classes() const {
  1833. return false;
  1834. }
  1835. bool VisualScriptLanguage::supports_builtin_mode() const {
  1836. return true;
  1837. }
  1838. int VisualScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  1839. return -1;
  1840. }
  1841. String VisualScriptLanguage::make_function(const String &p_class, const String &p_name, const PoolStringArray &p_args) const {
  1842. return String();
  1843. }
  1844. void VisualScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  1845. }
  1846. void VisualScriptLanguage::add_global_constant(const StringName &p_variable, const Variant &p_value) {
  1847. }
  1848. /* DEBUGGER FUNCTIONS */
  1849. bool VisualScriptLanguage::debug_break_parse(const String &p_file, int p_node, const String &p_error) {
  1850. //break because of parse error
  1851. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  1852. _debug_parse_err_node = p_node;
  1853. _debug_parse_err_file = p_file;
  1854. _debug_error = p_error;
  1855. ScriptDebugger::get_singleton()->debug(this, false, true);
  1856. return true;
  1857. } else {
  1858. return false;
  1859. }
  1860. }
  1861. bool VisualScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  1862. if (ScriptDebugger::get_singleton() && Thread::get_caller_id() == Thread::get_main_id()) {
  1863. _debug_parse_err_node = -1;
  1864. _debug_parse_err_file = "";
  1865. _debug_error = p_error;
  1866. ScriptDebugger::get_singleton()->debug(this, p_allow_continue, true);
  1867. return true;
  1868. } else {
  1869. return false;
  1870. }
  1871. }
  1872. String VisualScriptLanguage::debug_get_error() const {
  1873. return _debug_error;
  1874. }
  1875. int VisualScriptLanguage::debug_get_stack_level_count() const {
  1876. if (_debug_parse_err_node >= 0)
  1877. return 1;
  1878. return _debug_call_stack_pos;
  1879. }
  1880. int VisualScriptLanguage::debug_get_stack_level_line(int p_level) const {
  1881. if (_debug_parse_err_node >= 0)
  1882. return _debug_parse_err_node;
  1883. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, -1);
  1884. int l = _debug_call_stack_pos - p_level - 1;
  1885. return *(_call_stack[l].current_id);
  1886. }
  1887. String VisualScriptLanguage::debug_get_stack_level_function(int p_level) const {
  1888. if (_debug_parse_err_node >= 0)
  1889. return "";
  1890. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  1891. int l = _debug_call_stack_pos - p_level - 1;
  1892. return *_call_stack[l].function;
  1893. }
  1894. String VisualScriptLanguage::debug_get_stack_level_source(int p_level) const {
  1895. if (_debug_parse_err_node >= 0)
  1896. return _debug_parse_err_file;
  1897. ERR_FAIL_INDEX_V(p_level, _debug_call_stack_pos, "");
  1898. int l = _debug_call_stack_pos - p_level - 1;
  1899. return _call_stack[l].instance->get_script_ptr()->get_path();
  1900. }
  1901. 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) {
  1902. if (_debug_parse_err_node >= 0)
  1903. return;
  1904. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  1905. int l = _debug_call_stack_pos - p_level - 1;
  1906. const StringName *f = _call_stack[l].function;
  1907. ERR_FAIL_COND(!_call_stack[l].instance->functions.has(*f));
  1908. //VisualScriptInstance::Function *func = &_call_stack[l].instance->functions[*f];
  1909. VisualScriptNodeInstance *node = _call_stack[l].instance->instances[*_call_stack[l].current_id];
  1910. ERR_FAIL_COND(!node);
  1911. p_locals->push_back("node_name");
  1912. p_values->push_back(node->get_base_node()->get_text());
  1913. for (int i = 0; i < node->input_port_count; i++) {
  1914. String name = node->get_base_node()->get_input_value_port_info(i).name;
  1915. if (name == String()) {
  1916. name = "in_" + itos(i);
  1917. }
  1918. p_locals->push_back("input/" + name);
  1919. //value is trickier
  1920. int in_from = node->input_ports[i];
  1921. int in_value = in_from & VisualScriptNodeInstance::INPUT_MASK;
  1922. if (in_from & VisualScriptNodeInstance::INPUT_DEFAULT_VALUE_BIT) {
  1923. p_values->push_back(_call_stack[l].instance->default_values[in_value]);
  1924. } else {
  1925. p_values->push_back(_call_stack[l].stack[in_value]);
  1926. }
  1927. }
  1928. for (int i = 0; i < node->output_port_count; i++) {
  1929. String name = node->get_base_node()->get_output_value_port_info(i).name;
  1930. if (name == String()) {
  1931. name = "out_" + itos(i);
  1932. }
  1933. p_locals->push_back("output/" + name);
  1934. //value is trickier
  1935. int in_from = node->output_ports[i];
  1936. p_values->push_back(_call_stack[l].stack[in_from]);
  1937. }
  1938. for (int i = 0; i < node->get_working_memory_size(); i++) {
  1939. p_locals->push_back("working_mem/mem_" + itos(i));
  1940. p_values->push_back((*_call_stack[l].work_mem)[i]);
  1941. }
  1942. /*
  1943. ERR_FAIL_INDEX(p_level,_debug_call_stack_pos);
  1944. VisualFunction *f = _call_stack[l].function;
  1945. List<Pair<StringName,int> > locals;
  1946. f->debug_get_stack_member_state(*_call_stack[l].line,&locals);
  1947. for( List<Pair<StringName,int> >::Element *E = locals.front();E;E=E->next() ) {
  1948. p_locals->push_back(E->get().first);
  1949. p_values->push_back(_call_stack[l].stack[E->get().second]);
  1950. }
  1951. */
  1952. }
  1953. 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) {
  1954. if (_debug_parse_err_node >= 0)
  1955. return;
  1956. ERR_FAIL_INDEX(p_level, _debug_call_stack_pos);
  1957. int l = _debug_call_stack_pos - p_level - 1;
  1958. Ref<VisualScript> vs = _call_stack[l].instance->get_script();
  1959. if (vs.is_null())
  1960. return;
  1961. List<StringName> vars;
  1962. vs->get_variable_list(&vars);
  1963. for (List<StringName>::Element *E = vars.front(); E; E = E->next()) {
  1964. Variant v;
  1965. if (_call_stack[l].instance->get_variable(E->get(), &v)) {
  1966. p_members->push_back("variables/" + E->get());
  1967. p_values->push_back(v);
  1968. }
  1969. }
  1970. }
  1971. void VisualScriptLanguage::debug_get_globals(List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  1972. //no globals are really reachable in gdscript
  1973. }
  1974. String VisualScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  1975. return "";
  1976. }
  1977. void VisualScriptLanguage::reload_all_scripts() {
  1978. }
  1979. void VisualScriptLanguage::reload_tool_script(const Ref<Script> &p_script, bool p_soft_reload) {
  1980. }
  1981. /* LOADER FUNCTIONS */
  1982. void VisualScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  1983. p_extensions->push_back("vs");
  1984. }
  1985. void VisualScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  1986. }
  1987. void VisualScriptLanguage::get_public_constants(List<Pair<String, Variant> > *p_constants) const {
  1988. }
  1989. void VisualScriptLanguage::profiling_start() {
  1990. }
  1991. void VisualScriptLanguage::profiling_stop() {
  1992. }
  1993. int VisualScriptLanguage::profiling_get_accumulated_data(ProfilingInfo *p_info_arr, int p_info_max) {
  1994. return 0;
  1995. }
  1996. int VisualScriptLanguage::profiling_get_frame_data(ProfilingInfo *p_info_arr, int p_info_max) {
  1997. return 0;
  1998. }
  1999. VisualScriptLanguage *VisualScriptLanguage::singleton = NULL;
  2000. void VisualScriptLanguage::add_register_func(const String &p_name, VisualScriptNodeRegisterFunc p_func) {
  2001. ERR_FAIL_COND(register_funcs.has(p_name));
  2002. register_funcs[p_name] = p_func;
  2003. }
  2004. void VisualScriptLanguage::remove_register_func(const String &p_name) {
  2005. ERR_FAIL_COND(!register_funcs.has(p_name));
  2006. register_funcs.erase(p_name);
  2007. }
  2008. Ref<VisualScriptNode> VisualScriptLanguage::create_node_from_name(const String &p_name) {
  2009. ERR_FAIL_COND_V(!register_funcs.has(p_name), Ref<VisualScriptNode>());
  2010. return register_funcs[p_name](p_name);
  2011. }
  2012. void VisualScriptLanguage::get_registered_node_names(List<String> *r_names) {
  2013. for (Map<String, VisualScriptNodeRegisterFunc>::Element *E = register_funcs.front(); E; E = E->next()) {
  2014. r_names->push_back(E->key());
  2015. }
  2016. }
  2017. VisualScriptLanguage::VisualScriptLanguage() {
  2018. notification = "_notification";
  2019. _step = "_step";
  2020. _subcall = "_subcall";
  2021. singleton = this;
  2022. #ifndef NO_THREADS
  2023. lock = Mutex::create();
  2024. #endif
  2025. _debug_parse_err_node = -1;
  2026. _debug_parse_err_file = "";
  2027. _debug_call_stack_pos = 0;
  2028. int dmcs = GLOBAL_DEF("debug/settings/visual_script/max_call_stack", 1024);
  2029. 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
  2030. if (ScriptDebugger::get_singleton()) {
  2031. //debugging enabled!
  2032. _debug_max_call_stack = dmcs;
  2033. _call_stack = memnew_arr(CallLevel, _debug_max_call_stack + 1);
  2034. } else {
  2035. _debug_max_call_stack = 0;
  2036. _call_stack = NULL;
  2037. }
  2038. }
  2039. VisualScriptLanguage::~VisualScriptLanguage() {
  2040. if (lock)
  2041. memdelete(lock);
  2042. if (_call_stack) {
  2043. memdelete_arr(_call_stack);
  2044. }
  2045. singleton = NULL;
  2046. }