variant.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299
  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[i]), (rr[i]))) \
  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[i].hash_compare(r[i]))
  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. }