visual_script.cpp 84 KB

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