visual_script.cpp 88 KB

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