variant.cpp 67 KB

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