visual_script.cpp 78 KB

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