variant.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303
  1. /*************************************************************************/
  2. /* variant.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "variant.h"
  30. #include "math_funcs.h"
  31. #include "resource.h"
  32. #include "print_string.h"
  33. #include "scene/main/node.h"
  34. #include "scene/gui/control.h"
  35. #include "io/marshalls.h"
  36. #include "core_string_names.h"
  37. #include "variant_parser.h"
  38. String Variant::get_type_name(Variant::Type p_type) {
  39. switch( p_type ) {
  40. case NIL: {
  41. return "Nil";
  42. } break;
  43. // atomic types
  44. case BOOL: {
  45. return "bool";
  46. } break;
  47. case INT: {
  48. return "int";
  49. } break;
  50. case REAL: {
  51. return "float";
  52. } break;
  53. case STRING: {
  54. return "String";
  55. } break;
  56. // math types
  57. case VECTOR2: {
  58. return "Vector2";
  59. } break;
  60. case RECT2: {
  61. return "Rect2";
  62. } break;
  63. case TRANSFORM2D: {
  64. return "Transform2D";
  65. } break;
  66. case VECTOR3: {
  67. return "Vector3";
  68. } break;
  69. case PLANE: {
  70. return "Plane";
  71. } break;
  72. /*
  73. case QUAT: {
  74. } break;*/
  75. case RECT3: {
  76. return "Rect3";
  77. } break;
  78. case QUAT: {
  79. return "Quat";
  80. } break;
  81. case BASIS: {
  82. return "Basis";
  83. } break;
  84. case TRANSFORM: {
  85. return "Transform";
  86. } break;
  87. // misc types
  88. case COLOR: {
  89. return "Color";
  90. } break;
  91. case IMAGE: {
  92. return "Image";
  93. } break;
  94. case _RID: {
  95. return "RID";
  96. } break;
  97. case OBJECT: {
  98. return "Object";
  99. } break;
  100. case NODE_PATH: {
  101. return "NodePath";
  102. } break;
  103. case INPUT_EVENT: {
  104. return "InputEvent";
  105. } break;
  106. case DICTIONARY: {
  107. return "Dictionary";
  108. } break;
  109. case ARRAY: {
  110. return "Array";
  111. } break;
  112. // arrays
  113. case POOL_BYTE_ARRAY: {
  114. return "PoolByteArray";
  115. } break;
  116. case POOL_INT_ARRAY: {
  117. return "PoolIntArray";
  118. } break;
  119. case POOL_REAL_ARRAY: {
  120. return "PoolFloatArray";
  121. } break;
  122. case POOL_STRING_ARRAY: {
  123. return "PoolStringArray";
  124. } break;
  125. case POOL_VECTOR2_ARRAY: {
  126. return "PoolVector2Array";
  127. } break;
  128. case POOL_VECTOR3_ARRAY: {
  129. return "PoolVector3Array";
  130. } break;
  131. case POOL_COLOR_ARRAY: {
  132. return "PoolColorArray";
  133. } break;
  134. default: {}
  135. }
  136. return "";
  137. }
  138. bool Variant::can_convert(Variant::Type p_type_from,Variant::Type p_type_to) {
  139. if (p_type_from==p_type_to)
  140. return true;
  141. if (p_type_to==NIL && p_type_from!=NIL) //nil can convert to anything
  142. return true;
  143. if (p_type_from == NIL) {
  144. return (p_type_to == OBJECT);
  145. };
  146. const Type *valid_types=NULL;
  147. const Type *invalid_types=NULL;
  148. switch(p_type_to) {
  149. case BOOL: {
  150. static const Type valid[]={
  151. INT,
  152. REAL,
  153. STRING,
  154. NIL,
  155. };
  156. valid_types=valid;
  157. } break;
  158. case INT: {
  159. static const Type valid[]={
  160. BOOL,
  161. REAL,
  162. STRING,
  163. NIL,
  164. };
  165. valid_types=valid;
  166. } break;
  167. case REAL: {
  168. static const Type valid[]={
  169. BOOL,
  170. INT,
  171. STRING,
  172. NIL,
  173. };
  174. valid_types=valid;
  175. } break;
  176. case STRING: {
  177. static const Type invalid[]={
  178. OBJECT,
  179. IMAGE,
  180. NIL
  181. };
  182. invalid_types=invalid;
  183. } break;
  184. case TRANSFORM2D: {
  185. static const Type valid[]={
  186. TRANSFORM,
  187. NIL
  188. };
  189. valid_types=valid;
  190. } break;
  191. case QUAT: {
  192. static const Type valid[]={
  193. BASIS,
  194. NIL
  195. };
  196. valid_types=valid;
  197. } break;
  198. case BASIS: {
  199. static const Type valid[]={
  200. QUAT,
  201. NIL
  202. };
  203. valid_types=valid;
  204. } break;
  205. case TRANSFORM: {
  206. static const Type valid[]={
  207. TRANSFORM2D,
  208. QUAT,
  209. BASIS,
  210. NIL
  211. };
  212. valid_types=valid;
  213. } break;
  214. case COLOR: {
  215. static const Type valid[] = {
  216. STRING,
  217. INT,
  218. NIL,
  219. };
  220. valid_types = valid;
  221. } break;
  222. case _RID: {
  223. static const Type valid[]={
  224. OBJECT,
  225. NIL
  226. };
  227. valid_types=valid;
  228. } break;
  229. case OBJECT: {
  230. static const Type valid[]={
  231. NIL
  232. };
  233. valid_types=valid;
  234. } break;
  235. case NODE_PATH: {
  236. static const Type valid[]={
  237. STRING,
  238. NIL
  239. };
  240. valid_types=valid;
  241. } break;
  242. case ARRAY: {
  243. static const Type valid[]={
  244. POOL_BYTE_ARRAY,
  245. POOL_INT_ARRAY,
  246. POOL_STRING_ARRAY,
  247. POOL_REAL_ARRAY,
  248. POOL_COLOR_ARRAY,
  249. POOL_VECTOR2_ARRAY,
  250. POOL_VECTOR3_ARRAY,
  251. NIL
  252. };
  253. valid_types=valid;
  254. } break;
  255. // arrays
  256. case POOL_BYTE_ARRAY: {
  257. static const Type valid[]={
  258. ARRAY,
  259. NIL
  260. };
  261. valid_types=valid;
  262. } break;
  263. case POOL_INT_ARRAY: {
  264. static const Type valid[]={
  265. ARRAY,
  266. NIL
  267. };
  268. valid_types=valid;
  269. } break;
  270. case POOL_REAL_ARRAY: {
  271. static const Type valid[]={
  272. ARRAY,
  273. NIL
  274. };
  275. valid_types=valid;
  276. } break;
  277. case POOL_STRING_ARRAY: {
  278. static const Type valid[]={
  279. ARRAY,
  280. NIL
  281. };
  282. valid_types=valid;
  283. } break;
  284. case POOL_VECTOR2_ARRAY: {
  285. static const Type valid[]={
  286. ARRAY,
  287. NIL
  288. };
  289. valid_types=valid;
  290. } break;
  291. case POOL_VECTOR3_ARRAY: {
  292. static const Type valid[]={
  293. ARRAY,
  294. NIL
  295. };
  296. valid_types=valid;
  297. } break;
  298. case POOL_COLOR_ARRAY: {
  299. static const Type valid[]={
  300. ARRAY,
  301. NIL
  302. };
  303. valid_types=valid;
  304. } break;
  305. default: {}
  306. }
  307. if (valid_types) {
  308. int i=0;
  309. while(valid_types[i]!=NIL) {
  310. if (p_type_from==valid_types[i])
  311. return true;
  312. i++;
  313. }
  314. } else if (invalid_types) {
  315. int i=0;
  316. while(invalid_types[i]!=NIL) {
  317. if (p_type_from==invalid_types[i])
  318. return false;
  319. i++;
  320. }
  321. return true;
  322. }
  323. return false;
  324. }
  325. bool Variant::can_convert_strict(Variant::Type p_type_from,Variant::Type p_type_to) {
  326. if (p_type_from==p_type_to)
  327. return true;
  328. if (p_type_to==NIL && p_type_from!=NIL) //nil can convert to anything
  329. return true;
  330. if (p_type_from == NIL) {
  331. return (p_type_to == OBJECT);
  332. };
  333. const Type *valid_types=NULL;
  334. switch(p_type_to) {
  335. case BOOL: {
  336. static const Type valid[]={
  337. INT,
  338. REAL,
  339. //STRING,
  340. NIL,
  341. };
  342. valid_types=valid;
  343. } break;
  344. case INT: {
  345. static const Type valid[]={
  346. BOOL,
  347. REAL,
  348. //STRING,
  349. NIL,
  350. };
  351. valid_types=valid;
  352. } break;
  353. case REAL: {
  354. static const Type valid[]={
  355. BOOL,
  356. INT,
  357. //STRING,
  358. NIL,
  359. };
  360. valid_types=valid;
  361. } break;
  362. case STRING: {
  363. static const Type valid[]={
  364. NODE_PATH,
  365. NIL
  366. };
  367. valid_types=valid;
  368. } break;
  369. case TRANSFORM2D: {
  370. static const Type valid[]={
  371. TRANSFORM,
  372. NIL
  373. };
  374. valid_types=valid;
  375. } break;
  376. case QUAT: {
  377. static const Type valid[]={
  378. BASIS,
  379. NIL
  380. };
  381. valid_types=valid;
  382. } break;
  383. case BASIS: {
  384. static const Type valid[]={
  385. QUAT,
  386. NIL
  387. };
  388. valid_types=valid;
  389. } break;
  390. case TRANSFORM: {
  391. static const Type valid[]={
  392. TRANSFORM2D,
  393. QUAT,
  394. BASIS,
  395. NIL
  396. };
  397. valid_types=valid;
  398. } break;
  399. case COLOR: {
  400. static const Type valid[] = {
  401. STRING,
  402. INT,
  403. NIL,
  404. };
  405. valid_types = valid;
  406. } break;
  407. case _RID: {
  408. static const Type valid[]={
  409. OBJECT,
  410. NIL
  411. };
  412. valid_types=valid;
  413. } break;
  414. case OBJECT: {
  415. static const Type valid[]={
  416. NIL
  417. };
  418. valid_types=valid;
  419. } break;
  420. case NODE_PATH: {
  421. static const Type valid[]={
  422. STRING,
  423. NIL
  424. };
  425. valid_types=valid;
  426. } break;
  427. case ARRAY: {
  428. static const Type valid[]={
  429. POOL_BYTE_ARRAY,
  430. POOL_INT_ARRAY,
  431. POOL_STRING_ARRAY,
  432. POOL_REAL_ARRAY,
  433. POOL_COLOR_ARRAY,
  434. POOL_VECTOR2_ARRAY,
  435. POOL_VECTOR3_ARRAY,
  436. NIL
  437. };
  438. valid_types=valid;
  439. } break;
  440. // arrays
  441. case POOL_BYTE_ARRAY: {
  442. static const Type valid[]={
  443. ARRAY,
  444. NIL
  445. };
  446. valid_types=valid;
  447. } break;
  448. case POOL_INT_ARRAY: {
  449. static const Type valid[]={
  450. ARRAY,
  451. NIL
  452. };
  453. valid_types=valid;
  454. } break;
  455. case POOL_REAL_ARRAY: {
  456. static const Type valid[]={
  457. ARRAY,
  458. NIL
  459. };
  460. valid_types=valid;
  461. } break;
  462. case POOL_STRING_ARRAY: {
  463. static const Type valid[]={
  464. ARRAY,
  465. NIL
  466. };
  467. valid_types=valid;
  468. } break;
  469. case POOL_VECTOR2_ARRAY: {
  470. static const Type valid[]={
  471. ARRAY,
  472. NIL
  473. };
  474. valid_types=valid;
  475. } break;
  476. case POOL_VECTOR3_ARRAY: {
  477. static const Type valid[]={
  478. ARRAY,
  479. NIL
  480. };
  481. valid_types=valid;
  482. } break;
  483. case POOL_COLOR_ARRAY: {
  484. static const Type valid[]={
  485. ARRAY,
  486. NIL
  487. };
  488. valid_types=valid;
  489. } break;
  490. default: {}
  491. }
  492. if (valid_types) {
  493. int i=0;
  494. while(valid_types[i]!=NIL) {
  495. if (p_type_from==valid_types[i])
  496. return true;
  497. i++;
  498. }
  499. }
  500. return false;
  501. }
  502. bool Variant::operator==(const Variant& p_variant) const {
  503. if (type!=p_variant.type) //evaluation of operator== needs to be more strict
  504. return false;
  505. bool v;
  506. Variant r;
  507. evaluate(OP_EQUAL,*this,p_variant,r,v);
  508. return r;
  509. }
  510. bool Variant::operator!=(const Variant& p_variant) const {
  511. if (type!=p_variant.type) //evaluation of operator== needs to be more strict
  512. return true;
  513. bool v;
  514. Variant r;
  515. evaluate(OP_NOT_EQUAL,*this,p_variant,r,v);
  516. return r;
  517. }
  518. bool Variant::operator<(const Variant& p_variant) const {
  519. if (type!=p_variant.type) //if types differ, then order by type first
  520. return type<p_variant.type;
  521. bool v;
  522. Variant r;
  523. evaluate(OP_LESS,*this,p_variant,r,v);
  524. return r;
  525. }
  526. bool Variant::is_zero() const {
  527. switch( type ) {
  528. case NIL: {
  529. return true;
  530. } break;
  531. // atomic types
  532. case BOOL: {
  533. return _data._bool==false;
  534. } break;
  535. case INT: {
  536. return _data._int==0;
  537. } break;
  538. case REAL: {
  539. return _data._real==0;
  540. } break;
  541. case STRING: {
  542. return *reinterpret_cast<const String*>(_data._mem)==String();
  543. } break;
  544. // math types
  545. case VECTOR2: {
  546. return *reinterpret_cast<const Vector2*>(_data._mem)==Vector2();
  547. } break;
  548. case RECT2: {
  549. return *reinterpret_cast<const Rect2*>(_data._mem)==Rect2();
  550. } break;
  551. case TRANSFORM2D: {
  552. return *_data._transform2d==Transform2D();
  553. } break;
  554. case VECTOR3: {
  555. return *reinterpret_cast<const Vector3*>(_data._mem)==Vector3();
  556. } break;
  557. case PLANE: {
  558. return *reinterpret_cast<const Plane*>(_data._mem)==Plane();
  559. } break;
  560. /*
  561. case QUAT: {
  562. } break;*/
  563. case RECT3: {
  564. return *_data._rect3==Rect3();
  565. } break;
  566. case QUAT: {
  567. return *reinterpret_cast<const Quat*>(_data._mem)==Quat();
  568. } break;
  569. case BASIS: {
  570. return *_data._basis==Basis();
  571. } break;
  572. case TRANSFORM: {
  573. return *_data._transform == Transform();
  574. } break;
  575. // misc types
  576. case COLOR: {
  577. return *reinterpret_cast<const Color*>(_data._mem)==Color();
  578. } break;
  579. case IMAGE: {
  580. return _data._image->empty();
  581. } break;
  582. case _RID: {
  583. return *reinterpret_cast<const RID*>(_data._mem)==RID();
  584. } break;
  585. case OBJECT: {
  586. return _get_obj().obj==NULL;
  587. } break;
  588. case NODE_PATH: {
  589. return reinterpret_cast<const NodePath*>(_data._mem)->is_empty();
  590. } break;
  591. case INPUT_EVENT: {
  592. return _data._input_event->type==InputEvent::NONE;
  593. } break;
  594. case DICTIONARY: {
  595. return reinterpret_cast<const Dictionary*>(_data._mem)->empty();
  596. } break;
  597. case ARRAY: {
  598. return reinterpret_cast<const Array*>(_data._mem)->empty();
  599. } break;
  600. // arrays
  601. case POOL_BYTE_ARRAY: {
  602. return reinterpret_cast<const PoolVector<uint8_t>*>(_data._mem)->size()==0;
  603. } break;
  604. case POOL_INT_ARRAY: {
  605. return reinterpret_cast<const PoolVector<int>*>(_data._mem)->size()==0;
  606. } break;
  607. case POOL_REAL_ARRAY: {
  608. return reinterpret_cast<const PoolVector<real_t>*>(_data._mem)->size()==0;
  609. } break;
  610. case POOL_STRING_ARRAY: {
  611. return reinterpret_cast<const PoolVector<String>*>(_data._mem)->size()==0;
  612. } break;
  613. case POOL_VECTOR2_ARRAY: {
  614. return reinterpret_cast<const PoolVector<Vector2>*>(_data._mem)->size()==0;
  615. } break;
  616. case POOL_VECTOR3_ARRAY: {
  617. return reinterpret_cast<const PoolVector<Vector3>*>(_data._mem)->size()==0;
  618. } break;
  619. case POOL_COLOR_ARRAY: {
  620. return reinterpret_cast<const PoolVector<Color>*>(_data._mem)->size()==0;
  621. } break;
  622. default: {}
  623. }
  624. return false;
  625. }
  626. bool Variant::is_one() const {
  627. switch( type ) {
  628. case NIL: {
  629. return true;
  630. } break;
  631. // atomic types
  632. case BOOL: {
  633. return _data._bool==true;
  634. } break;
  635. case INT: {
  636. return _data._int==1;
  637. } break;
  638. case REAL: {
  639. return _data._real==1;
  640. } break;
  641. case VECTOR2: {
  642. return *reinterpret_cast<const Vector2*>(_data._mem)==Vector2(1,1);
  643. } break;
  644. case RECT2: {
  645. return *reinterpret_cast<const Rect2*>(_data._mem)==Rect2(1,1,1,1);
  646. } break;
  647. case VECTOR3: {
  648. return *reinterpret_cast<const Vector3*>(_data._mem)==Vector3(1,1,1);
  649. } break;
  650. case PLANE: {
  651. return *reinterpret_cast<const Plane*>(_data._mem)==Plane(1,1,1,1);
  652. } break;
  653. case COLOR: {
  654. return *reinterpret_cast<const Color*>(_data._mem)==Color(1,1,1,1);
  655. } break;
  656. default: { return !is_zero(); }
  657. }
  658. return false;
  659. }
  660. void Variant::reference(const Variant& p_variant) {
  661. if (this == &p_variant)
  662. return;
  663. clear();
  664. type=p_variant.type;
  665. switch( p_variant.type ) {
  666. case NIL: {
  667. // none
  668. } break;
  669. // atomic types
  670. case BOOL: {
  671. _data._bool=p_variant._data._bool;
  672. } break;
  673. case INT: {
  674. _data._int=p_variant._data._int;
  675. } break;
  676. case REAL: {
  677. _data._real=p_variant._data._real;
  678. } break;
  679. case STRING: {
  680. memnew_placement( _data._mem, String( *reinterpret_cast<const String*>(p_variant._data._mem) ) );
  681. } break;
  682. // math types
  683. case VECTOR2: {
  684. memnew_placement( _data._mem, Vector2( *reinterpret_cast<const Vector2*>(p_variant._data._mem) ) );
  685. } break;
  686. case RECT2: {
  687. memnew_placement( _data._mem, Rect2( *reinterpret_cast<const Rect2*>(p_variant._data._mem) ) );
  688. } break;
  689. case TRANSFORM2D: {
  690. _data._transform2d = memnew( Transform2D( *p_variant._data._transform2d ) );
  691. } break;
  692. case VECTOR3: {
  693. memnew_placement( _data._mem, Vector3( *reinterpret_cast<const Vector3*>(p_variant._data._mem) ) );
  694. } break;
  695. case PLANE: {
  696. memnew_placement( _data._mem, Plane( *reinterpret_cast<const Plane*>(p_variant._data._mem) ) );
  697. } break;
  698. /*
  699. case QUAT: {
  700. } break;*/
  701. case RECT3: {
  702. _data._rect3 = memnew( Rect3( *p_variant._data._rect3 ) );
  703. } break;
  704. case QUAT: {
  705. memnew_placement( _data._mem, Quat( *reinterpret_cast<const Quat*>(p_variant._data._mem) ) );
  706. } break;
  707. case BASIS: {
  708. _data._basis = memnew( Basis( *p_variant._data._basis ) );
  709. } break;
  710. case TRANSFORM: {
  711. _data._transform = memnew( Transform( *p_variant._data._transform ) );
  712. } break;
  713. // misc types
  714. case COLOR: {
  715. memnew_placement( _data._mem, Color( *reinterpret_cast<const Color*>(p_variant._data._mem) ) );
  716. } break;
  717. case IMAGE: {
  718. _data._image = memnew( Image( *p_variant._data._image ) );
  719. } break;
  720. case _RID: {
  721. memnew_placement( _data._mem, RID( *reinterpret_cast<const RID*>(p_variant._data._mem) ) );
  722. } break;
  723. case OBJECT: {
  724. memnew_placement( _data._mem, ObjData( p_variant._get_obj() ) );
  725. } break;
  726. case NODE_PATH: {
  727. memnew_placement( _data._mem, NodePath( *reinterpret_cast<const NodePath*>(p_variant._data._mem) ) );
  728. } break;
  729. case INPUT_EVENT: {
  730. _data._input_event= memnew( InputEvent( *p_variant._data._input_event ) );
  731. } break;
  732. case DICTIONARY: {
  733. memnew_placement( _data._mem, Dictionary( *reinterpret_cast<const Dictionary*>(p_variant._data._mem) ) );
  734. } break;
  735. case ARRAY: {
  736. memnew_placement( _data._mem, Array ( *reinterpret_cast<const Array*>(p_variant._data._mem) ) );
  737. } break;
  738. // arrays
  739. case POOL_BYTE_ARRAY: {
  740. memnew_placement( _data._mem, PoolVector<uint8_t> ( *reinterpret_cast<const PoolVector<uint8_t>*>(p_variant._data._mem) ) );
  741. } break;
  742. case POOL_INT_ARRAY: {
  743. memnew_placement( _data._mem, PoolVector<int> ( *reinterpret_cast<const PoolVector<int>*>(p_variant._data._mem) ) );
  744. } break;
  745. case POOL_REAL_ARRAY: {
  746. memnew_placement( _data._mem, PoolVector<real_t> ( *reinterpret_cast<const PoolVector<real_t>*>(p_variant._data._mem) ) );
  747. } break;
  748. case POOL_STRING_ARRAY: {
  749. memnew_placement( _data._mem, PoolVector<String> ( *reinterpret_cast<const PoolVector<String>*>(p_variant._data._mem) ) );
  750. } break;
  751. case POOL_VECTOR2_ARRAY: {
  752. memnew_placement( _data._mem, PoolVector<Vector2> ( *reinterpret_cast<const PoolVector<Vector2>*>(p_variant._data._mem) ) );
  753. } break;
  754. case POOL_VECTOR3_ARRAY: {
  755. memnew_placement( _data._mem, PoolVector<Vector3> ( *reinterpret_cast<const PoolVector<Vector3>*>(p_variant._data._mem) ) );
  756. } break;
  757. case POOL_COLOR_ARRAY: {
  758. memnew_placement( _data._mem, PoolVector<Color> ( *reinterpret_cast<const PoolVector<Color>*>(p_variant._data._mem) ) );
  759. } break;
  760. default: {}
  761. }
  762. }
  763. void Variant::zero() {
  764. switch(type) {
  765. case NIL: break;
  766. case BOOL: this->_data._bool = false; break;
  767. case INT: this->_data._int = 0; break;
  768. case REAL: this->_data._real = 0; break;
  769. case VECTOR2: *reinterpret_cast<Vector2*>(this->_data._mem) = Vector2(); break;
  770. case RECT2: *reinterpret_cast<Rect2*>(this->_data._mem) = Rect2(); break;
  771. case VECTOR3: *reinterpret_cast<Vector3*>(this->_data._mem) = Vector3(); break;
  772. case PLANE: *reinterpret_cast<Plane*>(this->_data._mem) = Plane(); break;
  773. case QUAT: *reinterpret_cast<Quat*>(this->_data._mem) = Quat(); break;
  774. case COLOR: *reinterpret_cast<Color*>(this->_data._mem) = Color(); break;
  775. default: this->clear(); break;
  776. }
  777. }
  778. void Variant::clear() {
  779. switch(type) {
  780. case STRING: {
  781. reinterpret_cast<String*>(_data._mem)->~String();
  782. } break;
  783. /*
  784. // no point, they don't allocate memory
  785. VECTOR3,
  786. PLANE,
  787. QUAT,
  788. COLOR,
  789. VECTOR2,
  790. RECT2
  791. */
  792. case TRANSFORM2D: {
  793. memdelete( _data._transform2d );
  794. } break;
  795. case RECT3: {
  796. memdelete( _data._rect3 );
  797. } break;
  798. case BASIS: {
  799. memdelete( _data._basis );
  800. } break;
  801. case TRANSFORM: {
  802. memdelete( _data._transform );
  803. } break;
  804. // misc types
  805. case IMAGE: {
  806. memdelete( _data._image );
  807. } break;
  808. case NODE_PATH: {
  809. reinterpret_cast<NodePath*>(_data._mem)->~NodePath();
  810. } break;
  811. case OBJECT: {
  812. _get_obj().obj=NULL;
  813. _get_obj().ref.unref();
  814. } break;
  815. case _RID: {
  816. // not much need probably
  817. reinterpret_cast<RID*>(_data._mem)->~RID();
  818. } break;
  819. case DICTIONARY: {
  820. reinterpret_cast<Dictionary*>(_data._mem)->~Dictionary();
  821. } break;
  822. case ARRAY: {
  823. reinterpret_cast<Array*>(_data._mem)->~Array();
  824. } break;
  825. case INPUT_EVENT: {
  826. memdelete( _data._input_event );
  827. } break;
  828. // arrays
  829. case POOL_BYTE_ARRAY: {
  830. reinterpret_cast< PoolVector<uint8_t>* >(_data._mem)->~PoolVector<uint8_t>();
  831. } break;
  832. case POOL_INT_ARRAY: {
  833. reinterpret_cast< PoolVector<int>* >(_data._mem)->~PoolVector<int>();
  834. } break;
  835. case POOL_REAL_ARRAY: {
  836. reinterpret_cast< PoolVector<real_t>* >(_data._mem)->~PoolVector<real_t>();
  837. } break;
  838. case POOL_STRING_ARRAY: {
  839. reinterpret_cast< PoolVector<String>* >(_data._mem)->~PoolVector<String>();
  840. } break;
  841. case POOL_VECTOR2_ARRAY: {
  842. reinterpret_cast< PoolVector<Vector2>* >(_data._mem)->~PoolVector<Vector2>();
  843. } break;
  844. case POOL_VECTOR3_ARRAY: {
  845. reinterpret_cast< PoolVector<Vector3>* >(_data._mem)->~PoolVector<Vector3>();
  846. } break;
  847. case POOL_COLOR_ARRAY: {
  848. reinterpret_cast< PoolVector<Color>* >(_data._mem)->~PoolVector<Color>();
  849. } break;
  850. default: {} /* not needed */
  851. }
  852. type=NIL;
  853. }
  854. Variant::operator signed int() const {
  855. switch( type ) {
  856. case NIL: return 0;
  857. case BOOL: return _data._bool ? 1 : 0;
  858. case INT: return _data._int;
  859. case REAL: return _data._real;
  860. case STRING: return operator String().to_int();
  861. default: {
  862. return 0;
  863. }
  864. }
  865. return 0;
  866. }
  867. Variant::operator unsigned int() const {
  868. switch( type ) {
  869. case NIL: return 0;
  870. case BOOL: return _data._bool ? 1 : 0;
  871. case INT: return _data._int;
  872. case REAL: return _data._real;
  873. case STRING: return operator String().to_int();
  874. default: {
  875. return 0;
  876. }
  877. }
  878. return 0;
  879. }
  880. Variant::operator int64_t() const {
  881. switch( type ) {
  882. case NIL: return 0;
  883. case BOOL: return _data._bool ? 1 : 0;
  884. case INT: return _data._int;
  885. case REAL: return _data._real;
  886. case STRING: return operator String().to_int();
  887. default: {
  888. return 0;
  889. }
  890. }
  891. return 0;
  892. }
  893. /*
  894. Variant::operator long unsigned int() const {
  895. switch( type ) {
  896. case NIL: return 0;
  897. case BOOL: return _data._bool ? 1 : 0;
  898. case INT: return _data._int;
  899. case REAL: return _data._real;
  900. case STRING: return operator String().to_int();
  901. default: {
  902. return 0;
  903. }
  904. }
  905. return 0;
  906. };
  907. */
  908. Variant::operator uint64_t() const {
  909. switch( type ) {
  910. case NIL: return 0;
  911. case BOOL: return _data._bool ? 1 : 0;
  912. case INT: return _data._int;
  913. case REAL: return _data._real;
  914. case STRING: return operator String().to_int();
  915. default: {
  916. return 0;
  917. }
  918. }
  919. return 0;
  920. }
  921. #ifdef NEED_LONG_INT
  922. Variant::operator signed long() const {
  923. switch( type ) {
  924. case NIL: return 0;
  925. case BOOL: return _data._bool ? 1 : 0;
  926. case INT: return _data._int;
  927. case REAL: return _data._real;
  928. case STRING: return operator String().to_int();
  929. default: {
  930. return 0;
  931. }
  932. }
  933. return 0;
  934. };
  935. Variant::operator unsigned long() const {
  936. switch( type ) {
  937. case NIL: return 0;
  938. case BOOL: return _data._bool ? 1 : 0;
  939. case INT: return _data._int;
  940. case REAL: return _data._real;
  941. case STRING: return operator String().to_int();
  942. default: {
  943. return 0;
  944. }
  945. }
  946. return 0;
  947. };
  948. #endif
  949. Variant::operator signed short() const {
  950. switch( type ) {
  951. case NIL: return 0;
  952. case BOOL: return _data._bool ? 1 : 0;
  953. case INT: return _data._int;
  954. case REAL: return _data._real;
  955. case STRING: return operator String().to_int();
  956. default: {
  957. return 0;
  958. }
  959. }
  960. return 0;
  961. }
  962. Variant::operator unsigned short() const {
  963. switch( type ) {
  964. case NIL: return 0;
  965. case BOOL: return _data._bool ? 1 : 0;
  966. case INT: return _data._int;
  967. case REAL: return _data._real;
  968. case STRING: return operator String().to_int();
  969. default: {
  970. return 0;
  971. }
  972. }
  973. return 0;
  974. }
  975. Variant::operator signed char() const {
  976. switch( type ) {
  977. case NIL: return 0;
  978. case BOOL: return _data._bool ? 1 : 0;
  979. case INT: return _data._int;
  980. case REAL: return _data._real;
  981. case STRING: return operator String().to_int();
  982. default: {
  983. return 0;
  984. }
  985. }
  986. return 0;
  987. }
  988. Variant::operator unsigned char() const {
  989. switch( type ) {
  990. case NIL: return 0;
  991. case BOOL: return _data._bool ? 1 : 0;
  992. case INT: return _data._int;
  993. case REAL: return _data._real;
  994. case STRING: return operator String().to_int();
  995. default: {
  996. return 0;
  997. }
  998. }
  999. return 0;
  1000. }
  1001. Variant::operator CharType() const {
  1002. return operator unsigned int();
  1003. }
  1004. Variant::operator float() const {
  1005. switch( type ) {
  1006. case NIL: return 0;
  1007. case BOOL: return _data._bool ? 1.0 : 0.0;
  1008. case INT: return (float)_data._int;
  1009. case REAL: return _data._real;
  1010. case STRING: return operator String().to_double();
  1011. default: {
  1012. return 0;
  1013. }
  1014. }
  1015. return 0;
  1016. }
  1017. Variant::operator double() const {
  1018. switch( type ) {
  1019. case NIL: return 0;
  1020. case BOOL: return _data._bool ? 1.0 : 0.0;
  1021. case INT: return (double)_data._int;
  1022. case REAL: return _data._real;
  1023. case STRING: return operator String().to_double();
  1024. default: {
  1025. return 0;
  1026. }
  1027. }
  1028. return true;
  1029. }
  1030. Variant::operator StringName() const {
  1031. if (type==NODE_PATH) {
  1032. return reinterpret_cast<const NodePath*>(_data._mem)->get_sname();
  1033. }
  1034. return StringName(operator String());
  1035. }
  1036. struct _VariantStrPair {
  1037. String key;
  1038. String value;
  1039. bool operator<(const _VariantStrPair& p) const {
  1040. return key < p.key;
  1041. }
  1042. };
  1043. Variant::operator String() const {
  1044. switch( type ) {
  1045. case NIL: return "Null";
  1046. case BOOL: return _data._bool ? "True" : "False";
  1047. case INT: return itos(_data._int);
  1048. case REAL: return rtos(_data._real);
  1049. case STRING: return *reinterpret_cast<const String*>(_data._mem);
  1050. case VECTOR2: return "("+operator Vector2()+")";
  1051. case RECT2: return "("+operator Rect2()+")";
  1052. case TRANSFORM2D: {
  1053. Transform2D mat32 = operator Transform2D();
  1054. return "("+Variant(mat32.elements[0]).operator String()+", "+Variant(mat32.elements[1]).operator String()+", "+Variant(mat32.elements[2]).operator String()+")";
  1055. } break;
  1056. case VECTOR3: return "("+operator Vector3()+")";
  1057. case PLANE: return operator Plane();
  1058. //case QUAT:
  1059. case RECT3: return operator Rect3();
  1060. case QUAT: return "("+operator Quat()+")";
  1061. case BASIS: {
  1062. Basis mat3 = operator Basis();
  1063. String mtx("(");
  1064. for (int i=0;i<3;i++) {
  1065. if (i!=0)
  1066. mtx+=", ";
  1067. mtx+="(";
  1068. for (int j=0;j<3;j++) {
  1069. if (j!=0)
  1070. mtx+=", ";
  1071. mtx+=Variant( mat3.elements[i][j] ).operator String();
  1072. }
  1073. mtx+=")";
  1074. }
  1075. return mtx+")";
  1076. } break;
  1077. case TRANSFORM: return operator Transform();
  1078. case NODE_PATH: return operator NodePath();
  1079. case INPUT_EVENT: return operator InputEvent();
  1080. case COLOR: return String::num( operator Color().r)+","+String::num( operator Color().g)+","+String::num( operator Color().b)+","+String::num( operator Color().a) ;
  1081. case DICTIONARY: {
  1082. const Dictionary &d =*reinterpret_cast<const Dictionary*>(_data._mem);
  1083. //const String *K=NULL;
  1084. String str;
  1085. List<Variant> keys;
  1086. d.get_key_list(&keys);
  1087. Vector<_VariantStrPair> pairs;
  1088. for(List<Variant>::Element *E=keys.front();E;E=E->next()) {
  1089. _VariantStrPair sp;
  1090. sp.key=String(E->get());
  1091. sp.value=d[E->get()];
  1092. pairs.push_back(sp);
  1093. }
  1094. pairs.sort();
  1095. for(int i=0;i<pairs.size();i++) {
  1096. if (i>0)
  1097. str+=", ";
  1098. str+="("+pairs[i].key+":"+pairs[i].value+")";
  1099. }
  1100. return str;
  1101. } break;
  1102. case POOL_VECTOR2_ARRAY: {
  1103. PoolVector<Vector2> vec = operator PoolVector<Vector2>();
  1104. String str("[");
  1105. for(int i=0;i<vec.size();i++) {
  1106. if (i>0)
  1107. str+=", ";
  1108. str=str+Variant( vec[i] );
  1109. }
  1110. str += "]";
  1111. return str;
  1112. } break;
  1113. case POOL_VECTOR3_ARRAY: {
  1114. PoolVector<Vector3> vec = operator PoolVector<Vector3>();
  1115. String str("[");
  1116. for(int i=0;i<vec.size();i++) {
  1117. if (i>0)
  1118. str+=", ";
  1119. str=str+Variant( vec[i] );
  1120. }
  1121. str += "]";
  1122. return str;
  1123. } break;
  1124. case POOL_STRING_ARRAY: {
  1125. PoolVector<String> vec = operator PoolVector<String>();
  1126. String str("[");
  1127. for(int i=0;i<vec.size();i++) {
  1128. if (i>0)
  1129. str+=", ";
  1130. str=str+vec[i];
  1131. }
  1132. str += "]";
  1133. return str;
  1134. } break;
  1135. case POOL_INT_ARRAY: {
  1136. PoolVector<int> vec = operator PoolVector<int>();
  1137. String str("[");
  1138. for(int i=0;i<vec.size();i++) {
  1139. if (i>0)
  1140. str+=", ";
  1141. str=str+itos(vec[i]);
  1142. }
  1143. str += "]";
  1144. return str;
  1145. } break;
  1146. case POOL_REAL_ARRAY: {
  1147. PoolVector<real_t> vec = operator PoolVector<real_t>();
  1148. String str("[");
  1149. for(int i=0;i<vec.size();i++) {
  1150. if (i>0)
  1151. str+=", ";
  1152. str=str+rtos(vec[i]);
  1153. }
  1154. str += "]";
  1155. return str;
  1156. } break;
  1157. case ARRAY: {
  1158. Array arr = operator Array();
  1159. String str("[");
  1160. for (int i=0; i<arr.size(); i++) {
  1161. if (i)
  1162. str+=", ";
  1163. str += String(arr[i]);
  1164. };
  1165. str += "]";
  1166. return str;
  1167. } break;
  1168. case OBJECT: {
  1169. if (_get_obj().obj) {
  1170. #ifdef DEBUG_ENABLED
  1171. if (ScriptDebugger::get_singleton() && _get_obj().ref.is_null()) {
  1172. //only if debugging!
  1173. if (!ObjectDB::instance_validate(_get_obj().obj)) {
  1174. return "[Deleted Object]";
  1175. };
  1176. };
  1177. #endif
  1178. return "["+_get_obj().obj->get_class()+":"+itos(_get_obj().obj->get_instance_ID())+"]";
  1179. } else
  1180. return "[Object:null]";
  1181. } break;
  1182. default: {
  1183. return "["+get_type_name(type)+"]";
  1184. }
  1185. }
  1186. return "";
  1187. }
  1188. Variant::operator Vector2() const {
  1189. if (type==VECTOR2)
  1190. return *reinterpret_cast<const Vector2*>(_data._mem);
  1191. else if (type==VECTOR3)
  1192. return Vector2(reinterpret_cast<const Vector3*>(_data._mem)->x,reinterpret_cast<const Vector3*>(_data._mem)->y);
  1193. else
  1194. return Vector2();
  1195. }
  1196. Variant::operator Rect2() const {
  1197. if (type==RECT2)
  1198. return *reinterpret_cast<const Rect2*>(_data._mem);
  1199. else
  1200. return Rect2();
  1201. }
  1202. Variant::operator Vector3() const {
  1203. if (type==VECTOR3)
  1204. return *reinterpret_cast<const Vector3*>(_data._mem);
  1205. else
  1206. return Vector3();
  1207. }
  1208. Variant::operator Plane() const {
  1209. if (type==PLANE)
  1210. return *reinterpret_cast<const Plane*>(_data._mem);
  1211. else
  1212. return Plane();
  1213. }
  1214. Variant::operator Rect3() const {
  1215. if (type==RECT3)
  1216. return *_data._rect3;
  1217. else
  1218. return Rect3();
  1219. }
  1220. Variant::operator Basis() const {
  1221. if (type==BASIS)
  1222. return *_data._basis;
  1223. else if (type==QUAT)
  1224. return *reinterpret_cast<const Quat*>(_data._mem);
  1225. else if (type==TRANSFORM)
  1226. return _data._transform->basis;
  1227. else
  1228. return Basis();
  1229. }
  1230. Variant::operator Quat() const {
  1231. if (type==QUAT)
  1232. return *reinterpret_cast<const Quat*>(_data._mem);
  1233. else if (type==BASIS)
  1234. return *_data._basis;
  1235. else if (type==TRANSFORM)
  1236. return _data._transform->basis;
  1237. else
  1238. return Quat();
  1239. }
  1240. Variant::operator Transform() const {
  1241. if (type==TRANSFORM)
  1242. return *_data._transform;
  1243. else if (type==BASIS)
  1244. return Transform(*_data._basis,Vector3());
  1245. else if (type==QUAT)
  1246. return Transform(Basis(*reinterpret_cast<const Quat*>(_data._mem)),Vector3());
  1247. else
  1248. return Transform();
  1249. }
  1250. Variant::operator Transform2D() const {
  1251. if (type==TRANSFORM2D) {
  1252. return *_data._transform2d;
  1253. } else if (type==TRANSFORM) {
  1254. const Transform& t = *_data._transform;
  1255. Transform2D m;
  1256. m.elements[0][0]=t.basis.elements[0][0];
  1257. m.elements[0][1]=t.basis.elements[1][0];
  1258. m.elements[1][0]=t.basis.elements[0][1];
  1259. m.elements[1][1]=t.basis.elements[1][1];
  1260. m.elements[2][0]=t.origin[0];
  1261. m.elements[2][1]=t.origin[1];
  1262. return m;
  1263. } else
  1264. return Transform2D();
  1265. }
  1266. Variant::operator Color() const {
  1267. if (type==COLOR)
  1268. return *reinterpret_cast<const Color*>(_data._mem);
  1269. else if (type==STRING)
  1270. return Color::html( operator String() );
  1271. else if (type==INT)
  1272. return Color::hex( operator int() );
  1273. else
  1274. return Color();
  1275. }
  1276. Variant::operator Image() const {
  1277. if (type==IMAGE)
  1278. return *_data._image;
  1279. else
  1280. return Image();
  1281. }
  1282. Variant::operator NodePath() const {
  1283. if (type==NODE_PATH)
  1284. return *reinterpret_cast<const NodePath*>(_data._mem);
  1285. else if (type==STRING)
  1286. return NodePath(operator String());
  1287. else
  1288. return NodePath();
  1289. }
  1290. Variant::operator RefPtr() const {
  1291. if (type==OBJECT)
  1292. return _get_obj().ref;
  1293. else
  1294. return RefPtr();
  1295. }
  1296. Variant::operator RID() const {
  1297. if (type==_RID)
  1298. return *reinterpret_cast<const RID*>(_data._mem);
  1299. else if (type==OBJECT && !_get_obj().ref.is_null()) {
  1300. return _get_obj().ref.get_rid();
  1301. } else if (type==OBJECT && _get_obj().obj) {
  1302. Variant::CallError ce;
  1303. Variant ret = _get_obj().obj->call(CoreStringNames::get_singleton()->get_rid,NULL,0,ce);
  1304. if (ce.error==Variant::CallError::CALL_OK && ret.get_type()==Variant::_RID) {
  1305. return ret;
  1306. }
  1307. return RID();
  1308. } else {
  1309. return RID();
  1310. }
  1311. }
  1312. Variant::operator Object*() const {
  1313. if (type==OBJECT)
  1314. return _get_obj().obj;
  1315. else
  1316. return NULL;
  1317. }
  1318. Variant::operator Node*() const {
  1319. if (type==OBJECT)
  1320. return _get_obj().obj?_get_obj().obj->cast_to<Node>():NULL;
  1321. else
  1322. return NULL;
  1323. }
  1324. Variant::operator Control*() const {
  1325. if (type==OBJECT)
  1326. return _get_obj().obj?_get_obj().obj->cast_to<Control>():NULL;
  1327. else
  1328. return NULL;
  1329. }
  1330. Variant::operator InputEvent() const {
  1331. if (type==INPUT_EVENT)
  1332. return *reinterpret_cast<const InputEvent*>(_data._input_event);
  1333. else
  1334. return InputEvent();
  1335. }
  1336. Variant::operator Dictionary() const {
  1337. if (type==DICTIONARY)
  1338. return *reinterpret_cast<const Dictionary*>(_data._mem);
  1339. else
  1340. return Dictionary();
  1341. }
  1342. template<class DA,class SA>
  1343. inline DA _convert_array(const SA& p_array) {
  1344. DA da;
  1345. da.resize(p_array.size());
  1346. for(int i=0;i<p_array.size();i++) {
  1347. da.set( i, Variant(p_array.get(i)) );
  1348. }
  1349. return da;
  1350. }
  1351. template<class DA>
  1352. inline DA _convert_array_from_variant(const Variant& p_variant) {
  1353. switch(p_variant.get_type()) {
  1354. case Variant::ARRAY: { return _convert_array<DA,Array >( p_variant.operator Array () ); }
  1355. case Variant::POOL_BYTE_ARRAY: { return _convert_array<DA,PoolVector<uint8_t> >( p_variant.operator PoolVector<uint8_t> () ); }
  1356. case Variant::POOL_INT_ARRAY: { return _convert_array<DA,PoolVector<int> >( p_variant.operator PoolVector<int> () ); }
  1357. case Variant::POOL_REAL_ARRAY: { return _convert_array<DA,PoolVector<real_t> >( p_variant.operator PoolVector<real_t> () ); }
  1358. case Variant::POOL_STRING_ARRAY: { return _convert_array<DA,PoolVector<String> >( p_variant.operator PoolVector<String> () ); }
  1359. case Variant::POOL_VECTOR2_ARRAY: { return _convert_array<DA,PoolVector<Vector2> >( p_variant.operator PoolVector<Vector2> () ); }
  1360. case Variant::POOL_VECTOR3_ARRAY: { return _convert_array<DA,PoolVector<Vector3> >( p_variant.operator PoolVector<Vector3> () ); }
  1361. case Variant::POOL_COLOR_ARRAY: { return _convert_array<DA,PoolVector<Color> >( p_variant.operator PoolVector<Color>() ); }
  1362. default: { return DA(); }
  1363. }
  1364. return DA();
  1365. }
  1366. Variant::operator Array() const {
  1367. if (type==ARRAY)
  1368. return *reinterpret_cast<const Array*>(_data._mem);
  1369. else
  1370. return _convert_array_from_variant<Array >(*this);
  1371. }
  1372. Variant::operator PoolVector<uint8_t>() const {
  1373. if (type==POOL_BYTE_ARRAY)
  1374. return *reinterpret_cast<const PoolVector<uint8_t>* >(_data._mem);
  1375. else
  1376. return _convert_array_from_variant<PoolVector<uint8_t> >(*this);
  1377. }
  1378. Variant::operator PoolVector<int>() const {
  1379. if (type==POOL_INT_ARRAY)
  1380. return *reinterpret_cast<const PoolVector<int>* >(_data._mem);
  1381. else
  1382. return _convert_array_from_variant<PoolVector<int> >(*this);
  1383. }
  1384. Variant::operator PoolVector<real_t>() const {
  1385. if (type==POOL_REAL_ARRAY)
  1386. return *reinterpret_cast<const PoolVector<real_t>* >(_data._mem);
  1387. else
  1388. return _convert_array_from_variant<PoolVector<real_t> >(*this);
  1389. }
  1390. Variant::operator PoolVector<String>() const {
  1391. if (type==POOL_STRING_ARRAY)
  1392. return *reinterpret_cast<const PoolVector<String>* >(_data._mem);
  1393. else
  1394. return _convert_array_from_variant<PoolVector<String> >(*this);
  1395. }
  1396. Variant::operator PoolVector<Vector3>() const {
  1397. if (type==POOL_VECTOR3_ARRAY)
  1398. return *reinterpret_cast<const PoolVector<Vector3>* >(_data._mem);
  1399. else
  1400. return _convert_array_from_variant<PoolVector<Vector3> >(*this);
  1401. }
  1402. Variant::operator PoolVector<Vector2>() const {
  1403. if (type==POOL_VECTOR2_ARRAY)
  1404. return *reinterpret_cast<const PoolVector<Vector2>* >(_data._mem);
  1405. else
  1406. return _convert_array_from_variant<PoolVector<Vector2> >(*this);
  1407. }
  1408. Variant::operator PoolVector<Color>() const {
  1409. if (type==POOL_COLOR_ARRAY)
  1410. return *reinterpret_cast<const PoolVector<Color>* >(_data._mem);
  1411. else
  1412. return _convert_array_from_variant<PoolVector<Color> >(*this);
  1413. }
  1414. /* helpers */
  1415. Variant::operator Vector<RID>() const {
  1416. Array va= operator Array();
  1417. Vector<RID> rids;
  1418. rids.resize(va.size());
  1419. for(int i=0;i<rids.size();i++)
  1420. rids[i]=va[i];
  1421. return rids;
  1422. }
  1423. Variant::operator Vector<Vector2>() const {
  1424. PoolVector<Vector2> from=operator PoolVector<Vector2>();
  1425. Vector<Vector2> to;
  1426. int len=from.size();
  1427. if (len==0)
  1428. return Vector<Vector2>();
  1429. to.resize(len);
  1430. PoolVector<Vector2>::Read r = from.read();
  1431. Vector2 *w = &to[0];
  1432. for (int i=0;i<len;i++) {
  1433. w[i]=r[i];
  1434. }
  1435. return to;
  1436. }
  1437. Variant::operator PoolVector<Plane>() const {
  1438. Array va= operator Array();
  1439. PoolVector<Plane> planes;
  1440. int va_size=va.size();
  1441. if (va_size==0)
  1442. return planes;
  1443. planes.resize(va_size);
  1444. PoolVector<Plane>::Write w = planes.write();
  1445. for(int i=0;i<va_size;i++)
  1446. w[i]=va[i];
  1447. return planes;
  1448. }
  1449. Variant::operator PoolVector<Face3>() const {
  1450. PoolVector<Vector3> va= operator PoolVector<Vector3>();
  1451. PoolVector<Face3> faces;
  1452. int va_size=va.size();
  1453. if (va_size==0)
  1454. return faces;
  1455. faces.resize(va_size/3);
  1456. PoolVector<Face3>::Write w = faces.write();
  1457. PoolVector<Vector3>::Read r = va.read();
  1458. for(int i=0;i<va_size;i++)
  1459. w[i/3].vertex[i%3]=r[i];
  1460. return faces;
  1461. }
  1462. Variant::operator Vector<Plane>() const {
  1463. Array va= operator Array();
  1464. Vector<Plane> planes;
  1465. int va_size=va.size();
  1466. if (va_size==0)
  1467. return planes;
  1468. planes.resize(va_size);
  1469. for(int i=0;i<va_size;i++)
  1470. planes[i]=va[i];
  1471. return planes;
  1472. }
  1473. Variant::operator Vector<Variant>() const {
  1474. Array from=operator Array();
  1475. Vector<Variant> to;
  1476. int len=from.size();
  1477. to.resize(len);
  1478. for (int i=0;i<len;i++) {
  1479. to[i]=from[i];
  1480. }
  1481. return to;
  1482. }
  1483. Variant::operator Vector<uint8_t>() const {
  1484. PoolVector<uint8_t> from=operator PoolVector<uint8_t>();
  1485. Vector<uint8_t> to;
  1486. int len=from.size();
  1487. to.resize(len);
  1488. for (int i=0;i<len;i++) {
  1489. to[i]=from[i];
  1490. }
  1491. return to;
  1492. }
  1493. Variant::operator Vector<int>() const {
  1494. PoolVector<int> from=operator PoolVector<int>();
  1495. Vector<int> to;
  1496. int len=from.size();
  1497. to.resize(len);
  1498. for (int i=0;i<len;i++) {
  1499. to[i]=from[i];
  1500. }
  1501. return to;
  1502. }
  1503. Variant::operator Vector<real_t>() const {
  1504. PoolVector<real_t> from=operator PoolVector<real_t>();
  1505. Vector<real_t> to;
  1506. int len=from.size();
  1507. to.resize(len);
  1508. for (int i=0;i<len;i++) {
  1509. to[i]=from[i];
  1510. }
  1511. return to;
  1512. }
  1513. Variant::operator Vector<String>() const {
  1514. PoolVector<String> from=operator PoolVector<String>();
  1515. Vector<String> to;
  1516. int len=from.size();
  1517. to.resize(len);
  1518. for (int i=0;i<len;i++) {
  1519. to[i]=from[i];
  1520. }
  1521. return to;
  1522. }
  1523. Variant::operator Vector<Vector3>() const {
  1524. PoolVector<Vector3> from=operator PoolVector<Vector3>();
  1525. Vector<Vector3> to;
  1526. int len=from.size();
  1527. if (len==0)
  1528. return Vector<Vector3>();
  1529. to.resize(len);
  1530. PoolVector<Vector3>::Read r = from.read();
  1531. Vector3 *w = &to[0];
  1532. for (int i=0;i<len;i++) {
  1533. w[i]=r[i];
  1534. }
  1535. return to;
  1536. }
  1537. Variant::operator Vector<Color>() const {
  1538. PoolVector<Color> from=operator PoolVector<Color>();
  1539. Vector<Color> to;
  1540. int len=from.size();
  1541. if (len==0)
  1542. return Vector<Color>();
  1543. to.resize(len);
  1544. PoolVector<Color>::Read r = from.read();
  1545. Color *w = &to[0];
  1546. for (int i=0;i<len;i++) {
  1547. w[i]=r[i];
  1548. }
  1549. return to;
  1550. }
  1551. Variant::operator Margin() const {
  1552. return (Margin)operator int();
  1553. }
  1554. Variant::operator Orientation() const {
  1555. return (Orientation)operator int();
  1556. }
  1557. Variant::operator IP_Address() const {
  1558. if (type==POOL_REAL_ARRAY || type==POOL_INT_ARRAY || type==POOL_BYTE_ARRAY) {
  1559. PoolVector<int> addr=operator PoolVector<int>();
  1560. if (addr.size()==4) {
  1561. return IP_Address(addr.get(0),addr.get(1),addr.get(2),addr.get(3));
  1562. }
  1563. }
  1564. return IP_Address( operator String() );
  1565. }
  1566. Variant::operator PowerState() const
  1567. {
  1568. return (PowerState) operator int();
  1569. }
  1570. Variant::Variant(bool p_bool) {
  1571. type=BOOL;
  1572. _data._bool=p_bool;
  1573. }
  1574. /*
  1575. Variant::Variant(long unsigned int p_long) {
  1576. type=INT;
  1577. _data._int=p_long;
  1578. };
  1579. */
  1580. Variant::Variant(signed int p_int) {
  1581. type=INT;
  1582. _data._int=p_int;
  1583. }
  1584. Variant::Variant(unsigned int p_int) {
  1585. type=INT;
  1586. _data._int=p_int;
  1587. }
  1588. #ifdef NEED_LONG_INT
  1589. Variant::Variant(signed long p_int) {
  1590. type=INT;
  1591. _data._int=p_int;
  1592. }
  1593. Variant::Variant(unsigned long p_int) {
  1594. type=INT;
  1595. _data._int=p_int;
  1596. }
  1597. #endif
  1598. Variant::Variant(int64_t p_int) {
  1599. type=INT;
  1600. _data._int=p_int;
  1601. }
  1602. Variant::Variant(uint64_t p_int) {
  1603. type=INT;
  1604. _data._int=p_int;
  1605. }
  1606. Variant::Variant(signed short p_short) {
  1607. type=INT;
  1608. _data._int=p_short;
  1609. }
  1610. Variant::Variant(unsigned short p_short) {
  1611. type=INT;
  1612. _data._int=p_short;
  1613. }
  1614. Variant::Variant(signed char p_char) {
  1615. type=INT;
  1616. _data._int=p_char;
  1617. }
  1618. Variant::Variant(unsigned char p_char) {
  1619. type=INT;
  1620. _data._int=p_char;
  1621. }
  1622. Variant::Variant(float p_float) {
  1623. type=REAL;
  1624. _data._real=p_float;
  1625. }
  1626. Variant::Variant(double p_double) {
  1627. type=REAL;
  1628. _data._real=p_double;
  1629. }
  1630. Variant::Variant(const StringName& p_string) {
  1631. type=STRING;
  1632. memnew_placement( _data._mem, String( p_string.operator String() ) );
  1633. }
  1634. Variant::Variant(const String& p_string) {
  1635. type=STRING;
  1636. memnew_placement( _data._mem, String( p_string ) );
  1637. }
  1638. Variant::Variant(const char * const p_cstring) {
  1639. type=STRING;
  1640. memnew_placement( _data._mem, String( (const char*)p_cstring ) );
  1641. }
  1642. Variant::Variant(const CharType * p_wstring) {
  1643. type=STRING;
  1644. memnew_placement( _data._mem, String( p_wstring ) );
  1645. }
  1646. Variant::Variant(const Vector3& p_vector3) {
  1647. type=VECTOR3;
  1648. memnew_placement( _data._mem, Vector3( p_vector3 ) );
  1649. }
  1650. Variant::Variant(const Vector2& p_vector2) {
  1651. type=VECTOR2;
  1652. memnew_placement( _data._mem, Vector2( p_vector2 ) );
  1653. }
  1654. Variant::Variant(const Rect2& p_rect2) {
  1655. type=RECT2;
  1656. memnew_placement( _data._mem, Rect2( p_rect2 ) );
  1657. }
  1658. Variant::Variant(const Plane& p_plane) {
  1659. type=PLANE;
  1660. memnew_placement( _data._mem, Plane( p_plane ) );
  1661. }
  1662. Variant::Variant(const Rect3& p_aabb) {
  1663. type=RECT3;
  1664. _data._rect3 = memnew( Rect3( p_aabb ) );
  1665. }
  1666. Variant::Variant(const Basis& p_matrix) {
  1667. type=BASIS;
  1668. _data._basis= memnew( Basis( p_matrix ) );
  1669. }
  1670. Variant::Variant(const Quat& p_quat) {
  1671. type=QUAT;
  1672. memnew_placement( _data._mem, Quat( p_quat ) );
  1673. }
  1674. Variant::Variant(const Transform& p_transform) {
  1675. type=TRANSFORM;
  1676. _data._transform = memnew( Transform( p_transform ) );
  1677. }
  1678. Variant::Variant(const Transform2D& p_transform) {
  1679. type=TRANSFORM2D;
  1680. _data._transform2d = memnew( Transform2D( p_transform ) );
  1681. }
  1682. Variant::Variant(const Color& p_color) {
  1683. type=COLOR;
  1684. memnew_placement( _data._mem, Color(p_color) );
  1685. }
  1686. Variant::Variant(const Image& p_image) {
  1687. type=IMAGE;
  1688. _data._image=memnew( Image(p_image) );
  1689. }
  1690. Variant::Variant(const NodePath& p_node_path) {
  1691. type=NODE_PATH;
  1692. memnew_placement( _data._mem, NodePath(p_node_path) );
  1693. }
  1694. Variant::Variant(const InputEvent& p_input_event) {
  1695. type=INPUT_EVENT;
  1696. _data._input_event = memnew( InputEvent(p_input_event) );
  1697. }
  1698. Variant::Variant(const RefPtr& p_resource) {
  1699. type=OBJECT;
  1700. memnew_placement( _data._mem, ObjData );
  1701. REF ref = p_resource;
  1702. _get_obj().obj=ref.ptr();
  1703. _get_obj().ref=p_resource;
  1704. }
  1705. Variant::Variant(const RID& p_rid) {
  1706. type=_RID;
  1707. memnew_placement( _data._mem, RID(p_rid) );
  1708. }
  1709. Variant::Variant(const Object* p_object) {
  1710. type=OBJECT;
  1711. memnew_placement( _data._mem, ObjData );
  1712. _get_obj().obj=const_cast<Object*>(p_object);
  1713. }
  1714. Variant::Variant(const Dictionary& p_dictionary) {
  1715. type=DICTIONARY;
  1716. memnew_placement( _data._mem, (Dictionary)( p_dictionary) );
  1717. }
  1718. Variant::Variant(const Array& p_array) {
  1719. type=ARRAY;
  1720. memnew_placement( _data._mem, Array(p_array) );
  1721. }
  1722. Variant::Variant(const PoolVector<Plane>& p_array) {
  1723. type=ARRAY;
  1724. Array *plane_array=memnew_placement( _data._mem, Array );
  1725. plane_array->resize( p_array.size() );
  1726. for (int i=0;i<p_array.size();i++) {
  1727. plane_array->operator [](i)=Variant(p_array[i]);
  1728. }
  1729. }
  1730. Variant::Variant(const Vector<Plane>& p_array) {
  1731. type=ARRAY;
  1732. Array *plane_array=memnew_placement( _data._mem, Array );
  1733. plane_array->resize( p_array.size() );
  1734. for (int i=0;i<p_array.size();i++) {
  1735. plane_array->operator [](i)=Variant(p_array[i]);
  1736. }
  1737. }
  1738. Variant::Variant(const Vector<RID>& p_array) {
  1739. type=ARRAY;
  1740. Array *rid_array=memnew_placement( _data._mem, Array );
  1741. rid_array->resize( p_array.size() );
  1742. for (int i=0;i<p_array.size();i++) {
  1743. rid_array->set(i,Variant(p_array[i]));
  1744. }
  1745. }
  1746. Variant::Variant(const Vector<Vector2>& p_array) {
  1747. type=NIL;
  1748. PoolVector<Vector2> v;
  1749. int len=p_array.size();
  1750. if (len>0) {
  1751. v.resize(len);
  1752. PoolVector<Vector2>::Write w = v.write();
  1753. const Vector2 *r = p_array.ptr();
  1754. for (int i=0;i<len;i++)
  1755. w[i]=r[i];
  1756. }
  1757. *this=v;
  1758. }
  1759. Variant::Variant(const PoolVector<uint8_t>& p_raw_array) {
  1760. type=POOL_BYTE_ARRAY;
  1761. memnew_placement( _data._mem, PoolVector<uint8_t>(p_raw_array) );
  1762. }
  1763. Variant::Variant(const PoolVector<int>& p_int_array) {
  1764. type=POOL_INT_ARRAY;
  1765. memnew_placement( _data._mem, PoolVector<int>(p_int_array) );
  1766. }
  1767. Variant::Variant(const PoolVector<real_t>& p_real_array) {
  1768. type=POOL_REAL_ARRAY;
  1769. memnew_placement( _data._mem, PoolVector<real_t>(p_real_array) );
  1770. }
  1771. Variant::Variant(const PoolVector<String>& p_string_array) {
  1772. type=POOL_STRING_ARRAY;
  1773. memnew_placement( _data._mem, PoolVector<String>(p_string_array) );
  1774. }
  1775. Variant::Variant(const PoolVector<Vector3>& p_vector3_array) {
  1776. type=POOL_VECTOR3_ARRAY;
  1777. memnew_placement( _data._mem, PoolVector<Vector3>(p_vector3_array) );
  1778. }
  1779. Variant::Variant(const PoolVector<Vector2>& p_vector2_array) {
  1780. type=POOL_VECTOR2_ARRAY;
  1781. memnew_placement( _data._mem, PoolVector<Vector2>(p_vector2_array) );
  1782. }
  1783. Variant::Variant(const PoolVector<Color>& p_color_array) {
  1784. type=POOL_COLOR_ARRAY;
  1785. memnew_placement( _data._mem, PoolVector<Color>(p_color_array) );
  1786. }
  1787. Variant::Variant(const PoolVector<Face3>& p_face_array) {
  1788. PoolVector<Vector3> vertices;
  1789. int face_count=p_face_array.size();
  1790. vertices.resize(face_count*3);
  1791. if (face_count) {
  1792. PoolVector<Face3>::Read r = p_face_array.read();
  1793. PoolVector<Vector3>::Write w = vertices.write();
  1794. for(int i=0;i<face_count;i++) {
  1795. for(int j=0;j<3;j++)
  1796. w[i*3+j]=r[i].vertex[j];
  1797. }
  1798. r=PoolVector<Face3>::Read();
  1799. w=PoolVector<Vector3>::Write();
  1800. }
  1801. type = NIL;
  1802. *this = vertices;
  1803. }
  1804. /* helpers */
  1805. Variant::Variant(const Vector<Variant>& p_array) {
  1806. type=NIL;
  1807. Array v;
  1808. int len=p_array.size();
  1809. v.resize(len);
  1810. for (int i=0;i<len;i++)
  1811. v.set(i,p_array[i]);
  1812. *this=v;
  1813. }
  1814. Variant::Variant(const Vector<uint8_t>& p_array) {
  1815. type=NIL;
  1816. PoolVector<uint8_t> v;
  1817. int len=p_array.size();
  1818. v.resize(len);
  1819. for (int i=0;i<len;i++)
  1820. v.set(i,p_array[i]);
  1821. *this=v;
  1822. }
  1823. Variant::Variant(const Vector<int>& p_array) {
  1824. type=NIL;
  1825. PoolVector<int> v;
  1826. int len=p_array.size();
  1827. v.resize(len);
  1828. for (int i=0;i<len;i++)
  1829. v.set(i,p_array[i]);
  1830. *this=v;
  1831. }
  1832. Variant::Variant(const Vector<real_t>& p_array) {
  1833. type=NIL;
  1834. PoolVector<real_t> v;
  1835. int len=p_array.size();
  1836. v.resize(len);
  1837. for (int i=0;i<len;i++)
  1838. v.set(i,p_array[i]);
  1839. *this=v;
  1840. }
  1841. Variant::Variant(const Vector<String>& p_array) {
  1842. type=NIL;
  1843. PoolVector<String> v;
  1844. int len=p_array.size();
  1845. v.resize(len);
  1846. for (int i=0;i<len;i++)
  1847. v.set(i,p_array[i]);
  1848. *this=v;
  1849. }
  1850. Variant::Variant(const Vector<Vector3>& p_array) {
  1851. type=NIL;
  1852. PoolVector<Vector3> v;
  1853. int len=p_array.size();
  1854. if (len>0) {
  1855. v.resize(len);
  1856. PoolVector<Vector3>::Write w = v.write();
  1857. const Vector3 *r = p_array.ptr();
  1858. for (int i=0;i<len;i++)
  1859. w[i]=r[i];
  1860. }
  1861. *this=v;
  1862. }
  1863. Variant::Variant(const Vector<Color>& p_array) {
  1864. type=NIL;
  1865. PoolVector<Color> v;
  1866. int len=p_array.size();
  1867. v.resize(len);
  1868. for (int i=0;i<len;i++)
  1869. v.set(i,p_array[i]);
  1870. *this=v;
  1871. }
  1872. void Variant::operator=(const Variant& p_variant) {
  1873. reference(p_variant);
  1874. }
  1875. Variant::Variant(const IP_Address& p_address) {
  1876. type=STRING;
  1877. memnew_placement( _data._mem, String( p_address ) );
  1878. }
  1879. Variant::Variant(const Variant& p_variant) {
  1880. type=NIL;
  1881. reference(p_variant);
  1882. }
  1883. /*
  1884. Variant::~Variant() {
  1885. clear();
  1886. }*/
  1887. uint32_t Variant::hash() const {
  1888. switch( type ) {
  1889. case NIL: {
  1890. return 0;
  1891. } break;
  1892. case BOOL: {
  1893. return _data._bool?1:0;
  1894. } break;
  1895. case INT: {
  1896. return _data._int;
  1897. } break;
  1898. case REAL: {
  1899. return hash_djb2_one_float(_data._real);
  1900. } break;
  1901. case STRING: {
  1902. return reinterpret_cast<const String*>(_data._mem)->hash();
  1903. } break;
  1904. // math types
  1905. case VECTOR2: {
  1906. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->x);
  1907. return hash_djb2_one_float(reinterpret_cast<const Vector2*>(_data._mem)->y,hash);
  1908. } break;
  1909. case RECT2: {
  1910. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.x);
  1911. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->pos.y,hash);
  1912. hash = hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.x,hash);
  1913. return hash_djb2_one_float(reinterpret_cast<const Rect2*>(_data._mem)->size.y,hash);
  1914. } break;
  1915. case TRANSFORM2D: {
  1916. uint32_t hash = 5831;
  1917. for(int i=0;i<3;i++) {
  1918. for(int j=0;j<2;j++) {
  1919. hash = hash_djb2_one_float(_data._transform2d->elements[i][j],hash);
  1920. }
  1921. }
  1922. return hash;
  1923. } break;
  1924. case VECTOR3: {
  1925. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->x);
  1926. hash = hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->y,hash);
  1927. return hash_djb2_one_float(reinterpret_cast<const Vector3*>(_data._mem)->z,hash);
  1928. } break;
  1929. case PLANE: {
  1930. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.x);
  1931. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.y,hash);
  1932. hash = hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->normal.z,hash);
  1933. return hash_djb2_one_float(reinterpret_cast<const Plane*>(_data._mem)->d,hash);
  1934. } break;
  1935. /*
  1936. case QUAT: {
  1937. } break;*/
  1938. case RECT3: {
  1939. uint32_t hash = 5831;
  1940. for(int i=0;i<3;i++) {
  1941. hash = hash_djb2_one_float(_data._rect3->pos[i],hash);
  1942. hash = hash_djb2_one_float(_data._rect3->size[i],hash);
  1943. }
  1944. return hash;
  1945. } break;
  1946. case QUAT: {
  1947. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->x);
  1948. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->y,hash);
  1949. hash = hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->z,hash);
  1950. return hash_djb2_one_float(reinterpret_cast<const Quat*>(_data._mem)->w,hash);
  1951. } break;
  1952. case BASIS: {
  1953. uint32_t hash = 5831;
  1954. for(int i=0;i<3;i++) {
  1955. for(int j=0;j<3;j++) {
  1956. hash = hash_djb2_one_float(_data._basis->elements[i][j],hash);
  1957. }
  1958. }
  1959. return hash;
  1960. } break;
  1961. case TRANSFORM: {
  1962. uint32_t hash = 5831;
  1963. for(int i=0;i<3;i++) {
  1964. for(int j=0;j<3;j++) {
  1965. hash = hash_djb2_one_float(_data._transform->basis.elements[i][j],hash);
  1966. }
  1967. hash = hash_djb2_one_float(_data._transform->origin[i],hash);
  1968. }
  1969. return hash;
  1970. } break;
  1971. // misc types
  1972. case COLOR: {
  1973. uint32_t hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->r);
  1974. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->g,hash);
  1975. hash = hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->b,hash);
  1976. return hash_djb2_one_float(reinterpret_cast<const Color*>(_data._mem)->a,hash);
  1977. } break;
  1978. case IMAGE: {
  1979. return 0;
  1980. } break;
  1981. case _RID: {
  1982. return hash_djb2_one_64(reinterpret_cast<const RID*>(_data._mem)->get_id());
  1983. } break;
  1984. case OBJECT: {
  1985. return hash_djb2_one_64(make_uint64_t(_get_obj().obj));
  1986. } break;
  1987. case NODE_PATH: {
  1988. return reinterpret_cast<const NodePath*>(_data._mem)->hash();
  1989. } break;
  1990. case INPUT_EVENT: {
  1991. return hash_djb2_buffer((uint8_t*)_data._input_event,sizeof(InputEvent));
  1992. } break;
  1993. case DICTIONARY: {
  1994. return reinterpret_cast<const Dictionary*>(_data._mem)->hash();
  1995. } break;
  1996. case ARRAY: {
  1997. const Array& arr = *reinterpret_cast<const Array* >(_data._mem);
  1998. return arr.hash();
  1999. } break;
  2000. case POOL_BYTE_ARRAY: {
  2001. const PoolVector<uint8_t>& arr = *reinterpret_cast<const PoolVector<uint8_t>* >(_data._mem);
  2002. int len = arr.size();
  2003. PoolVector<uint8_t>::Read r = arr.read();
  2004. return hash_djb2_buffer((uint8_t*)&r[0],len);
  2005. } break;
  2006. case POOL_INT_ARRAY: {
  2007. const PoolVector<int>& arr = *reinterpret_cast<const PoolVector<int>* >(_data._mem);
  2008. int len = arr.size();
  2009. PoolVector<int>::Read r = arr.read();
  2010. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(int));
  2011. } break;
  2012. case POOL_REAL_ARRAY: {
  2013. const PoolVector<real_t>& arr = *reinterpret_cast<const PoolVector<real_t>* >(_data._mem);
  2014. int len = arr.size();
  2015. PoolVector<real_t>::Read r = arr.read();
  2016. return hash_djb2_buffer((uint8_t*)&r[0],len*sizeof(real_t));
  2017. } break;
  2018. case POOL_STRING_ARRAY: {
  2019. uint32_t hash=5831;
  2020. const PoolVector<String>& arr = *reinterpret_cast<const PoolVector<String>* >(_data._mem);
  2021. int len = arr.size();
  2022. PoolVector<String>::Read r = arr.read();
  2023. for(int i=0;i<len;i++) {
  2024. hash = hash_djb2_one_32(r[i].hash(),hash);
  2025. }
  2026. return hash;
  2027. } break;
  2028. case POOL_VECTOR2_ARRAY: {
  2029. uint32_t hash=5831;
  2030. const PoolVector<Vector2>& arr = *reinterpret_cast<const PoolVector<Vector2>* >(_data._mem);
  2031. int len = arr.size();
  2032. PoolVector<Vector2>::Read r = arr.read();
  2033. for(int i=0;i<len;i++) {
  2034. hash = hash_djb2_one_float(r[i].x,hash);
  2035. hash = hash_djb2_one_float(r[i].y,hash);
  2036. }
  2037. return hash;
  2038. } break;
  2039. case POOL_VECTOR3_ARRAY: {
  2040. uint32_t hash=5831;
  2041. const PoolVector<Vector3>& arr = *reinterpret_cast<const PoolVector<Vector3>* >(_data._mem);
  2042. int len = arr.size();
  2043. PoolVector<Vector3>::Read r = arr.read();
  2044. for(int i=0;i<len;i++) {
  2045. hash = hash_djb2_one_float(r[i].x,hash);
  2046. hash = hash_djb2_one_float(r[i].y,hash);
  2047. hash = hash_djb2_one_float(r[i].z,hash);
  2048. }
  2049. return hash;
  2050. } break;
  2051. case POOL_COLOR_ARRAY: {
  2052. uint32_t hash=5831;
  2053. const PoolVector<Color>& arr = *reinterpret_cast<const PoolVector<Color>* >(_data._mem);
  2054. int len = arr.size();
  2055. PoolVector<Color>::Read r = arr.read();
  2056. for(int i=0;i<len;i++) {
  2057. hash = hash_djb2_one_float(r[i].r,hash);
  2058. hash = hash_djb2_one_float(r[i].g,hash);
  2059. hash = hash_djb2_one_float(r[i].b,hash);
  2060. hash = hash_djb2_one_float(r[i].a,hash);
  2061. }
  2062. return hash;
  2063. } break;
  2064. default: {}
  2065. }
  2066. return 0;
  2067. }
  2068. #define hash_compare_scalar(p_lhs, p_rhs)\
  2069. ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) == Math::is_nan(p_rhs))
  2070. #define hash_compare_vector2(p_lhs, p_rhs)\
  2071. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2072. (hash_compare_scalar((p_lhs).y, (p_rhs).y))
  2073. #define hash_compare_vector3(p_lhs, p_rhs)\
  2074. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2075. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2076. (hash_compare_scalar((p_lhs).z, (p_rhs).z))
  2077. #define hash_compare_quat(p_lhs, p_rhs)\
  2078. (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
  2079. (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
  2080. (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
  2081. (hash_compare_scalar((p_lhs).w, (p_rhs).w))
  2082. #define hash_compare_color(p_lhs, p_rhs)\
  2083. (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
  2084. (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
  2085. (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
  2086. (hash_compare_scalar((p_lhs).a, (p_rhs).a))
  2087. #define hash_compare_pool_array(p_lhs, p_rhs, p_type, p_compare_func)\
  2088. const PoolVector<p_type>& l = *reinterpret_cast<const PoolVector<p_type>*>(p_lhs);\
  2089. const PoolVector<p_type>& r = *reinterpret_cast<const PoolVector<p_type>*>(p_rhs);\
  2090. \
  2091. if(l.size() != r.size()) \
  2092. return false; \
  2093. \
  2094. PoolVector<p_type>::Read lr = l.read(); \
  2095. PoolVector<p_type>::Read rr = r.read(); \
  2096. \
  2097. for(int i = 0; i < l.size(); ++i) { \
  2098. if(! p_compare_func((lr[i]), (rr[i]))) \
  2099. return false; \
  2100. }\
  2101. \
  2102. return true
  2103. bool Variant::hash_compare(const Variant& p_variant) const {
  2104. if (type != p_variant.type)
  2105. return false;
  2106. switch( type ) {
  2107. case REAL: {
  2108. return hash_compare_scalar(_data._real, p_variant._data._real);
  2109. } break;
  2110. case VECTOR2: {
  2111. const Vector2* l = reinterpret_cast<const Vector2*>(_data._mem);
  2112. const Vector2* r = reinterpret_cast<const Vector2*>(p_variant._data._mem);
  2113. return hash_compare_vector2(*l, *r);
  2114. } break;
  2115. case RECT2: {
  2116. const Rect2* l = reinterpret_cast<const Rect2*>(_data._mem);
  2117. const Rect2* r = reinterpret_cast<const Rect2*>(p_variant._data._mem);
  2118. return (hash_compare_vector2(l->pos, r->pos)) &&
  2119. (hash_compare_vector2(l->size, r->size));
  2120. } break;
  2121. case TRANSFORM2D: {
  2122. Transform2D* l = _data._transform2d;
  2123. Transform2D* r = p_variant._data._transform2d;
  2124. for(int i=0;i<3;i++) {
  2125. if (! (hash_compare_vector2(l->elements[i], r->elements[i])))
  2126. return false;
  2127. }
  2128. return true;
  2129. } break;
  2130. case VECTOR3: {
  2131. const Vector3* l = reinterpret_cast<const Vector3*>(_data._mem);
  2132. const Vector3* r = reinterpret_cast<const Vector3*>(p_variant._data._mem);
  2133. return hash_compare_vector3(*l, *r);
  2134. } break;
  2135. case PLANE: {
  2136. const Plane* l = reinterpret_cast<const Plane*>(_data._mem);
  2137. const Plane* r = reinterpret_cast<const Plane*>(p_variant._data._mem);
  2138. return (hash_compare_vector3(l->normal, r->normal)) &&
  2139. (hash_compare_scalar(l->d, r->d));
  2140. } break;
  2141. case RECT3: {
  2142. const Rect3* l = _data._rect3;
  2143. const Rect3* r = p_variant._data._rect3;
  2144. return (hash_compare_vector3(l->pos, r->pos) &&
  2145. (hash_compare_vector3(l->size, r->size)));
  2146. } break;
  2147. case QUAT: {
  2148. const Quat* l = reinterpret_cast<const Quat*>(_data._mem);
  2149. const Quat* r = reinterpret_cast<const Quat*>(p_variant._data._mem);
  2150. return hash_compare_quat(*l, *r);
  2151. } break;
  2152. case BASIS: {
  2153. const Basis* l = _data._basis;
  2154. const Basis* r = p_variant._data._basis;
  2155. for(int i=0;i<3;i++) {
  2156. if (! (hash_compare_vector3(l->elements[i], r->elements[i])))
  2157. return false;
  2158. }
  2159. return true;
  2160. } break;
  2161. case TRANSFORM: {
  2162. const Transform* l = _data._transform;
  2163. const Transform* r = p_variant._data._transform;
  2164. for(int i=0;i<3;i++) {
  2165. if (! (hash_compare_vector3(l->basis.elements[i], r->basis.elements[i])))
  2166. return false;
  2167. }
  2168. return hash_compare_vector3(l->origin, r->origin);
  2169. } break;
  2170. case COLOR: {
  2171. const Color* l = reinterpret_cast<const Color*>(_data._mem);
  2172. const Color* r = reinterpret_cast<const Color*>(p_variant._data._mem);
  2173. return hash_compare_color(*l, *r);
  2174. } break;
  2175. case ARRAY: {
  2176. const Array& l = *(reinterpret_cast<const Array*>(_data._mem));
  2177. const Array& r = *(reinterpret_cast<const Array*>(p_variant._data._mem));
  2178. if(l.size() != r.size())
  2179. return false;
  2180. for(int i = 0; i < l.size(); ++i) {
  2181. if(! l[i].hash_compare(r[i]))
  2182. return false;
  2183. }
  2184. return true;
  2185. } break;
  2186. case POOL_REAL_ARRAY: {
  2187. hash_compare_pool_array(_data._mem, p_variant._data._mem, real_t, hash_compare_scalar);
  2188. } break;
  2189. case POOL_VECTOR2_ARRAY: {
  2190. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector2, hash_compare_vector2);
  2191. } break;
  2192. case POOL_VECTOR3_ARRAY: {
  2193. hash_compare_pool_array(_data._mem, p_variant._data._mem, Vector3, hash_compare_vector3);
  2194. } break;
  2195. case POOL_COLOR_ARRAY: {
  2196. hash_compare_pool_array(_data._mem, p_variant._data._mem, Color, hash_compare_color);
  2197. } break;
  2198. default:
  2199. bool v;
  2200. Variant r;
  2201. evaluate(OP_EQUAL,*this,p_variant,r,v);
  2202. return r;
  2203. }
  2204. return false;
  2205. }
  2206. bool Variant::is_ref() const {
  2207. return type==OBJECT && !_get_obj().ref.is_null();
  2208. }
  2209. Vector<Variant> varray() {
  2210. return Vector<Variant>();
  2211. }
  2212. Vector<Variant> varray(const Variant& p_arg1) {
  2213. Vector<Variant> v;
  2214. v.push_back(p_arg1);
  2215. return v;
  2216. }
  2217. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2) {
  2218. Vector<Variant> v;
  2219. v.push_back(p_arg1);
  2220. v.push_back(p_arg2);
  2221. return v;
  2222. }
  2223. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3) {
  2224. Vector<Variant> v;
  2225. v.push_back(p_arg1);
  2226. v.push_back(p_arg2);
  2227. v.push_back(p_arg3);
  2228. return v;
  2229. }
  2230. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4) {
  2231. Vector<Variant> v;
  2232. v.push_back(p_arg1);
  2233. v.push_back(p_arg2);
  2234. v.push_back(p_arg3);
  2235. v.push_back(p_arg4);
  2236. return v;
  2237. }
  2238. Vector<Variant> varray(const Variant& p_arg1,const Variant& p_arg2,const Variant& p_arg3,const Variant& p_arg4,const Variant& p_arg5) {
  2239. Vector<Variant> v;
  2240. v.push_back(p_arg1);
  2241. v.push_back(p_arg2);
  2242. v.push_back(p_arg3);
  2243. v.push_back(p_arg4);
  2244. v.push_back(p_arg5);
  2245. return v;
  2246. }
  2247. void Variant::static_assign(const Variant& p_variant) {
  2248. }
  2249. bool Variant::is_shared() const {
  2250. switch(type) {
  2251. case OBJECT: return true;
  2252. case ARRAY: return true;
  2253. case DICTIONARY: return true;
  2254. default: {}
  2255. }
  2256. return false;
  2257. }
  2258. Variant Variant::call(const StringName& p_method,VARIANT_ARG_DECLARE) {
  2259. VARIANT_ARGPTRS;
  2260. int argc=0;
  2261. for(int i=0;i<VARIANT_ARG_MAX;i++) {
  2262. if (argptr[i]->get_type()==Variant::NIL)
  2263. break;
  2264. argc++;
  2265. }
  2266. CallError error;
  2267. Variant ret = call(p_method,argptr,argc,error);
  2268. switch(error.error) {
  2269. case CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2270. String err = "Invalid type for argument #"+itos(error.argument)+", expected '"+Variant::get_type_name(error.expected)+"'.";
  2271. ERR_PRINT(err.utf8().get_data());
  2272. } break;
  2273. case CallError::CALL_ERROR_INVALID_METHOD: {
  2274. String err = "Invalid method '"+p_method+"' for type '"+Variant::get_type_name(type)+"'.";
  2275. ERR_PRINT(err.utf8().get_data());
  2276. } break;
  2277. case CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2278. String err = "Too many arguments for method '"+p_method+"'";
  2279. ERR_PRINT(err.utf8().get_data());
  2280. } break;
  2281. default: {}
  2282. }
  2283. return ret;
  2284. }
  2285. void Variant::construct_from_string(const String& p_string,Variant& r_value,ObjectConstruct p_obj_construct,void *p_construct_ud) {
  2286. r_value=Variant();
  2287. }
  2288. String Variant::get_construct_string() const {
  2289. String vars;
  2290. VariantWriter::write_to_string(*this,vars);
  2291. return vars;
  2292. }
  2293. String Variant::get_call_error_text(Object* p_base, const StringName& p_method,const Variant** p_argptrs,int p_argcount,const Variant::CallError &ce) {
  2294. String err_text;
  2295. if (ce.error==Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  2296. int errorarg=ce.argument;
  2297. err_text="Cannot convert argument "+itos(errorarg+1)+" from "+Variant::get_type_name(p_argptrs[errorarg]->get_type())+" to "+Variant::get_type_name(ce.expected)+".";
  2298. } else if (ce.error==Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  2299. err_text="Method expected "+itos(ce.argument)+" arguments, but called with "+itos(p_argcount)+".";
  2300. } else if (ce.error==Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  2301. err_text="Method expected "+itos(ce.argument)+" arguments, but called with "+itos(p_argcount)+".";
  2302. } else if (ce.error==Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  2303. err_text="Method not found.";
  2304. } else if (ce.error==Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  2305. err_text="Instance is null";
  2306. } else if (ce.error==Variant::CallError::CALL_OK){
  2307. return "Call OK";
  2308. }
  2309. String class_name = p_base->get_class();
  2310. Ref<Script> script = p_base->get_script();
  2311. if (script.is_valid() && script->get_path().is_resource_file()) {
  2312. class_name+="("+script->get_path().get_file()+")";
  2313. }
  2314. return "'"+class_name+"::"+String(p_method)+"': "+err_text;
  2315. }
  2316. String vformat(const String& p_text, const Variant& p1,const Variant& p2,const Variant& p3,const Variant& p4,const Variant& p5) {
  2317. Array args;
  2318. if (p1.get_type()!=Variant::NIL) {
  2319. args.push_back(p1);
  2320. if (p2.get_type()!=Variant::NIL) {
  2321. args.push_back(p2);
  2322. if (p3.get_type()!=Variant::NIL) {
  2323. args.push_back(p3);
  2324. if (p4.get_type()!=Variant::NIL) {
  2325. args.push_back(p4);
  2326. if (p5.get_type()!=Variant::NIL) {
  2327. args.push_back(p5);
  2328. }
  2329. }
  2330. }
  2331. }
  2332. }
  2333. bool error=false;
  2334. String fmt = p_text.sprintf(args,&error);
  2335. ERR_FAIL_COND_V(error,String());
  2336. return fmt;
  2337. }