variant.cpp 64 KB

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