visual_script.cpp 76 KB

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