visual_script.cpp 84 KB

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