visual_script.cpp 82 KB

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