gdscript_functions.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. /**************************************************************************/
  2. /* gdscript_functions.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "gdscript_functions.h"
  31. #include "core/class_db.h"
  32. #include "core/func_ref.h"
  33. #include "core/io/json.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/math/math_funcs.h"
  36. #include "core/os/os.h"
  37. #include "core/reference.h"
  38. #include "core/variant_parser.h"
  39. #include "gdscript.h"
  40. const char *GDScriptFunctions::get_func_name(Function p_func) {
  41. ERR_FAIL_INDEX_V(p_func, FUNC_MAX, "");
  42. static const char *_names[FUNC_MAX] = {
  43. "sin",
  44. "cos",
  45. "tan",
  46. "sinh",
  47. "cosh",
  48. "tanh",
  49. "asin",
  50. "acos",
  51. "atan",
  52. "atan2",
  53. "sqrt",
  54. "fmod",
  55. "fposmod",
  56. "posmod",
  57. "floor",
  58. "ceil",
  59. "round",
  60. "abs",
  61. "sign",
  62. "pow",
  63. "log",
  64. "exp",
  65. "is_nan",
  66. "is_inf",
  67. "is_equal_approx",
  68. "is_zero_approx",
  69. "ease",
  70. "decimals",
  71. "step_decimals",
  72. "stepify",
  73. "lerp",
  74. "lerp_angle",
  75. "inverse_lerp",
  76. "range_lerp",
  77. "smoothstep",
  78. "move_toward",
  79. "dectime",
  80. "randomize",
  81. "randi",
  82. "randf",
  83. "rand_range",
  84. "seed",
  85. "rand_seed",
  86. "deg2rad",
  87. "rad2deg",
  88. "linear2db",
  89. "db2linear",
  90. "polar2cartesian",
  91. "cartesian2polar",
  92. "wrapi",
  93. "wrapf",
  94. "max",
  95. "min",
  96. "clamp",
  97. "nearest_po2",
  98. "weakref",
  99. "funcref",
  100. "convert",
  101. "typeof",
  102. "type_exists",
  103. "char",
  104. "ord",
  105. "str",
  106. "print",
  107. "printt",
  108. "prints",
  109. "printerr",
  110. "printraw",
  111. "print_debug",
  112. "push_error",
  113. "push_warning",
  114. "var2str",
  115. "str2var",
  116. "var2bytes",
  117. "bytes2var",
  118. "range",
  119. "load",
  120. "inst2dict",
  121. "dict2inst",
  122. "validate_json",
  123. "parse_json",
  124. "to_json",
  125. "hash",
  126. "Color8",
  127. "ColorN",
  128. "print_stack",
  129. "get_stack",
  130. "instance_from_id",
  131. "len",
  132. "is_instance_valid",
  133. "deep_equal",
  134. };
  135. return _names[p_func];
  136. }
  137. void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_count, Variant &r_ret, Variant::CallError &r_error) {
  138. r_error.error = Variant::CallError::CALL_OK;
  139. #ifdef DEBUG_ENABLED
  140. #define VALIDATE_ARG_COUNT(m_count) \
  141. if (p_arg_count < m_count) { \
  142. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \
  143. r_error.argument = m_count; \
  144. r_ret = Variant(); \
  145. return; \
  146. } \
  147. if (p_arg_count > m_count) { \
  148. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \
  149. r_error.argument = m_count; \
  150. r_ret = Variant(); \
  151. return; \
  152. }
  153. #define VALIDATE_ARG_NUM(m_arg) \
  154. if (!p_args[m_arg]->is_num()) { \
  155. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  156. r_error.argument = m_arg; \
  157. r_error.expected = Variant::REAL; \
  158. r_ret = Variant(); \
  159. return; \
  160. }
  161. #else
  162. #define VALIDATE_ARG_COUNT(m_count)
  163. #define VALIDATE_ARG_NUM(m_arg)
  164. #endif
  165. //using a switch, so the compiler generates a jumptable
  166. switch (p_func) {
  167. case MATH_SIN: {
  168. VALIDATE_ARG_COUNT(1);
  169. VALIDATE_ARG_NUM(0);
  170. r_ret = Math::sin((double)*p_args[0]);
  171. } break;
  172. case MATH_COS: {
  173. VALIDATE_ARG_COUNT(1);
  174. VALIDATE_ARG_NUM(0);
  175. r_ret = Math::cos((double)*p_args[0]);
  176. } break;
  177. case MATH_TAN: {
  178. VALIDATE_ARG_COUNT(1);
  179. VALIDATE_ARG_NUM(0);
  180. r_ret = Math::tan((double)*p_args[0]);
  181. } break;
  182. case MATH_SINH: {
  183. VALIDATE_ARG_COUNT(1);
  184. VALIDATE_ARG_NUM(0);
  185. r_ret = Math::sinh((double)*p_args[0]);
  186. } break;
  187. case MATH_COSH: {
  188. VALIDATE_ARG_COUNT(1);
  189. VALIDATE_ARG_NUM(0);
  190. r_ret = Math::cosh((double)*p_args[0]);
  191. } break;
  192. case MATH_TANH: {
  193. VALIDATE_ARG_COUNT(1);
  194. VALIDATE_ARG_NUM(0);
  195. r_ret = Math::tanh((double)*p_args[0]);
  196. } break;
  197. case MATH_ASIN: {
  198. VALIDATE_ARG_COUNT(1);
  199. VALIDATE_ARG_NUM(0);
  200. r_ret = Math::asin((double)*p_args[0]);
  201. } break;
  202. case MATH_ACOS: {
  203. VALIDATE_ARG_COUNT(1);
  204. VALIDATE_ARG_NUM(0);
  205. r_ret = Math::acos((double)*p_args[0]);
  206. } break;
  207. case MATH_ATAN: {
  208. VALIDATE_ARG_COUNT(1);
  209. VALIDATE_ARG_NUM(0);
  210. r_ret = Math::atan((double)*p_args[0]);
  211. } break;
  212. case MATH_ATAN2: {
  213. VALIDATE_ARG_COUNT(2);
  214. VALIDATE_ARG_NUM(0);
  215. VALIDATE_ARG_NUM(1);
  216. r_ret = Math::atan2((double)*p_args[0], (double)*p_args[1]);
  217. } break;
  218. case MATH_SQRT: {
  219. VALIDATE_ARG_COUNT(1);
  220. VALIDATE_ARG_NUM(0);
  221. r_ret = Math::sqrt((double)*p_args[0]);
  222. } break;
  223. case MATH_FMOD: {
  224. VALIDATE_ARG_COUNT(2);
  225. VALIDATE_ARG_NUM(0);
  226. VALIDATE_ARG_NUM(1);
  227. r_ret = Math::fmod((double)*p_args[0], (double)*p_args[1]);
  228. } break;
  229. case MATH_FPOSMOD: {
  230. VALIDATE_ARG_COUNT(2);
  231. VALIDATE_ARG_NUM(0);
  232. VALIDATE_ARG_NUM(1);
  233. r_ret = Math::fposmod((double)*p_args[0], (double)*p_args[1]);
  234. } break;
  235. case MATH_POSMOD: {
  236. VALIDATE_ARG_COUNT(2);
  237. VALIDATE_ARG_NUM(0);
  238. VALIDATE_ARG_NUM(1);
  239. r_ret = Math::posmod((int64_t)*p_args[0], (int64_t)*p_args[1]);
  240. } break;
  241. case MATH_FLOOR: {
  242. VALIDATE_ARG_COUNT(1);
  243. VALIDATE_ARG_NUM(0);
  244. r_ret = Math::floor((double)*p_args[0]);
  245. } break;
  246. case MATH_CEIL: {
  247. VALIDATE_ARG_COUNT(1);
  248. VALIDATE_ARG_NUM(0);
  249. r_ret = Math::ceil((double)*p_args[0]);
  250. } break;
  251. case MATH_ROUND: {
  252. VALIDATE_ARG_COUNT(1);
  253. VALIDATE_ARG_NUM(0);
  254. r_ret = Math::round((double)*p_args[0]);
  255. } break;
  256. case MATH_ABS: {
  257. VALIDATE_ARG_COUNT(1);
  258. if (p_args[0]->get_type() == Variant::INT) {
  259. int64_t i = *p_args[0];
  260. r_ret = ABS(i);
  261. } else if (p_args[0]->get_type() == Variant::REAL) {
  262. double r = *p_args[0];
  263. r_ret = Math::abs(r);
  264. } else {
  265. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  266. r_error.argument = 0;
  267. r_error.expected = Variant::REAL;
  268. r_ret = Variant();
  269. }
  270. } break;
  271. case MATH_SIGN: {
  272. VALIDATE_ARG_COUNT(1);
  273. if (p_args[0]->get_type() == Variant::INT) {
  274. int64_t i = *p_args[0];
  275. r_ret = i < 0 ? -1 : (i > 0 ? +1 : 0);
  276. } else if (p_args[0]->get_type() == Variant::REAL) {
  277. double r = *p_args[0];
  278. r_ret = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0);
  279. } else {
  280. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  281. r_error.argument = 0;
  282. r_error.expected = Variant::REAL;
  283. r_ret = Variant();
  284. }
  285. } break;
  286. case MATH_POW: {
  287. VALIDATE_ARG_COUNT(2);
  288. VALIDATE_ARG_NUM(0);
  289. VALIDATE_ARG_NUM(1);
  290. r_ret = Math::pow((double)*p_args[0], (double)*p_args[1]);
  291. } break;
  292. case MATH_LOG: {
  293. VALIDATE_ARG_COUNT(1);
  294. VALIDATE_ARG_NUM(0);
  295. r_ret = Math::log((double)*p_args[0]);
  296. } break;
  297. case MATH_EXP: {
  298. VALIDATE_ARG_COUNT(1);
  299. VALIDATE_ARG_NUM(0);
  300. r_ret = Math::exp((double)*p_args[0]);
  301. } break;
  302. case MATH_ISNAN: {
  303. VALIDATE_ARG_COUNT(1);
  304. VALIDATE_ARG_NUM(0);
  305. r_ret = Math::is_nan((double)*p_args[0]);
  306. } break;
  307. case MATH_ISINF: {
  308. VALIDATE_ARG_COUNT(1);
  309. VALIDATE_ARG_NUM(0);
  310. r_ret = Math::is_inf((double)*p_args[0]);
  311. } break;
  312. case MATH_ISEQUALAPPROX: {
  313. VALIDATE_ARG_COUNT(2);
  314. VALIDATE_ARG_NUM(0);
  315. VALIDATE_ARG_NUM(1);
  316. r_ret = Math::is_equal_approx((real_t)*p_args[0], (real_t)*p_args[1]);
  317. } break;
  318. case MATH_ISZEROAPPROX: {
  319. VALIDATE_ARG_COUNT(1);
  320. VALIDATE_ARG_NUM(0);
  321. r_ret = Math::is_zero_approx((real_t)*p_args[0]);
  322. } break;
  323. case MATH_EASE: {
  324. VALIDATE_ARG_COUNT(2);
  325. VALIDATE_ARG_NUM(0);
  326. VALIDATE_ARG_NUM(1);
  327. r_ret = Math::ease((double)*p_args[0], (double)*p_args[1]);
  328. } break;
  329. case MATH_DECIMALS: {
  330. VALIDATE_ARG_COUNT(1);
  331. VALIDATE_ARG_NUM(0);
  332. r_ret = Math::step_decimals((double)*p_args[0]);
  333. WARN_DEPRECATED_MSG("GDScript method 'decimals' is deprecated and has been renamed to 'step_decimals', please update your code accordingly.");
  334. } break;
  335. case MATH_STEP_DECIMALS: {
  336. VALIDATE_ARG_COUNT(1);
  337. VALIDATE_ARG_NUM(0);
  338. r_ret = Math::step_decimals((double)*p_args[0]);
  339. } break;
  340. case MATH_STEPIFY: {
  341. VALIDATE_ARG_COUNT(2);
  342. VALIDATE_ARG_NUM(0);
  343. VALIDATE_ARG_NUM(1);
  344. r_ret = Math::stepify((double)*p_args[0], (double)*p_args[1]);
  345. } break;
  346. case MATH_LERP: {
  347. VALIDATE_ARG_COUNT(3);
  348. VALIDATE_ARG_NUM(2);
  349. const double t = (double)*p_args[2];
  350. switch (p_args[0]->get_type() == p_args[1]->get_type() ? p_args[0]->get_type() : Variant::REAL) {
  351. case Variant::VECTOR2: {
  352. r_ret = ((Vector2)*p_args[0]).linear_interpolate((Vector2)*p_args[1], t);
  353. } break;
  354. case Variant::VECTOR3: {
  355. r_ret = ((Vector3)*p_args[0]).linear_interpolate((Vector3)*p_args[1], t);
  356. } break;
  357. case Variant::COLOR: {
  358. r_ret = ((Color)*p_args[0]).linear_interpolate((Color)*p_args[1], t);
  359. } break;
  360. default: {
  361. VALIDATE_ARG_NUM(0);
  362. VALIDATE_ARG_NUM(1);
  363. r_ret = Math::lerp((double)*p_args[0], (double)*p_args[1], t);
  364. } break;
  365. }
  366. } break;
  367. case MATH_LERP_ANGLE: {
  368. VALIDATE_ARG_COUNT(3);
  369. VALIDATE_ARG_NUM(0);
  370. VALIDATE_ARG_NUM(1);
  371. VALIDATE_ARG_NUM(2);
  372. r_ret = Math::lerp_angle((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  373. } break;
  374. case MATH_INVERSE_LERP: {
  375. VALIDATE_ARG_COUNT(3);
  376. VALIDATE_ARG_NUM(0);
  377. VALIDATE_ARG_NUM(1);
  378. VALIDATE_ARG_NUM(2);
  379. r_ret = Math::inverse_lerp((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  380. } break;
  381. case MATH_RANGE_LERP: {
  382. VALIDATE_ARG_COUNT(5);
  383. VALIDATE_ARG_NUM(0);
  384. VALIDATE_ARG_NUM(1);
  385. VALIDATE_ARG_NUM(2);
  386. VALIDATE_ARG_NUM(3);
  387. VALIDATE_ARG_NUM(4);
  388. r_ret = Math::range_lerp((double)*p_args[0], (double)*p_args[1], (double)*p_args[2], (double)*p_args[3], (double)*p_args[4]);
  389. } break;
  390. case MATH_SMOOTHSTEP: {
  391. VALIDATE_ARG_COUNT(3);
  392. VALIDATE_ARG_NUM(0);
  393. VALIDATE_ARG_NUM(1);
  394. VALIDATE_ARG_NUM(2);
  395. r_ret = Math::smoothstep((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  396. } break;
  397. case MATH_MOVE_TOWARD: {
  398. VALIDATE_ARG_COUNT(3);
  399. VALIDATE_ARG_NUM(0);
  400. VALIDATE_ARG_NUM(1);
  401. VALIDATE_ARG_NUM(2);
  402. r_ret = Math::move_toward((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  403. } break;
  404. case MATH_DECTIME: {
  405. VALIDATE_ARG_COUNT(3);
  406. VALIDATE_ARG_NUM(0);
  407. VALIDATE_ARG_NUM(1);
  408. VALIDATE_ARG_NUM(2);
  409. r_ret = Math::dectime((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  410. } break;
  411. case MATH_RANDOMIZE: {
  412. VALIDATE_ARG_COUNT(0);
  413. Math::randomize();
  414. r_ret = Variant();
  415. } break;
  416. case MATH_RAND: {
  417. VALIDATE_ARG_COUNT(0);
  418. r_ret = Math::rand();
  419. } break;
  420. case MATH_RANDF: {
  421. VALIDATE_ARG_COUNT(0);
  422. r_ret = Math::randf();
  423. } break;
  424. case MATH_RANDOM: {
  425. VALIDATE_ARG_COUNT(2);
  426. VALIDATE_ARG_NUM(0);
  427. VALIDATE_ARG_NUM(1);
  428. r_ret = Math::random((double)*p_args[0], (double)*p_args[1]);
  429. } break;
  430. case MATH_SEED: {
  431. VALIDATE_ARG_COUNT(1);
  432. VALIDATE_ARG_NUM(0);
  433. uint64_t seed = *p_args[0];
  434. Math::seed(seed);
  435. r_ret = Variant();
  436. } break;
  437. case MATH_RANDSEED: {
  438. VALIDATE_ARG_COUNT(1);
  439. VALIDATE_ARG_NUM(0);
  440. uint64_t seed = *p_args[0];
  441. int ret = Math::rand_from_seed(&seed);
  442. Array reta;
  443. reta.push_back(ret);
  444. reta.push_back(seed);
  445. r_ret = reta;
  446. } break;
  447. case MATH_DEG2RAD: {
  448. VALIDATE_ARG_COUNT(1);
  449. VALIDATE_ARG_NUM(0);
  450. r_ret = Math::deg2rad((double)*p_args[0]);
  451. } break;
  452. case MATH_RAD2DEG: {
  453. VALIDATE_ARG_COUNT(1);
  454. VALIDATE_ARG_NUM(0);
  455. r_ret = Math::rad2deg((double)*p_args[0]);
  456. } break;
  457. case MATH_LINEAR2DB: {
  458. VALIDATE_ARG_COUNT(1);
  459. VALIDATE_ARG_NUM(0);
  460. r_ret = Math::linear2db((double)*p_args[0]);
  461. } break;
  462. case MATH_DB2LINEAR: {
  463. VALIDATE_ARG_COUNT(1);
  464. VALIDATE_ARG_NUM(0);
  465. r_ret = Math::db2linear((double)*p_args[0]);
  466. } break;
  467. case MATH_POLAR2CARTESIAN: {
  468. VALIDATE_ARG_COUNT(2);
  469. VALIDATE_ARG_NUM(0);
  470. VALIDATE_ARG_NUM(1);
  471. double r = *p_args[0];
  472. double th = *p_args[1];
  473. r_ret = Vector2(r * Math::cos(th), r * Math::sin(th));
  474. } break;
  475. case MATH_CARTESIAN2POLAR: {
  476. VALIDATE_ARG_COUNT(2);
  477. VALIDATE_ARG_NUM(0);
  478. VALIDATE_ARG_NUM(1);
  479. double x = *p_args[0];
  480. double y = *p_args[1];
  481. r_ret = Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x));
  482. } break;
  483. case MATH_WRAP: {
  484. VALIDATE_ARG_COUNT(3);
  485. r_ret = Math::wrapi((int64_t)*p_args[0], (int64_t)*p_args[1], (int64_t)*p_args[2]);
  486. } break;
  487. case MATH_WRAPF: {
  488. VALIDATE_ARG_COUNT(3);
  489. r_ret = Math::wrapf((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
  490. } break;
  491. case LOGIC_MAX: {
  492. VALIDATE_ARG_COUNT(2);
  493. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT) {
  494. int64_t a = *p_args[0];
  495. int64_t b = *p_args[1];
  496. r_ret = MAX(a, b);
  497. } else {
  498. VALIDATE_ARG_NUM(0);
  499. VALIDATE_ARG_NUM(1);
  500. double a = *p_args[0];
  501. double b = *p_args[1];
  502. r_ret = MAX(a, b);
  503. }
  504. } break;
  505. case LOGIC_MIN: {
  506. VALIDATE_ARG_COUNT(2);
  507. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT) {
  508. int64_t a = *p_args[0];
  509. int64_t b = *p_args[1];
  510. r_ret = MIN(a, b);
  511. } else {
  512. VALIDATE_ARG_NUM(0);
  513. VALIDATE_ARG_NUM(1);
  514. double a = *p_args[0];
  515. double b = *p_args[1];
  516. r_ret = MIN(a, b);
  517. }
  518. } break;
  519. case LOGIC_CLAMP: {
  520. VALIDATE_ARG_COUNT(3);
  521. if (p_args[0]->get_type() == Variant::INT && p_args[1]->get_type() == Variant::INT && p_args[2]->get_type() == Variant::INT) {
  522. int64_t a = *p_args[0];
  523. int64_t b = *p_args[1];
  524. int64_t c = *p_args[2];
  525. r_ret = CLAMP(a, b, c);
  526. } else {
  527. VALIDATE_ARG_NUM(0);
  528. VALIDATE_ARG_NUM(1);
  529. VALIDATE_ARG_NUM(2);
  530. double a = *p_args[0];
  531. double b = *p_args[1];
  532. double c = *p_args[2];
  533. r_ret = CLAMP(a, b, c);
  534. }
  535. } break;
  536. case LOGIC_NEAREST_PO2: {
  537. VALIDATE_ARG_COUNT(1);
  538. VALIDATE_ARG_NUM(0);
  539. int64_t num = *p_args[0];
  540. r_ret = next_power_of_2(num);
  541. } break;
  542. case OBJ_WEAKREF: {
  543. VALIDATE_ARG_COUNT(1);
  544. if (p_args[0]->get_type() == Variant::OBJECT) {
  545. if (p_args[0]->is_ref()) {
  546. Ref<WeakRef> wref = memnew(WeakRef);
  547. REF r = *p_args[0];
  548. if (r.is_valid()) {
  549. wref->set_ref(r);
  550. }
  551. r_ret = wref;
  552. } else {
  553. Ref<WeakRef> wref = memnew(WeakRef);
  554. Object *obj = *p_args[0];
  555. if (obj) {
  556. wref->set_obj(obj);
  557. }
  558. r_ret = wref;
  559. }
  560. } else if (p_args[0]->get_type() == Variant::NIL) {
  561. Ref<WeakRef> wref = memnew(WeakRef);
  562. r_ret = wref;
  563. } else {
  564. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  565. r_error.argument = 0;
  566. r_error.expected = Variant::OBJECT;
  567. r_ret = Variant();
  568. return;
  569. }
  570. } break;
  571. case FUNC_FUNCREF: {
  572. VALIDATE_ARG_COUNT(2);
  573. if (p_args[0]->get_type() != Variant::OBJECT) {
  574. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  575. r_error.argument = 0;
  576. r_error.expected = Variant::OBJECT;
  577. r_ret = Variant();
  578. return;
  579. }
  580. if (p_args[1]->get_type() != Variant::STRING && p_args[1]->get_type() != Variant::NODE_PATH) {
  581. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  582. r_error.argument = 1;
  583. r_error.expected = Variant::STRING;
  584. r_ret = Variant();
  585. return;
  586. }
  587. Ref<FuncRef> fr = memnew(FuncRef);
  588. fr->set_instance(*p_args[0]);
  589. fr->set_function(*p_args[1]);
  590. r_ret = fr;
  591. } break;
  592. case TYPE_CONVERT: {
  593. VALIDATE_ARG_COUNT(2);
  594. VALIDATE_ARG_NUM(1);
  595. int type = *p_args[1];
  596. if (type < 0 || type >= Variant::VARIANT_MAX) {
  597. r_ret = RTR("Invalid type argument to convert(), use TYPE_* constants.");
  598. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  599. r_error.argument = 0;
  600. r_error.expected = Variant::INT;
  601. return;
  602. } else {
  603. r_ret = Variant::construct(Variant::Type(type), p_args, 1, r_error);
  604. }
  605. } break;
  606. case TYPE_OF: {
  607. VALIDATE_ARG_COUNT(1);
  608. r_ret = p_args[0]->get_type();
  609. } break;
  610. case TYPE_EXISTS: {
  611. VALIDATE_ARG_COUNT(1);
  612. r_ret = ClassDB::class_exists(*p_args[0]);
  613. } break;
  614. case TEXT_CHAR: {
  615. VALIDATE_ARG_COUNT(1);
  616. VALIDATE_ARG_NUM(0);
  617. CharType result[2] = { *p_args[0], 0 };
  618. r_ret = String(result);
  619. } break;
  620. case TEXT_ORD: {
  621. VALIDATE_ARG_COUNT(1);
  622. if (p_args[0]->get_type() != Variant::STRING) {
  623. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  624. r_error.argument = 0;
  625. r_error.expected = Variant::STRING;
  626. r_ret = Variant();
  627. return;
  628. }
  629. String str = p_args[0]->operator String();
  630. if (str.length() != 1) {
  631. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  632. r_error.argument = 0;
  633. r_error.expected = Variant::STRING;
  634. r_ret = RTR("Expected a string of length 1 (a character).");
  635. return;
  636. }
  637. r_ret = str.get(0);
  638. } break;
  639. case TEXT_STR: {
  640. if (p_arg_count < 1) {
  641. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  642. r_error.argument = 1;
  643. r_ret = Variant();
  644. return;
  645. }
  646. String str;
  647. for (int i = 0; i < p_arg_count; i++) {
  648. String os = p_args[i]->operator String();
  649. if (i == 0) {
  650. str = os;
  651. } else {
  652. str += os;
  653. }
  654. }
  655. r_ret = str;
  656. } break;
  657. case TEXT_PRINT: {
  658. String str;
  659. for (int i = 0; i < p_arg_count; i++) {
  660. str += p_args[i]->operator String();
  661. }
  662. print_line(str);
  663. r_ret = Variant();
  664. } break;
  665. case TEXT_PRINT_TABBED: {
  666. String str;
  667. for (int i = 0; i < p_arg_count; i++) {
  668. if (i) {
  669. str += "\t";
  670. }
  671. str += p_args[i]->operator String();
  672. }
  673. print_line(str);
  674. r_ret = Variant();
  675. } break;
  676. case TEXT_PRINT_SPACED: {
  677. String str;
  678. for (int i = 0; i < p_arg_count; i++) {
  679. if (i) {
  680. str += " ";
  681. }
  682. str += p_args[i]->operator String();
  683. }
  684. print_line(str);
  685. r_ret = Variant();
  686. } break;
  687. case TEXT_PRINTERR: {
  688. String str;
  689. for (int i = 0; i < p_arg_count; i++) {
  690. str += p_args[i]->operator String();
  691. }
  692. print_error(str);
  693. r_ret = Variant();
  694. } break;
  695. case TEXT_PRINTRAW: {
  696. String str;
  697. for (int i = 0; i < p_arg_count; i++) {
  698. str += p_args[i]->operator String();
  699. }
  700. OS::get_singleton()->print("%s", str.utf8().get_data());
  701. r_ret = Variant();
  702. } break;
  703. case TEXT_PRINT_DEBUG: {
  704. String str;
  705. for (int i = 0; i < p_arg_count; i++) {
  706. str += p_args[i]->operator String();
  707. }
  708. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  709. if (script->debug_get_stack_level_count() > 0) {
  710. str += "\n At: " + script->debug_get_stack_level_source(0) + ":" + itos(script->debug_get_stack_level_line(0)) + ":" + script->debug_get_stack_level_function(0) + "()";
  711. }
  712. print_line(str);
  713. r_ret = Variant();
  714. } break;
  715. case PUSH_ERROR: {
  716. VALIDATE_ARG_COUNT(1);
  717. if (p_args[0]->get_type() != Variant::STRING) {
  718. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  719. r_error.argument = 0;
  720. r_error.expected = Variant::STRING;
  721. r_ret = Variant();
  722. break;
  723. }
  724. String message = *p_args[0];
  725. ERR_PRINT(message);
  726. r_ret = Variant();
  727. } break;
  728. case PUSH_WARNING: {
  729. VALIDATE_ARG_COUNT(1);
  730. if (p_args[0]->get_type() != Variant::STRING) {
  731. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  732. r_error.argument = 0;
  733. r_error.expected = Variant::STRING;
  734. r_ret = Variant();
  735. break;
  736. }
  737. String message = *p_args[0];
  738. WARN_PRINT(message);
  739. r_ret = Variant();
  740. } break;
  741. case VAR_TO_STR: {
  742. VALIDATE_ARG_COUNT(1);
  743. String vars;
  744. VariantWriter::write_to_string(*p_args[0], vars);
  745. r_ret = vars;
  746. } break;
  747. case STR_TO_VAR: {
  748. VALIDATE_ARG_COUNT(1);
  749. if (p_args[0]->get_type() != Variant::STRING) {
  750. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  751. r_error.argument = 0;
  752. r_error.expected = Variant::STRING;
  753. r_ret = Variant();
  754. return;
  755. }
  756. r_ret = *p_args[0];
  757. VariantParser::StreamString ss;
  758. ss.s = *p_args[0];
  759. String errs;
  760. int line;
  761. (void)VariantParser::parse(&ss, r_ret, errs, line);
  762. } break;
  763. case VAR_TO_BYTES: {
  764. bool full_objects = false;
  765. if (p_arg_count < 1) {
  766. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  767. r_error.argument = 1;
  768. r_ret = Variant();
  769. return;
  770. } else if (p_arg_count > 2) {
  771. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  772. r_error.argument = 2;
  773. r_ret = Variant();
  774. } else if (p_arg_count == 2) {
  775. if (p_args[1]->get_type() != Variant::BOOL) {
  776. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  777. r_error.argument = 1;
  778. r_error.expected = Variant::BOOL;
  779. r_ret = Variant();
  780. return;
  781. }
  782. full_objects = *p_args[1];
  783. }
  784. PoolByteArray barr;
  785. int len;
  786. Error err = encode_variant(*p_args[0], nullptr, len, full_objects);
  787. if (err) {
  788. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  789. r_error.argument = 0;
  790. r_error.expected = Variant::NIL;
  791. r_ret = "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).";
  792. return;
  793. }
  794. barr.resize(len);
  795. {
  796. PoolByteArray::Write w = barr.write();
  797. encode_variant(*p_args[0], w.ptr(), len, full_objects);
  798. }
  799. r_ret = barr;
  800. } break;
  801. case BYTES_TO_VAR: {
  802. bool allow_objects = false;
  803. if (p_arg_count < 1) {
  804. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  805. r_error.argument = 1;
  806. r_ret = Variant();
  807. return;
  808. } else if (p_arg_count > 2) {
  809. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  810. r_error.argument = 2;
  811. r_ret = Variant();
  812. } else if (p_arg_count == 2) {
  813. if (p_args[1]->get_type() != Variant::BOOL) {
  814. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  815. r_error.argument = 1;
  816. r_error.expected = Variant::BOOL;
  817. r_ret = Variant();
  818. return;
  819. }
  820. allow_objects = *p_args[1];
  821. }
  822. if (p_args[0]->get_type() != Variant::POOL_BYTE_ARRAY) {
  823. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  824. r_error.argument = 1;
  825. r_error.expected = Variant::POOL_BYTE_ARRAY;
  826. r_ret = Variant();
  827. return;
  828. }
  829. PoolByteArray varr = *p_args[0];
  830. Variant ret;
  831. {
  832. PoolByteArray::Read r = varr.read();
  833. Error err = decode_variant(ret, r.ptr(), varr.size(), nullptr, allow_objects);
  834. if (err != OK) {
  835. r_ret = RTR("Not enough bytes for decoding bytes, or invalid format.");
  836. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  837. r_error.argument = 0;
  838. r_error.expected = Variant::POOL_BYTE_ARRAY;
  839. return;
  840. }
  841. }
  842. r_ret = ret;
  843. } break;
  844. case GEN_RANGE: {
  845. switch (p_arg_count) {
  846. case 0: {
  847. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  848. r_error.argument = 1;
  849. r_ret = Variant();
  850. } break;
  851. case 1: {
  852. VALIDATE_ARG_NUM(0);
  853. int count = *p_args[0];
  854. Array arr;
  855. if (count <= 0) {
  856. r_ret = arr;
  857. return;
  858. }
  859. Error err = arr.resize(count);
  860. if (err != OK) {
  861. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  862. r_ret = Variant();
  863. return;
  864. }
  865. for (int i = 0; i < count; i++) {
  866. arr[i] = i;
  867. }
  868. r_ret = arr;
  869. } break;
  870. case 2: {
  871. VALIDATE_ARG_NUM(0);
  872. VALIDATE_ARG_NUM(1);
  873. int from = *p_args[0];
  874. int to = *p_args[1];
  875. Array arr;
  876. if (from >= to) {
  877. r_ret = arr;
  878. return;
  879. }
  880. Error err = arr.resize(to - from);
  881. if (err != OK) {
  882. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  883. r_ret = Variant();
  884. return;
  885. }
  886. for (int i = from; i < to; i++) {
  887. arr[i - from] = i;
  888. }
  889. r_ret = arr;
  890. } break;
  891. case 3: {
  892. VALIDATE_ARG_NUM(0);
  893. VALIDATE_ARG_NUM(1);
  894. VALIDATE_ARG_NUM(2);
  895. int from = *p_args[0];
  896. int to = *p_args[1];
  897. int incr = *p_args[2];
  898. if (incr == 0) {
  899. r_ret = RTR("Step argument is zero!");
  900. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  901. return;
  902. }
  903. Array arr;
  904. if (from >= to && incr > 0) {
  905. r_ret = arr;
  906. return;
  907. }
  908. if (from <= to && incr < 0) {
  909. r_ret = arr;
  910. return;
  911. }
  912. //calculate how many
  913. int count = 0;
  914. if (incr > 0) {
  915. count = ((to - from - 1) / incr) + 1;
  916. } else {
  917. count = ((from - to - 1) / -incr) + 1;
  918. }
  919. Error err = arr.resize(count);
  920. if (err != OK) {
  921. r_error.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  922. r_ret = Variant();
  923. return;
  924. }
  925. if (incr > 0) {
  926. int idx = 0;
  927. for (int i = from; i < to; i += incr) {
  928. arr[idx++] = i;
  929. }
  930. } else {
  931. int idx = 0;
  932. for (int i = from; i > to; i += incr) {
  933. arr[idx++] = i;
  934. }
  935. }
  936. r_ret = arr;
  937. } break;
  938. default: {
  939. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  940. r_error.argument = 3;
  941. r_ret = Variant();
  942. } break;
  943. }
  944. } break;
  945. case RESOURCE_LOAD: {
  946. VALIDATE_ARG_COUNT(1);
  947. if (p_args[0]->get_type() != Variant::STRING) {
  948. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  949. r_error.argument = 0;
  950. r_error.expected = Variant::STRING;
  951. r_ret = Variant();
  952. } else {
  953. r_ret = ResourceLoader::load(*p_args[0]);
  954. }
  955. } break;
  956. case INST2DICT: {
  957. VALIDATE_ARG_COUNT(1);
  958. if (p_args[0]->get_type() == Variant::NIL) {
  959. r_ret = Variant();
  960. } else if (p_args[0]->get_type() != Variant::OBJECT) {
  961. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  962. r_error.argument = 0;
  963. r_ret = Variant();
  964. } else {
  965. Object *obj = *p_args[0];
  966. if (!obj) {
  967. r_ret = Variant();
  968. } else if (!obj->get_script_instance() || obj->get_script_instance()->get_language() != GDScriptLanguage::get_singleton()) {
  969. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  970. r_error.argument = 0;
  971. r_error.expected = Variant::DICTIONARY;
  972. r_ret = RTR("Not a script with an instance");
  973. return;
  974. } else {
  975. GDScriptInstance *ins = static_cast<GDScriptInstance *>(obj->get_script_instance());
  976. Ref<GDScript> base = ins->get_script();
  977. if (base.is_null()) {
  978. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  979. r_error.argument = 0;
  980. r_error.expected = Variant::DICTIONARY;
  981. r_ret = RTR("Not based on a script");
  982. return;
  983. }
  984. GDScript *p = base.ptr();
  985. Vector<StringName> sname;
  986. while (p->_owner) {
  987. sname.push_back(p->name);
  988. p = p->_owner;
  989. }
  990. sname.invert();
  991. if (!p->path.is_resource_file()) {
  992. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  993. r_error.argument = 0;
  994. r_error.expected = Variant::DICTIONARY;
  995. r_ret = Variant();
  996. r_ret = RTR("Not based on a resource file");
  997. return;
  998. }
  999. NodePath cp(sname, Vector<StringName>(), false);
  1000. Dictionary d;
  1001. d["@subpath"] = cp;
  1002. d["@path"] = p->get_path();
  1003. for (Map<StringName, GDScript::MemberInfo>::Element *E = base->member_indices.front(); E; E = E->next()) {
  1004. if (!d.has(E->key())) {
  1005. d[E->key()] = ins->members[E->get().index];
  1006. }
  1007. }
  1008. r_ret = d;
  1009. }
  1010. }
  1011. } break;
  1012. case DICT2INST: {
  1013. VALIDATE_ARG_COUNT(1);
  1014. if (p_args[0]->get_type() != Variant::DICTIONARY) {
  1015. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1016. r_error.argument = 0;
  1017. r_error.expected = Variant::DICTIONARY;
  1018. r_ret = Variant();
  1019. return;
  1020. }
  1021. Dictionary d = *p_args[0];
  1022. if (!d.has("@path")) {
  1023. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1024. r_error.argument = 0;
  1025. r_error.expected = Variant::OBJECT;
  1026. r_ret = RTR("Invalid instance dictionary format (missing @path)");
  1027. return;
  1028. }
  1029. Ref<Script> scr = ResourceLoader::load(d["@path"]);
  1030. if (!scr.is_valid()) {
  1031. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1032. r_error.argument = 0;
  1033. r_error.expected = Variant::OBJECT;
  1034. r_ret = RTR("Invalid instance dictionary format (can't load script at @path)");
  1035. return;
  1036. }
  1037. Ref<GDScript> gdscr = scr;
  1038. if (!gdscr.is_valid()) {
  1039. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1040. r_error.argument = 0;
  1041. r_error.expected = Variant::OBJECT;
  1042. r_ret = Variant();
  1043. r_ret = RTR("Invalid instance dictionary format (invalid script at @path)");
  1044. return;
  1045. }
  1046. NodePath sub;
  1047. if (d.has("@subpath")) {
  1048. sub = d["@subpath"];
  1049. }
  1050. for (int i = 0; i < sub.get_name_count(); i++) {
  1051. gdscr = gdscr->subclasses[sub.get_name(i)];
  1052. if (!gdscr.is_valid()) {
  1053. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1054. r_error.argument = 0;
  1055. r_error.expected = Variant::OBJECT;
  1056. r_ret = Variant();
  1057. r_ret = RTR("Invalid instance dictionary (invalid subclasses)");
  1058. return;
  1059. }
  1060. }
  1061. r_ret = gdscr->_new(nullptr, -1 /*skip initializer*/, r_error);
  1062. if (r_error.error != Variant::CallError::CALL_OK) {
  1063. r_ret = Variant();
  1064. return;
  1065. }
  1066. GDScriptInstance *ins = static_cast<GDScriptInstance *>(static_cast<Object *>(r_ret)->get_script_instance());
  1067. Ref<GDScript> gd_ref = ins->get_script();
  1068. for (Map<StringName, GDScript::MemberInfo>::Element *E = gd_ref->member_indices.front(); E; E = E->next()) {
  1069. if (d.has(E->key())) {
  1070. ins->members.write[E->get().index] = d[E->key()];
  1071. }
  1072. }
  1073. } break;
  1074. case VALIDATE_JSON: {
  1075. VALIDATE_ARG_COUNT(1);
  1076. if (p_args[0]->get_type() != Variant::STRING) {
  1077. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1078. r_error.argument = 0;
  1079. r_error.expected = Variant::STRING;
  1080. r_ret = Variant();
  1081. return;
  1082. }
  1083. String errs;
  1084. int errl;
  1085. Error err = JSON::parse(*p_args[0], r_ret, errs, errl);
  1086. if (err != OK) {
  1087. r_ret = itos(errl) + ":" + errs;
  1088. } else {
  1089. r_ret = "";
  1090. }
  1091. } break;
  1092. case PARSE_JSON: {
  1093. VALIDATE_ARG_COUNT(1);
  1094. if (p_args[0]->get_type() != Variant::STRING) {
  1095. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1096. r_error.argument = 0;
  1097. r_error.expected = Variant::STRING;
  1098. r_ret = Variant();
  1099. return;
  1100. }
  1101. String errs;
  1102. int errl;
  1103. Error err = JSON::parse(*p_args[0], r_ret, errs, errl);
  1104. if (err != OK) {
  1105. r_ret = Variant();
  1106. ERR_PRINT(vformat("Error parsing JSON at line %s: %s", errl, errs));
  1107. }
  1108. } break;
  1109. case TO_JSON: {
  1110. VALIDATE_ARG_COUNT(1);
  1111. r_ret = JSON::print(*p_args[0]);
  1112. } break;
  1113. case HASH: {
  1114. VALIDATE_ARG_COUNT(1);
  1115. r_ret = p_args[0]->hash();
  1116. } break;
  1117. case COLOR8: {
  1118. if (p_arg_count < 3) {
  1119. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1120. r_error.argument = 3;
  1121. r_ret = Variant();
  1122. return;
  1123. }
  1124. if (p_arg_count > 4) {
  1125. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  1126. r_error.argument = 4;
  1127. r_ret = Variant();
  1128. return;
  1129. }
  1130. VALIDATE_ARG_NUM(0);
  1131. VALIDATE_ARG_NUM(1);
  1132. VALIDATE_ARG_NUM(2);
  1133. Color color((float)*p_args[0] / 255.0f, (float)*p_args[1] / 255.0f, (float)*p_args[2] / 255.0f);
  1134. if (p_arg_count == 4) {
  1135. VALIDATE_ARG_NUM(3);
  1136. color.a = (float)*p_args[3] / 255.0f;
  1137. }
  1138. r_ret = color;
  1139. } break;
  1140. case COLORN: {
  1141. if (p_arg_count < 1) {
  1142. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1143. r_error.argument = 1;
  1144. r_ret = Variant();
  1145. return;
  1146. }
  1147. if (p_arg_count > 2) {
  1148. r_error.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  1149. r_error.argument = 2;
  1150. r_ret = Variant();
  1151. return;
  1152. }
  1153. if (p_args[0]->get_type() != Variant::STRING) {
  1154. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1155. r_error.argument = 0;
  1156. r_ret = Variant();
  1157. } else {
  1158. Color color = Color::named(*p_args[0]);
  1159. if (p_arg_count == 2) {
  1160. VALIDATE_ARG_NUM(1);
  1161. color.a = *p_args[1];
  1162. }
  1163. r_ret = color;
  1164. }
  1165. } break;
  1166. case PRINT_STACK: {
  1167. VALIDATE_ARG_COUNT(0);
  1168. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  1169. for (int i = 0; i < script->debug_get_stack_level_count(); i++) {
  1170. print_line("Frame " + itos(i) + " - " + script->debug_get_stack_level_source(i) + ":" + itos(script->debug_get_stack_level_line(i)) + " in function '" + script->debug_get_stack_level_function(i) + "'");
  1171. };
  1172. } break;
  1173. case GET_STACK: {
  1174. VALIDATE_ARG_COUNT(0);
  1175. ScriptLanguage *script = GDScriptLanguage::get_singleton();
  1176. Array ret;
  1177. for (int i = 0; i < script->debug_get_stack_level_count(); i++) {
  1178. Dictionary frame;
  1179. frame["source"] = script->debug_get_stack_level_source(i);
  1180. frame["function"] = script->debug_get_stack_level_function(i);
  1181. frame["line"] = script->debug_get_stack_level_line(i);
  1182. ret.push_back(frame);
  1183. };
  1184. r_ret = ret;
  1185. } break;
  1186. case INSTANCE_FROM_ID: {
  1187. VALIDATE_ARG_COUNT(1);
  1188. if (p_args[0]->get_type() != Variant::INT && p_args[0]->get_type() != Variant::REAL) {
  1189. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1190. r_error.argument = 0;
  1191. r_error.expected = Variant::INT;
  1192. r_ret = Variant();
  1193. break;
  1194. }
  1195. uint32_t id = *p_args[0];
  1196. r_ret = ObjectDB::get_instance(id);
  1197. } break;
  1198. case LEN: {
  1199. VALIDATE_ARG_COUNT(1);
  1200. switch (p_args[0]->get_type()) {
  1201. case Variant::STRING: {
  1202. String d = *p_args[0];
  1203. r_ret = d.length();
  1204. } break;
  1205. case Variant::DICTIONARY: {
  1206. Dictionary d = *p_args[0];
  1207. r_ret = d.size();
  1208. } break;
  1209. case Variant::ARRAY: {
  1210. Array d = *p_args[0];
  1211. r_ret = d.size();
  1212. } break;
  1213. case Variant::POOL_BYTE_ARRAY: {
  1214. PoolVector<uint8_t> d = *p_args[0];
  1215. r_ret = d.size();
  1216. } break;
  1217. case Variant::POOL_INT_ARRAY: {
  1218. PoolVector<int> d = *p_args[0];
  1219. r_ret = d.size();
  1220. } break;
  1221. case Variant::POOL_REAL_ARRAY: {
  1222. PoolVector<real_t> d = *p_args[0];
  1223. r_ret = d.size();
  1224. } break;
  1225. case Variant::POOL_STRING_ARRAY: {
  1226. PoolVector<String> d = *p_args[0];
  1227. r_ret = d.size();
  1228. } break;
  1229. case Variant::POOL_VECTOR2_ARRAY: {
  1230. PoolVector<Vector2> d = *p_args[0];
  1231. r_ret = d.size();
  1232. } break;
  1233. case Variant::POOL_VECTOR3_ARRAY: {
  1234. PoolVector<Vector3> d = *p_args[0];
  1235. r_ret = d.size();
  1236. } break;
  1237. case Variant::POOL_COLOR_ARRAY: {
  1238. PoolVector<Color> d = *p_args[0];
  1239. r_ret = d.size();
  1240. } break;
  1241. default: {
  1242. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1243. r_error.argument = 0;
  1244. r_error.expected = Variant::OBJECT;
  1245. r_ret = Variant();
  1246. r_ret = RTR("Object can't provide a length.");
  1247. }
  1248. }
  1249. } break;
  1250. case IS_INSTANCE_VALID: {
  1251. VALIDATE_ARG_COUNT(1);
  1252. if (p_args[0]->get_type() != Variant::OBJECT) {
  1253. r_ret = false;
  1254. } else {
  1255. Object *obj = *p_args[0];
  1256. r_ret = obj != nullptr;
  1257. }
  1258. } break;
  1259. case DEEP_EQUAL: {
  1260. VALIDATE_ARG_COUNT(2);
  1261. r_ret = p_args[0]->deep_equal(*p_args[1]);
  1262. } break;
  1263. case FUNC_MAX: {
  1264. ERR_FAIL();
  1265. } break;
  1266. }
  1267. }
  1268. bool GDScriptFunctions::is_deterministic(Function p_func) {
  1269. //man i couldn't have chosen a worse function name,
  1270. //way too controversial..
  1271. switch (p_func) {
  1272. case MATH_SIN:
  1273. case MATH_COS:
  1274. case MATH_TAN:
  1275. case MATH_SINH:
  1276. case MATH_COSH:
  1277. case MATH_TANH:
  1278. case MATH_ASIN:
  1279. case MATH_ACOS:
  1280. case MATH_ATAN:
  1281. case MATH_ATAN2:
  1282. case MATH_SQRT:
  1283. case MATH_FMOD:
  1284. case MATH_FPOSMOD:
  1285. case MATH_POSMOD:
  1286. case MATH_FLOOR:
  1287. case MATH_CEIL:
  1288. case MATH_ROUND:
  1289. case MATH_ABS:
  1290. case MATH_SIGN:
  1291. case MATH_POW:
  1292. case MATH_LOG:
  1293. case MATH_EXP:
  1294. case MATH_ISNAN:
  1295. case MATH_ISINF:
  1296. case MATH_EASE:
  1297. case MATH_DECIMALS:
  1298. case MATH_STEP_DECIMALS:
  1299. case MATH_STEPIFY:
  1300. case MATH_LERP:
  1301. case MATH_INVERSE_LERP:
  1302. case MATH_RANGE_LERP:
  1303. case MATH_SMOOTHSTEP:
  1304. case MATH_MOVE_TOWARD:
  1305. case MATH_DECTIME:
  1306. case MATH_DEG2RAD:
  1307. case MATH_RAD2DEG:
  1308. case MATH_LINEAR2DB:
  1309. case MATH_DB2LINEAR:
  1310. case MATH_POLAR2CARTESIAN:
  1311. case MATH_CARTESIAN2POLAR:
  1312. case MATH_WRAP:
  1313. case MATH_WRAPF:
  1314. case LOGIC_MAX:
  1315. case LOGIC_MIN:
  1316. case LOGIC_CLAMP:
  1317. case LOGIC_NEAREST_PO2:
  1318. case TYPE_CONVERT:
  1319. case TYPE_OF:
  1320. case TYPE_EXISTS:
  1321. case TEXT_CHAR:
  1322. case TEXT_ORD:
  1323. case TEXT_STR:
  1324. case COLOR8:
  1325. case LEN:
  1326. // enable for debug only, otherwise not desirable - case GEN_RANGE:
  1327. return true;
  1328. default:
  1329. return false;
  1330. }
  1331. return false;
  1332. }
  1333. MethodInfo GDScriptFunctions::get_info(Function p_func) {
  1334. #ifdef DEBUG_ENABLED
  1335. #define ARGNAME(m_name) m_name
  1336. #else
  1337. #define ARGNAME(m_name) ""
  1338. #endif
  1339. //using a switch, so the compiler generates a jumptable
  1340. switch (p_func) {
  1341. case MATH_SIN: {
  1342. MethodInfo mi("sin", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1343. mi.return_val.type = Variant::REAL;
  1344. return mi;
  1345. } break;
  1346. case MATH_COS: {
  1347. MethodInfo mi("cos", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1348. mi.return_val.type = Variant::REAL;
  1349. return mi;
  1350. } break;
  1351. case MATH_TAN: {
  1352. MethodInfo mi("tan", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1353. mi.return_val.type = Variant::REAL;
  1354. return mi;
  1355. } break;
  1356. case MATH_SINH: {
  1357. MethodInfo mi("sinh", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1358. mi.return_val.type = Variant::REAL;
  1359. return mi;
  1360. } break;
  1361. case MATH_COSH: {
  1362. MethodInfo mi("cosh", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1363. mi.return_val.type = Variant::REAL;
  1364. return mi;
  1365. } break;
  1366. case MATH_TANH: {
  1367. MethodInfo mi("tanh", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1368. mi.return_val.type = Variant::REAL;
  1369. return mi;
  1370. } break;
  1371. case MATH_ASIN: {
  1372. MethodInfo mi("asin", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1373. mi.return_val.type = Variant::REAL;
  1374. return mi;
  1375. } break;
  1376. case MATH_ACOS: {
  1377. MethodInfo mi("acos", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1378. mi.return_val.type = Variant::REAL;
  1379. return mi;
  1380. } break;
  1381. case MATH_ATAN: {
  1382. MethodInfo mi("atan", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1383. mi.return_val.type = Variant::REAL;
  1384. return mi;
  1385. } break;
  1386. case MATH_ATAN2: {
  1387. MethodInfo mi("atan2", PropertyInfo(Variant::REAL, ARGNAME("y")), PropertyInfo(Variant::REAL, ARGNAME("x")));
  1388. mi.return_val.type = Variant::REAL;
  1389. return mi;
  1390. } break;
  1391. case MATH_SQRT: {
  1392. MethodInfo mi("sqrt", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1393. mi.return_val.type = Variant::REAL;
  1394. return mi;
  1395. } break;
  1396. case MATH_FMOD: {
  1397. MethodInfo mi("fmod", PropertyInfo(Variant::REAL, ARGNAME("a")), PropertyInfo(Variant::REAL, ARGNAME("b")));
  1398. mi.return_val.type = Variant::REAL;
  1399. return mi;
  1400. } break;
  1401. case MATH_FPOSMOD: {
  1402. MethodInfo mi("fposmod", PropertyInfo(Variant::REAL, ARGNAME("a")), PropertyInfo(Variant::REAL, ARGNAME("b")));
  1403. mi.return_val.type = Variant::REAL;
  1404. return mi;
  1405. } break;
  1406. case MATH_POSMOD: {
  1407. MethodInfo mi("posmod", PropertyInfo(Variant::INT, ARGNAME("a")), PropertyInfo(Variant::INT, ARGNAME("b")));
  1408. mi.return_val.type = Variant::INT;
  1409. return mi;
  1410. } break;
  1411. case MATH_FLOOR: {
  1412. MethodInfo mi("floor", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1413. mi.return_val.type = Variant::REAL;
  1414. return mi;
  1415. } break;
  1416. case MATH_CEIL: {
  1417. MethodInfo mi("ceil", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1418. mi.return_val.type = Variant::REAL;
  1419. return mi;
  1420. } break;
  1421. case MATH_ROUND: {
  1422. MethodInfo mi("round", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1423. mi.return_val.type = Variant::REAL;
  1424. return mi;
  1425. } break;
  1426. case MATH_ABS: {
  1427. MethodInfo mi("abs", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1428. mi.return_val.type = Variant::REAL;
  1429. return mi;
  1430. } break;
  1431. case MATH_SIGN: {
  1432. MethodInfo mi("sign", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1433. mi.return_val.type = Variant::REAL;
  1434. return mi;
  1435. } break;
  1436. case MATH_POW: {
  1437. MethodInfo mi("pow", PropertyInfo(Variant::REAL, ARGNAME("base")), PropertyInfo(Variant::REAL, ARGNAME("exp")));
  1438. mi.return_val.type = Variant::REAL;
  1439. return mi;
  1440. } break;
  1441. case MATH_LOG: {
  1442. MethodInfo mi("log", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1443. mi.return_val.type = Variant::REAL;
  1444. return mi;
  1445. } break;
  1446. case MATH_EXP: {
  1447. MethodInfo mi("exp", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1448. mi.return_val.type = Variant::REAL;
  1449. return mi;
  1450. } break;
  1451. case MATH_ISNAN: {
  1452. MethodInfo mi("is_nan", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1453. mi.return_val.type = Variant::BOOL;
  1454. return mi;
  1455. } break;
  1456. case MATH_ISINF: {
  1457. MethodInfo mi("is_inf", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1458. mi.return_val.type = Variant::BOOL;
  1459. return mi;
  1460. } break;
  1461. case MATH_ISEQUALAPPROX: {
  1462. MethodInfo mi("is_equal_approx", PropertyInfo(Variant::REAL, ARGNAME("a")), PropertyInfo(Variant::REAL, ARGNAME("b")));
  1463. mi.return_val.type = Variant::BOOL;
  1464. return mi;
  1465. } break;
  1466. case MATH_ISZEROAPPROX: {
  1467. MethodInfo mi("is_zero_approx", PropertyInfo(Variant::REAL, ARGNAME("s")));
  1468. mi.return_val.type = Variant::BOOL;
  1469. return mi;
  1470. } break;
  1471. case MATH_EASE: {
  1472. MethodInfo mi("ease", PropertyInfo(Variant::REAL, ARGNAME("s")), PropertyInfo(Variant::REAL, ARGNAME("curve")));
  1473. mi.return_val.type = Variant::REAL;
  1474. return mi;
  1475. } break;
  1476. case MATH_DECIMALS: {
  1477. MethodInfo mi("decimals", PropertyInfo(Variant::REAL, ARGNAME("step")));
  1478. mi.return_val.type = Variant::INT;
  1479. return mi;
  1480. } break;
  1481. case MATH_STEP_DECIMALS: {
  1482. MethodInfo mi("step_decimals", PropertyInfo(Variant::REAL, ARGNAME("step")));
  1483. mi.return_val.type = Variant::INT;
  1484. return mi;
  1485. } break;
  1486. case MATH_STEPIFY: {
  1487. MethodInfo mi("stepify", PropertyInfo(Variant::REAL, ARGNAME("s")), PropertyInfo(Variant::REAL, ARGNAME("step")));
  1488. mi.return_val.type = Variant::REAL;
  1489. return mi;
  1490. } break;
  1491. case MATH_LERP: {
  1492. MethodInfo mi("lerp", PropertyInfo(Variant::NIL, ARGNAME("from")), PropertyInfo(Variant::NIL, ARGNAME("to")), PropertyInfo(Variant::REAL, ARGNAME("weight")));
  1493. mi.return_val.type = Variant::NIL;
  1494. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1495. return mi;
  1496. } break;
  1497. case MATH_LERP_ANGLE: {
  1498. MethodInfo mi("lerp_angle", PropertyInfo(Variant::REAL, ARGNAME("from")), PropertyInfo(Variant::REAL, ARGNAME("to")), PropertyInfo(Variant::REAL, ARGNAME("weight")));
  1499. mi.return_val.type = Variant::REAL;
  1500. return mi;
  1501. } break;
  1502. case MATH_INVERSE_LERP: {
  1503. MethodInfo mi("inverse_lerp", PropertyInfo(Variant::REAL, ARGNAME("from")), PropertyInfo(Variant::REAL, ARGNAME("to")), PropertyInfo(Variant::REAL, ARGNAME("weight")));
  1504. mi.return_val.type = Variant::REAL;
  1505. return mi;
  1506. } break;
  1507. case MATH_RANGE_LERP: {
  1508. MethodInfo mi("range_lerp", PropertyInfo(Variant::REAL, ARGNAME("value")), PropertyInfo(Variant::REAL, ARGNAME("istart")), PropertyInfo(Variant::REAL, ARGNAME("istop")), PropertyInfo(Variant::REAL, ARGNAME("ostart")), PropertyInfo(Variant::REAL, ARGNAME("ostop")));
  1509. mi.return_val.type = Variant::REAL;
  1510. return mi;
  1511. } break;
  1512. case MATH_SMOOTHSTEP: {
  1513. MethodInfo mi("smoothstep", PropertyInfo(Variant::REAL, ARGNAME("from")), PropertyInfo(Variant::REAL, ARGNAME("to")), PropertyInfo(Variant::REAL, ARGNAME("s")));
  1514. mi.return_val.type = Variant::REAL;
  1515. return mi;
  1516. } break;
  1517. case MATH_MOVE_TOWARD: {
  1518. MethodInfo mi("move_toward", PropertyInfo(Variant::REAL, ARGNAME("from")), PropertyInfo(Variant::REAL, ARGNAME("to")), PropertyInfo(Variant::REAL, ARGNAME("delta")));
  1519. mi.return_val.type = Variant::REAL;
  1520. return mi;
  1521. } break;
  1522. case MATH_DECTIME: {
  1523. MethodInfo mi("dectime", PropertyInfo(Variant::REAL, ARGNAME("value")), PropertyInfo(Variant::REAL, ARGNAME("amount")), PropertyInfo(Variant::REAL, ARGNAME("step")));
  1524. mi.return_val.type = Variant::REAL;
  1525. return mi;
  1526. } break;
  1527. case MATH_RANDOMIZE: {
  1528. MethodInfo mi("randomize");
  1529. mi.return_val.type = Variant::NIL;
  1530. return mi;
  1531. } break;
  1532. case MATH_RAND: {
  1533. MethodInfo mi("randi");
  1534. mi.return_val.type = Variant::INT;
  1535. return mi;
  1536. } break;
  1537. case MATH_RANDF: {
  1538. MethodInfo mi("randf");
  1539. mi.return_val.type = Variant::REAL;
  1540. return mi;
  1541. } break;
  1542. case MATH_RANDOM: {
  1543. MethodInfo mi("rand_range", PropertyInfo(Variant::REAL, ARGNAME("from")), PropertyInfo(Variant::REAL, ARGNAME("to")));
  1544. mi.return_val.type = Variant::REAL;
  1545. return mi;
  1546. } break;
  1547. case MATH_SEED: {
  1548. MethodInfo mi("seed", PropertyInfo(Variant::INT, ARGNAME("seed")));
  1549. mi.return_val.type = Variant::NIL;
  1550. return mi;
  1551. } break;
  1552. case MATH_RANDSEED: {
  1553. MethodInfo mi("rand_seed", PropertyInfo(Variant::INT, ARGNAME("seed")));
  1554. mi.return_val.type = Variant::ARRAY;
  1555. return mi;
  1556. } break;
  1557. case MATH_DEG2RAD: {
  1558. MethodInfo mi("deg2rad", PropertyInfo(Variant::REAL, ARGNAME("deg")));
  1559. mi.return_val.type = Variant::REAL;
  1560. return mi;
  1561. } break;
  1562. case MATH_RAD2DEG: {
  1563. MethodInfo mi("rad2deg", PropertyInfo(Variant::REAL, ARGNAME("rad")));
  1564. mi.return_val.type = Variant::REAL;
  1565. return mi;
  1566. } break;
  1567. case MATH_LINEAR2DB: {
  1568. MethodInfo mi("linear2db", PropertyInfo(Variant::REAL, ARGNAME("nrg")));
  1569. mi.return_val.type = Variant::REAL;
  1570. return mi;
  1571. } break;
  1572. case MATH_DB2LINEAR: {
  1573. MethodInfo mi("db2linear", PropertyInfo(Variant::REAL, ARGNAME("db")));
  1574. mi.return_val.type = Variant::REAL;
  1575. return mi;
  1576. } break;
  1577. case MATH_POLAR2CARTESIAN: {
  1578. MethodInfo mi("polar2cartesian", PropertyInfo(Variant::REAL, ARGNAME("r")), PropertyInfo(Variant::REAL, ARGNAME("th")));
  1579. mi.return_val.type = Variant::VECTOR2;
  1580. return mi;
  1581. } break;
  1582. case MATH_CARTESIAN2POLAR: {
  1583. MethodInfo mi("cartesian2polar", PropertyInfo(Variant::REAL, ARGNAME("x")), PropertyInfo(Variant::REAL, ARGNAME("y")));
  1584. mi.return_val.type = Variant::VECTOR2;
  1585. return mi;
  1586. } break;
  1587. case MATH_WRAP: {
  1588. MethodInfo mi("wrapi", PropertyInfo(Variant::INT, ARGNAME("value")), PropertyInfo(Variant::INT, ARGNAME("min")), PropertyInfo(Variant::INT, ARGNAME("max")));
  1589. mi.return_val.type = Variant::INT;
  1590. return mi;
  1591. } break;
  1592. case MATH_WRAPF: {
  1593. MethodInfo mi("wrapf", PropertyInfo(Variant::REAL, ARGNAME("value")), PropertyInfo(Variant::REAL, ARGNAME("min")), PropertyInfo(Variant::REAL, ARGNAME("max")));
  1594. mi.return_val.type = Variant::REAL;
  1595. return mi;
  1596. } break;
  1597. case LOGIC_MAX: {
  1598. MethodInfo mi("max", PropertyInfo(Variant::REAL, ARGNAME("a")), PropertyInfo(Variant::REAL, ARGNAME("b")));
  1599. mi.return_val.type = Variant::REAL;
  1600. return mi;
  1601. } break;
  1602. case LOGIC_MIN: {
  1603. MethodInfo mi("min", PropertyInfo(Variant::REAL, ARGNAME("a")), PropertyInfo(Variant::REAL, ARGNAME("b")));
  1604. mi.return_val.type = Variant::REAL;
  1605. return mi;
  1606. } break;
  1607. case LOGIC_CLAMP: {
  1608. MethodInfo mi("clamp", PropertyInfo(Variant::REAL, ARGNAME("value")), PropertyInfo(Variant::REAL, ARGNAME("min")), PropertyInfo(Variant::REAL, ARGNAME("max")));
  1609. mi.return_val.type = Variant::REAL;
  1610. return mi;
  1611. } break;
  1612. case LOGIC_NEAREST_PO2: {
  1613. MethodInfo mi("nearest_po2", PropertyInfo(Variant::INT, ARGNAME("value")));
  1614. mi.return_val.type = Variant::INT;
  1615. return mi;
  1616. } break;
  1617. case OBJ_WEAKREF: {
  1618. MethodInfo mi("weakref", PropertyInfo(Variant::OBJECT, ARGNAME("obj")));
  1619. mi.return_val.type = Variant::OBJECT;
  1620. mi.return_val.class_name = "WeakRef";
  1621. return mi;
  1622. } break;
  1623. case FUNC_FUNCREF: {
  1624. MethodInfo mi("funcref", PropertyInfo(Variant::OBJECT, ARGNAME("instance")), PropertyInfo(Variant::STRING, ARGNAME("funcname")));
  1625. mi.return_val.type = Variant::OBJECT;
  1626. mi.return_val.class_name = "FuncRef";
  1627. return mi;
  1628. } break;
  1629. case TYPE_CONVERT: {
  1630. MethodInfo mi("convert", PropertyInfo(Variant::NIL, ARGNAME("what"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::INT, ARGNAME("type")));
  1631. mi.return_val.type = Variant::NIL;
  1632. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1633. return mi;
  1634. } break;
  1635. case TYPE_OF: {
  1636. MethodInfo mi("typeof", PropertyInfo(Variant::NIL, ARGNAME("what"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1637. mi.return_val.type = Variant::INT;
  1638. return mi;
  1639. } break;
  1640. case TYPE_EXISTS: {
  1641. MethodInfo mi("type_exists", PropertyInfo(Variant::STRING, ARGNAME("type")));
  1642. mi.return_val.type = Variant::BOOL;
  1643. return mi;
  1644. } break;
  1645. case TEXT_CHAR: {
  1646. MethodInfo mi("char", PropertyInfo(Variant::INT, ARGNAME("code")));
  1647. mi.return_val.type = Variant::STRING;
  1648. return mi;
  1649. } break;
  1650. case TEXT_ORD: {
  1651. MethodInfo mi("ord", PropertyInfo(Variant::STRING, ARGNAME("char")));
  1652. mi.return_val.type = Variant::INT;
  1653. return mi;
  1654. } break;
  1655. case TEXT_STR: {
  1656. MethodInfo mi("str");
  1657. mi.return_val.type = Variant::STRING;
  1658. mi.flags |= METHOD_FLAG_VARARG;
  1659. return mi;
  1660. } break;
  1661. case TEXT_PRINT: {
  1662. MethodInfo mi("print");
  1663. mi.return_val.type = Variant::NIL;
  1664. mi.flags |= METHOD_FLAG_VARARG;
  1665. return mi;
  1666. } break;
  1667. case TEXT_PRINT_TABBED: {
  1668. MethodInfo mi("printt");
  1669. mi.return_val.type = Variant::NIL;
  1670. mi.flags |= METHOD_FLAG_VARARG;
  1671. return mi;
  1672. } break;
  1673. case TEXT_PRINT_SPACED: {
  1674. MethodInfo mi("prints");
  1675. mi.return_val.type = Variant::NIL;
  1676. mi.flags |= METHOD_FLAG_VARARG;
  1677. return mi;
  1678. } break;
  1679. case TEXT_PRINTERR: {
  1680. MethodInfo mi("printerr");
  1681. mi.return_val.type = Variant::NIL;
  1682. mi.flags |= METHOD_FLAG_VARARG;
  1683. return mi;
  1684. } break;
  1685. case TEXT_PRINTRAW: {
  1686. MethodInfo mi("printraw");
  1687. mi.return_val.type = Variant::NIL;
  1688. mi.flags |= METHOD_FLAG_VARARG;
  1689. return mi;
  1690. } break;
  1691. case TEXT_PRINT_DEBUG: {
  1692. MethodInfo mi("print_debug");
  1693. mi.return_val.type = Variant::NIL;
  1694. mi.flags |= METHOD_FLAG_VARARG;
  1695. return mi;
  1696. } break;
  1697. case PUSH_ERROR: {
  1698. MethodInfo mi(Variant::NIL, "push_error", PropertyInfo(Variant::STRING, ARGNAME("message")));
  1699. mi.return_val.type = Variant::NIL;
  1700. return mi;
  1701. } break;
  1702. case PUSH_WARNING: {
  1703. MethodInfo mi(Variant::NIL, "push_warning", PropertyInfo(Variant::STRING, ARGNAME("message")));
  1704. mi.return_val.type = Variant::NIL;
  1705. return mi;
  1706. } break;
  1707. case VAR_TO_STR: {
  1708. MethodInfo mi("var2str", PropertyInfo(Variant::NIL, ARGNAME("var"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1709. mi.return_val.type = Variant::STRING;
  1710. return mi;
  1711. } break;
  1712. case STR_TO_VAR: {
  1713. MethodInfo mi(Variant::NIL, "str2var", PropertyInfo(Variant::STRING, ARGNAME("string")));
  1714. mi.return_val.type = Variant::NIL;
  1715. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1716. return mi;
  1717. } break;
  1718. case VAR_TO_BYTES: {
  1719. MethodInfo mi("var2bytes", PropertyInfo(Variant::NIL, ARGNAME("var"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::BOOL, ARGNAME("full_objects")));
  1720. mi.default_arguments.push_back(false);
  1721. mi.return_val.type = Variant::POOL_BYTE_ARRAY;
  1722. return mi;
  1723. } break;
  1724. case BYTES_TO_VAR: {
  1725. MethodInfo mi(Variant::NIL, "bytes2var", PropertyInfo(Variant::POOL_BYTE_ARRAY, ARGNAME("bytes")), PropertyInfo(Variant::BOOL, ARGNAME("allow_objects")));
  1726. mi.default_arguments.push_back(false);
  1727. mi.return_val.type = Variant::NIL;
  1728. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1729. return mi;
  1730. } break;
  1731. case GEN_RANGE: {
  1732. MethodInfo mi("range");
  1733. mi.return_val.type = Variant::ARRAY;
  1734. mi.flags |= METHOD_FLAG_VARARG;
  1735. return mi;
  1736. } break;
  1737. case RESOURCE_LOAD: {
  1738. MethodInfo mi("load", PropertyInfo(Variant::STRING, ARGNAME("path")));
  1739. mi.return_val.type = Variant::OBJECT;
  1740. mi.return_val.class_name = "Resource";
  1741. return mi;
  1742. } break;
  1743. case INST2DICT: {
  1744. MethodInfo mi("inst2dict", PropertyInfo(Variant::OBJECT, ARGNAME("inst")));
  1745. mi.return_val.type = Variant::DICTIONARY;
  1746. return mi;
  1747. } break;
  1748. case DICT2INST: {
  1749. MethodInfo mi("dict2inst", PropertyInfo(Variant::DICTIONARY, ARGNAME("dict")));
  1750. mi.return_val.type = Variant::OBJECT;
  1751. return mi;
  1752. } break;
  1753. case VALIDATE_JSON: {
  1754. MethodInfo mi("validate_json", PropertyInfo(Variant::STRING, ARGNAME("json")));
  1755. mi.return_val.type = Variant::STRING;
  1756. return mi;
  1757. } break;
  1758. case PARSE_JSON: {
  1759. MethodInfo mi(Variant::NIL, "parse_json", PropertyInfo(Variant::STRING, ARGNAME("json")));
  1760. mi.return_val.type = Variant::NIL;
  1761. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1762. return mi;
  1763. } break;
  1764. case TO_JSON: {
  1765. MethodInfo mi("to_json", PropertyInfo(Variant::NIL, ARGNAME("var"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1766. mi.return_val.type = Variant::STRING;
  1767. return mi;
  1768. } break;
  1769. case HASH: {
  1770. MethodInfo mi("hash", PropertyInfo(Variant::NIL, ARGNAME("var"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1771. mi.return_val.type = Variant::INT;
  1772. return mi;
  1773. } break;
  1774. case COLOR8: {
  1775. MethodInfo mi("Color8", PropertyInfo(Variant::INT, ARGNAME("r8")), PropertyInfo(Variant::INT, ARGNAME("g8")), PropertyInfo(Variant::INT, ARGNAME("b8")), PropertyInfo(Variant::INT, ARGNAME("a8")));
  1776. mi.default_arguments.push_back(255);
  1777. mi.return_val.type = Variant::COLOR;
  1778. return mi;
  1779. } break;
  1780. case COLORN: {
  1781. MethodInfo mi("ColorN", PropertyInfo(Variant::STRING, ARGNAME("name")), PropertyInfo(Variant::REAL, ARGNAME("alpha")));
  1782. mi.default_arguments.push_back(1.0f);
  1783. mi.return_val.type = Variant::COLOR;
  1784. return mi;
  1785. } break;
  1786. case PRINT_STACK: {
  1787. MethodInfo mi("print_stack");
  1788. mi.return_val.type = Variant::NIL;
  1789. return mi;
  1790. } break;
  1791. case GET_STACK: {
  1792. MethodInfo mi("get_stack");
  1793. mi.return_val.type = Variant::ARRAY;
  1794. return mi;
  1795. } break;
  1796. case INSTANCE_FROM_ID: {
  1797. MethodInfo mi("instance_from_id", PropertyInfo(Variant::INT, ARGNAME("instance_id")));
  1798. mi.return_val.type = Variant::OBJECT;
  1799. return mi;
  1800. } break;
  1801. case LEN: {
  1802. MethodInfo mi("len", PropertyInfo(Variant::NIL, ARGNAME("var"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT));
  1803. mi.return_val.type = Variant::INT;
  1804. return mi;
  1805. } break;
  1806. case IS_INSTANCE_VALID: {
  1807. MethodInfo mi("is_instance_valid", PropertyInfo(Variant::OBJECT, ARGNAME("instance")));
  1808. mi.return_val.type = Variant::BOOL;
  1809. return mi;
  1810. } break;
  1811. case DEEP_EQUAL: {
  1812. MethodInfo mi("deep_equal", PropertyInfo(Variant::NIL, ARGNAME("a")), PropertyInfo(Variant::NIL, ARGNAME("b")));
  1813. mi.return_val.type = Variant::BOOL;
  1814. return mi;
  1815. } break;
  1816. case FUNC_MAX: {
  1817. // Unreachable.
  1818. break;
  1819. }
  1820. }
  1821. #undef ARGNAME
  1822. MethodInfo mi;
  1823. mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1824. ERR_FAIL_V_MSG(mi, "Trying to get info of invalid builtin method.");
  1825. }