variant.cpp 62 KB

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