gdscript_functions.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  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, 0, 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. }