visual_script.cpp 80 KB

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