visual_script_builtin_funcs.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /**************************************************************************/
  2. /* visual_script_builtin_funcs.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 "visual_script_builtin_funcs.h"
  31. #include "core/class_db.h"
  32. #include "core/func_ref.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/os/os.h"
  36. #include "core/reference.h"
  37. #include "core/variant_parser.h"
  38. const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX] = {
  39. "sin",
  40. "cos",
  41. "tan",
  42. "sinh",
  43. "cosh",
  44. "tanh",
  45. "asin",
  46. "acos",
  47. "atan",
  48. "atan2",
  49. "sqrt",
  50. "fmod",
  51. "fposmod",
  52. "floor",
  53. "ceil",
  54. "round",
  55. "abs",
  56. "sign",
  57. "pow",
  58. "log",
  59. "exp",
  60. "is_nan",
  61. "is_inf",
  62. "ease",
  63. "decimals",
  64. "stepify",
  65. "lerp",
  66. "inverse_lerp",
  67. "range_lerp",
  68. "move_toward",
  69. "dectime",
  70. "randomize",
  71. "randi",
  72. "randf",
  73. "rand_range",
  74. "seed",
  75. "rand_seed",
  76. "deg2rad",
  77. "rad2deg",
  78. "linear2db",
  79. "db2linear",
  80. "polar2cartesian",
  81. "cartesian2polar",
  82. "wrapi",
  83. "wrapf",
  84. "max",
  85. "min",
  86. "clamp",
  87. "nearest_po2",
  88. "weakref",
  89. "funcref",
  90. "convert",
  91. "typeof",
  92. "type_exists",
  93. "char",
  94. "str",
  95. "print",
  96. "printerr",
  97. "printraw",
  98. "var2str",
  99. "str2var",
  100. "var2bytes",
  101. "bytes2var",
  102. "color_named",
  103. "smoothstep",
  104. "posmod",
  105. "lerp_angle",
  106. "ord",
  107. };
  108. VisualScriptBuiltinFunc::BuiltinFunc VisualScriptBuiltinFunc::find_function(const String &p_string) {
  109. for (int i = 0; i < FUNC_MAX; i++) {
  110. if (p_string == func_name[i]) {
  111. return BuiltinFunc(i);
  112. }
  113. }
  114. return FUNC_MAX;
  115. }
  116. String VisualScriptBuiltinFunc::get_func_name(BuiltinFunc p_func) {
  117. ERR_FAIL_INDEX_V(p_func, FUNC_MAX, String());
  118. return func_name[p_func];
  119. }
  120. int VisualScriptBuiltinFunc::get_output_sequence_port_count() const {
  121. return has_input_sequence_port() ? 1 : 0;
  122. }
  123. bool VisualScriptBuiltinFunc::has_input_sequence_port() const {
  124. switch (func) {
  125. case MATH_RANDOMIZE:
  126. case TEXT_PRINT:
  127. case TEXT_PRINTERR:
  128. case TEXT_PRINTRAW:
  129. case MATH_SEED:
  130. return true;
  131. default:
  132. return false;
  133. }
  134. }
  135. int VisualScriptBuiltinFunc::get_func_argument_count(BuiltinFunc p_func) {
  136. switch (p_func) {
  137. case MATH_RANDOMIZE:
  138. case MATH_RAND:
  139. case MATH_RANDF:
  140. return 0;
  141. case MATH_SIN:
  142. case MATH_COS:
  143. case MATH_TAN:
  144. case MATH_SINH:
  145. case MATH_COSH:
  146. case MATH_TANH:
  147. case MATH_ASIN:
  148. case MATH_ACOS:
  149. case MATH_ATAN:
  150. case MATH_SQRT:
  151. case MATH_FLOOR:
  152. case MATH_CEIL:
  153. case MATH_ROUND:
  154. case MATH_ABS:
  155. case MATH_SIGN:
  156. case MATH_LOG:
  157. case MATH_EXP:
  158. case MATH_ISNAN:
  159. case MATH_ISINF:
  160. case MATH_DECIMALS:
  161. case MATH_SEED:
  162. case MATH_RANDSEED:
  163. case MATH_DEG2RAD:
  164. case MATH_RAD2DEG:
  165. case MATH_LINEAR2DB:
  166. case MATH_DB2LINEAR:
  167. case LOGIC_NEAREST_PO2:
  168. case OBJ_WEAKREF:
  169. case TYPE_OF:
  170. case TEXT_CHAR:
  171. case TEXT_ORD:
  172. case TEXT_STR:
  173. case TEXT_PRINT:
  174. case TEXT_PRINTERR:
  175. case TEXT_PRINTRAW:
  176. case VAR_TO_STR:
  177. case STR_TO_VAR:
  178. case TYPE_EXISTS:
  179. return 1;
  180. case VAR_TO_BYTES:
  181. case BYTES_TO_VAR:
  182. case MATH_ATAN2:
  183. case MATH_FMOD:
  184. case MATH_FPOSMOD:
  185. case MATH_POSMOD:
  186. case MATH_POW:
  187. case MATH_EASE:
  188. case MATH_STEPIFY:
  189. case MATH_RANDOM:
  190. case MATH_POLAR2CARTESIAN:
  191. case MATH_CARTESIAN2POLAR:
  192. case LOGIC_MAX:
  193. case LOGIC_MIN:
  194. case FUNC_FUNCREF:
  195. case TYPE_CONVERT:
  196. case COLORN:
  197. return 2;
  198. case MATH_LERP:
  199. case MATH_LERP_ANGLE:
  200. case MATH_INVERSE_LERP:
  201. case MATH_SMOOTHSTEP:
  202. case MATH_MOVE_TOWARD:
  203. case MATH_DECTIME:
  204. case MATH_WRAP:
  205. case MATH_WRAPF:
  206. case LOGIC_CLAMP:
  207. return 3;
  208. case MATH_RANGE_LERP:
  209. return 5;
  210. case FUNC_MAX: {
  211. }
  212. }
  213. return 0;
  214. }
  215. int VisualScriptBuiltinFunc::get_input_value_port_count() const {
  216. return get_func_argument_count(func);
  217. }
  218. int VisualScriptBuiltinFunc::get_output_value_port_count() const {
  219. switch (func) {
  220. case MATH_RANDOMIZE:
  221. case TEXT_PRINT:
  222. case TEXT_PRINTERR:
  223. case TEXT_PRINTRAW:
  224. case MATH_SEED:
  225. return 0;
  226. case MATH_RANDSEED:
  227. return 2;
  228. default:
  229. return 1;
  230. }
  231. return 1;
  232. }
  233. String VisualScriptBuiltinFunc::get_output_sequence_port_text(int p_port) const {
  234. return String();
  235. }
  236. PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const {
  237. switch (func) {
  238. case MATH_SIN:
  239. case MATH_COS:
  240. case MATH_TAN:
  241. case MATH_SINH:
  242. case MATH_COSH:
  243. case MATH_TANH:
  244. case MATH_ASIN:
  245. case MATH_ACOS:
  246. case MATH_ATAN:
  247. case MATH_SQRT:
  248. case MATH_FLOOR:
  249. case MATH_CEIL:
  250. case MATH_ROUND:
  251. case MATH_ABS:
  252. case MATH_SIGN:
  253. case MATH_LOG:
  254. case MATH_EXP:
  255. case MATH_ISNAN:
  256. case MATH_ISINF: {
  257. return PropertyInfo(Variant::REAL, "s");
  258. } break;
  259. case MATH_ATAN2: {
  260. if (p_idx == 0) {
  261. return PropertyInfo(Variant::REAL, "y");
  262. } else {
  263. return PropertyInfo(Variant::REAL, "x");
  264. }
  265. } break;
  266. case MATH_FMOD:
  267. case MATH_FPOSMOD:
  268. case LOGIC_MAX:
  269. case LOGIC_MIN: {
  270. if (p_idx == 0) {
  271. return PropertyInfo(Variant::REAL, "a");
  272. } else {
  273. return PropertyInfo(Variant::REAL, "b");
  274. }
  275. } break;
  276. case MATH_POSMOD: {
  277. if (p_idx == 0) {
  278. return PropertyInfo(Variant::INT, "a");
  279. } else {
  280. return PropertyInfo(Variant::INT, "b");
  281. }
  282. } break;
  283. case MATH_POW: {
  284. if (p_idx == 0) {
  285. return PropertyInfo(Variant::REAL, "base");
  286. } else {
  287. return PropertyInfo(Variant::REAL, "exp");
  288. }
  289. } break;
  290. case MATH_EASE: {
  291. if (p_idx == 0) {
  292. return PropertyInfo(Variant::REAL, "s");
  293. } else {
  294. return PropertyInfo(Variant::REAL, "curve");
  295. }
  296. } break;
  297. case MATH_DECIMALS: {
  298. return PropertyInfo(Variant::REAL, "step");
  299. } break;
  300. case MATH_STEPIFY: {
  301. if (p_idx == 0) {
  302. return PropertyInfo(Variant::REAL, "s");
  303. } else {
  304. return PropertyInfo(Variant::REAL, "steps");
  305. }
  306. } break;
  307. case MATH_LERP:
  308. case MATH_LERP_ANGLE:
  309. case MATH_INVERSE_LERP:
  310. case MATH_SMOOTHSTEP: {
  311. if (p_idx == 0) {
  312. return PropertyInfo(Variant::REAL, "from");
  313. } else if (p_idx == 1) {
  314. return PropertyInfo(Variant::REAL, "to");
  315. } else {
  316. return PropertyInfo(Variant::REAL, "weight");
  317. }
  318. } break;
  319. case MATH_RANGE_LERP: {
  320. if (p_idx == 0) {
  321. return PropertyInfo(Variant::REAL, "value");
  322. } else if (p_idx == 1) {
  323. return PropertyInfo(Variant::REAL, "istart");
  324. } else if (p_idx == 2) {
  325. return PropertyInfo(Variant::REAL, "istop");
  326. } else if (p_idx == 3) {
  327. return PropertyInfo(Variant::REAL, "ostart");
  328. } else {
  329. return PropertyInfo(Variant::REAL, "ostop");
  330. }
  331. } break;
  332. case MATH_MOVE_TOWARD: {
  333. if (p_idx == 0) {
  334. return PropertyInfo(Variant::REAL, "from");
  335. } else if (p_idx == 1) {
  336. return PropertyInfo(Variant::REAL, "to");
  337. } else {
  338. return PropertyInfo(Variant::REAL, "delta");
  339. }
  340. } break;
  341. case MATH_DECTIME: {
  342. if (p_idx == 0) {
  343. return PropertyInfo(Variant::REAL, "value");
  344. } else if (p_idx == 1) {
  345. return PropertyInfo(Variant::REAL, "amount");
  346. } else {
  347. return PropertyInfo(Variant::REAL, "step");
  348. }
  349. } break;
  350. case MATH_RANDOMIZE:
  351. case MATH_RAND:
  352. case MATH_RANDF: {
  353. } break;
  354. case MATH_RANDOM: {
  355. if (p_idx == 0) {
  356. return PropertyInfo(Variant::REAL, "from");
  357. } else {
  358. return PropertyInfo(Variant::REAL, "to");
  359. }
  360. } break;
  361. case MATH_SEED:
  362. case MATH_RANDSEED: {
  363. return PropertyInfo(Variant::INT, "seed");
  364. } break;
  365. case MATH_DEG2RAD: {
  366. return PropertyInfo(Variant::REAL, "deg");
  367. } break;
  368. case MATH_RAD2DEG: {
  369. return PropertyInfo(Variant::REAL, "rad");
  370. } break;
  371. case MATH_LINEAR2DB: {
  372. return PropertyInfo(Variant::REAL, "nrg");
  373. } break;
  374. case MATH_DB2LINEAR: {
  375. return PropertyInfo(Variant::REAL, "db");
  376. } break;
  377. case MATH_POLAR2CARTESIAN: {
  378. if (p_idx == 0) {
  379. return PropertyInfo(Variant::REAL, "r");
  380. } else {
  381. return PropertyInfo(Variant::REAL, "th");
  382. }
  383. } break;
  384. case MATH_CARTESIAN2POLAR: {
  385. if (p_idx == 0) {
  386. return PropertyInfo(Variant::REAL, "x");
  387. } else {
  388. return PropertyInfo(Variant::REAL, "y");
  389. }
  390. } break;
  391. case MATH_WRAP: {
  392. if (p_idx == 0) {
  393. return PropertyInfo(Variant::INT, "value");
  394. } else if (p_idx == 1) {
  395. return PropertyInfo(Variant::INT, "min");
  396. } else {
  397. return PropertyInfo(Variant::INT, "max");
  398. }
  399. } break;
  400. case MATH_WRAPF:
  401. case LOGIC_CLAMP: {
  402. if (p_idx == 0) {
  403. return PropertyInfo(Variant::REAL, "value");
  404. } else if (p_idx == 1) {
  405. return PropertyInfo(Variant::REAL, "min");
  406. } else {
  407. return PropertyInfo(Variant::REAL, "max");
  408. }
  409. } break;
  410. case LOGIC_NEAREST_PO2: {
  411. return PropertyInfo(Variant::INT, "value");
  412. } break;
  413. case OBJ_WEAKREF: {
  414. return PropertyInfo(Variant::OBJECT, "source");
  415. } break;
  416. case FUNC_FUNCREF: {
  417. if (p_idx == 0) {
  418. return PropertyInfo(Variant::OBJECT, "instance");
  419. } else {
  420. return PropertyInfo(Variant::STRING, "funcname");
  421. }
  422. } break;
  423. case TYPE_CONVERT: {
  424. if (p_idx == 0) {
  425. return PropertyInfo(Variant::NIL, "what");
  426. } else {
  427. return PropertyInfo(Variant::STRING, "type");
  428. }
  429. } break;
  430. case TYPE_OF: {
  431. return PropertyInfo(Variant::NIL, "what");
  432. } break;
  433. case TYPE_EXISTS: {
  434. return PropertyInfo(Variant::STRING, "type");
  435. } break;
  436. case TEXT_ORD: {
  437. return PropertyInfo(Variant::STRING, "character");
  438. } break;
  439. case TEXT_CHAR: {
  440. return PropertyInfo(Variant::INT, "ascii");
  441. } break;
  442. case TEXT_STR:
  443. case TEXT_PRINT:
  444. case TEXT_PRINTERR:
  445. case TEXT_PRINTRAW: {
  446. return PropertyInfo(Variant::NIL, "value");
  447. } break;
  448. case STR_TO_VAR: {
  449. return PropertyInfo(Variant::STRING, "string");
  450. } break;
  451. case VAR_TO_STR:
  452. case VAR_TO_BYTES: {
  453. if (p_idx == 0) {
  454. return PropertyInfo(Variant::NIL, "var");
  455. } else {
  456. return PropertyInfo(Variant::BOOL, "full_objects");
  457. }
  458. } break;
  459. case BYTES_TO_VAR: {
  460. if (p_idx == 0) {
  461. return PropertyInfo(Variant::POOL_BYTE_ARRAY, "bytes");
  462. } else {
  463. return PropertyInfo(Variant::BOOL, "allow_objects");
  464. }
  465. } break;
  466. case COLORN: {
  467. if (p_idx == 0) {
  468. return PropertyInfo(Variant::STRING, "name");
  469. } else {
  470. return PropertyInfo(Variant::REAL, "alpha");
  471. }
  472. } break;
  473. case FUNC_MAX: {
  474. }
  475. }
  476. return PropertyInfo();
  477. }
  478. PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) const {
  479. Variant::Type t = Variant::NIL;
  480. switch (func) {
  481. case MATH_SIN:
  482. case MATH_COS:
  483. case MATH_TAN:
  484. case MATH_SINH:
  485. case MATH_COSH:
  486. case MATH_TANH:
  487. case MATH_ASIN:
  488. case MATH_ACOS:
  489. case MATH_ATAN:
  490. case MATH_ATAN2:
  491. case MATH_SQRT:
  492. case MATH_FMOD:
  493. case MATH_FPOSMOD:
  494. case MATH_FLOOR:
  495. case MATH_CEIL: {
  496. t = Variant::REAL;
  497. } break;
  498. case MATH_POSMOD: {
  499. t = Variant::INT;
  500. } break;
  501. case MATH_ROUND: {
  502. t = Variant::REAL;
  503. } break;
  504. case MATH_ABS: {
  505. t = Variant::REAL;
  506. } break;
  507. case MATH_SIGN: {
  508. t = Variant::REAL;
  509. } break;
  510. case MATH_POW:
  511. case MATH_LOG:
  512. case MATH_EXP: {
  513. t = Variant::REAL;
  514. } break;
  515. case MATH_ISNAN:
  516. case MATH_ISINF: {
  517. t = Variant::BOOL;
  518. } break;
  519. case MATH_EASE: {
  520. t = Variant::REAL;
  521. } break;
  522. case MATH_DECIMALS: {
  523. t = Variant::INT;
  524. } break;
  525. case MATH_STEPIFY:
  526. case MATH_LERP:
  527. case MATH_LERP_ANGLE:
  528. case MATH_INVERSE_LERP:
  529. case MATH_RANGE_LERP:
  530. case MATH_SMOOTHSTEP:
  531. case MATH_MOVE_TOWARD:
  532. case MATH_DECTIME: {
  533. t = Variant::REAL;
  534. } break;
  535. case MATH_RANDOMIZE: {
  536. } break;
  537. case MATH_RAND: {
  538. t = Variant::INT;
  539. } break;
  540. case MATH_RANDF:
  541. case MATH_RANDOM: {
  542. t = Variant::REAL;
  543. } break;
  544. case MATH_SEED: {
  545. } break;
  546. case MATH_RANDSEED: {
  547. if (p_idx == 0) {
  548. return PropertyInfo(Variant::INT, "rnd");
  549. } else {
  550. return PropertyInfo(Variant::INT, "seed");
  551. }
  552. } break;
  553. case MATH_DEG2RAD:
  554. case MATH_RAD2DEG:
  555. case MATH_LINEAR2DB:
  556. case MATH_WRAPF:
  557. case MATH_DB2LINEAR: {
  558. t = Variant::REAL;
  559. } break;
  560. case MATH_POLAR2CARTESIAN:
  561. case MATH_CARTESIAN2POLAR: {
  562. t = Variant::VECTOR2;
  563. } break;
  564. case MATH_WRAP: {
  565. t = Variant::INT;
  566. } break;
  567. case LOGIC_MAX:
  568. case LOGIC_MIN:
  569. case LOGIC_CLAMP: {
  570. t = Variant::REAL;
  571. } break;
  572. case LOGIC_NEAREST_PO2: {
  573. t = Variant::INT;
  574. } break;
  575. case OBJ_WEAKREF: {
  576. t = Variant::OBJECT;
  577. } break;
  578. case FUNC_FUNCREF: {
  579. t = Variant::OBJECT;
  580. } break;
  581. case TYPE_CONVERT: {
  582. } break;
  583. case TEXT_ORD:
  584. case TYPE_OF: {
  585. t = Variant::INT;
  586. } break;
  587. case TYPE_EXISTS: {
  588. t = Variant::BOOL;
  589. } break;
  590. case TEXT_CHAR:
  591. case TEXT_STR: {
  592. t = Variant::STRING;
  593. } break;
  594. case TEXT_PRINT: {
  595. } break;
  596. case TEXT_PRINTERR: {
  597. } break;
  598. case TEXT_PRINTRAW: {
  599. } break;
  600. case VAR_TO_STR: {
  601. t = Variant::STRING;
  602. } break;
  603. case STR_TO_VAR: {
  604. } break;
  605. case VAR_TO_BYTES: {
  606. if (p_idx == 0) {
  607. t = Variant::POOL_BYTE_ARRAY;
  608. } else {
  609. t = Variant::BOOL;
  610. }
  611. } break;
  612. case BYTES_TO_VAR: {
  613. if (p_idx == 1) {
  614. t = Variant::BOOL;
  615. }
  616. } break;
  617. case COLORN: {
  618. t = Variant::COLOR;
  619. } break;
  620. case FUNC_MAX: {
  621. }
  622. }
  623. return PropertyInfo(t, "");
  624. }
  625. /*
  626. String VisualScriptBuiltinFunc::get_caption() const {
  627. return "BuiltinFunc";
  628. }
  629. */
  630. String VisualScriptBuiltinFunc::get_caption() const {
  631. return func_name[func];
  632. }
  633. void VisualScriptBuiltinFunc::set_func(BuiltinFunc p_which) {
  634. ERR_FAIL_INDEX(p_which, FUNC_MAX);
  635. func = p_which;
  636. _change_notify();
  637. ports_changed_notify();
  638. }
  639. VisualScriptBuiltinFunc::BuiltinFunc VisualScriptBuiltinFunc::get_func() {
  640. return func;
  641. }
  642. #define VALIDATE_ARG_NUM(m_arg) \
  643. if (!p_inputs[m_arg]->is_num()) { \
  644. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; \
  645. r_error.argument = m_arg; \
  646. r_error.expected = Variant::REAL; \
  647. return; \
  648. }
  649. void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Variant::CallError &r_error, String &r_error_str) {
  650. switch (p_func) {
  651. case VisualScriptBuiltinFunc::MATH_SIN: {
  652. VALIDATE_ARG_NUM(0);
  653. *r_return = Math::sin((double)*p_inputs[0]);
  654. } break;
  655. case VisualScriptBuiltinFunc::MATH_COS: {
  656. VALIDATE_ARG_NUM(0);
  657. *r_return = Math::cos((double)*p_inputs[0]);
  658. } break;
  659. case VisualScriptBuiltinFunc::MATH_TAN: {
  660. VALIDATE_ARG_NUM(0);
  661. *r_return = Math::tan((double)*p_inputs[0]);
  662. } break;
  663. case VisualScriptBuiltinFunc::MATH_SINH: {
  664. VALIDATE_ARG_NUM(0);
  665. *r_return = Math::sinh((double)*p_inputs[0]);
  666. } break;
  667. case VisualScriptBuiltinFunc::MATH_COSH: {
  668. VALIDATE_ARG_NUM(0);
  669. *r_return = Math::cosh((double)*p_inputs[0]);
  670. } break;
  671. case VisualScriptBuiltinFunc::MATH_TANH: {
  672. VALIDATE_ARG_NUM(0);
  673. *r_return = Math::tanh((double)*p_inputs[0]);
  674. } break;
  675. case VisualScriptBuiltinFunc::MATH_ASIN: {
  676. VALIDATE_ARG_NUM(0);
  677. *r_return = Math::asin((double)*p_inputs[0]);
  678. } break;
  679. case VisualScriptBuiltinFunc::MATH_ACOS: {
  680. VALIDATE_ARG_NUM(0);
  681. *r_return = Math::acos((double)*p_inputs[0]);
  682. } break;
  683. case VisualScriptBuiltinFunc::MATH_ATAN: {
  684. VALIDATE_ARG_NUM(0);
  685. *r_return = Math::atan((double)*p_inputs[0]);
  686. } break;
  687. case VisualScriptBuiltinFunc::MATH_ATAN2: {
  688. VALIDATE_ARG_NUM(0);
  689. VALIDATE_ARG_NUM(1);
  690. *r_return = Math::atan2((double)*p_inputs[0], (double)*p_inputs[1]);
  691. } break;
  692. case VisualScriptBuiltinFunc::MATH_SQRT: {
  693. VALIDATE_ARG_NUM(0);
  694. *r_return = Math::sqrt((double)*p_inputs[0]);
  695. } break;
  696. case VisualScriptBuiltinFunc::MATH_FMOD: {
  697. VALIDATE_ARG_NUM(0);
  698. VALIDATE_ARG_NUM(1);
  699. *r_return = Math::fmod((double)*p_inputs[0], (double)*p_inputs[1]);
  700. } break;
  701. case VisualScriptBuiltinFunc::MATH_FPOSMOD: {
  702. VALIDATE_ARG_NUM(0);
  703. VALIDATE_ARG_NUM(1);
  704. *r_return = Math::fposmod((double)*p_inputs[0], (double)*p_inputs[1]);
  705. } break;
  706. case VisualScriptBuiltinFunc::MATH_POSMOD: {
  707. VALIDATE_ARG_NUM(0);
  708. VALIDATE_ARG_NUM(1);
  709. *r_return = Math::posmod((int64_t)*p_inputs[0], (int64_t)*p_inputs[1]);
  710. } break;
  711. case VisualScriptBuiltinFunc::MATH_FLOOR: {
  712. VALIDATE_ARG_NUM(0);
  713. *r_return = Math::floor((double)*p_inputs[0]);
  714. } break;
  715. case VisualScriptBuiltinFunc::MATH_CEIL: {
  716. VALIDATE_ARG_NUM(0);
  717. *r_return = Math::ceil((double)*p_inputs[0]);
  718. } break;
  719. case VisualScriptBuiltinFunc::MATH_ROUND: {
  720. VALIDATE_ARG_NUM(0);
  721. *r_return = Math::round((double)*p_inputs[0]);
  722. } break;
  723. case VisualScriptBuiltinFunc::MATH_ABS: {
  724. if (p_inputs[0]->get_type() == Variant::INT) {
  725. int64_t i = *p_inputs[0];
  726. *r_return = ABS(i);
  727. } else if (p_inputs[0]->get_type() == Variant::REAL) {
  728. real_t r = *p_inputs[0];
  729. *r_return = Math::abs(r);
  730. } else {
  731. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  732. r_error.argument = 0;
  733. r_error.expected = Variant::REAL;
  734. }
  735. } break;
  736. case VisualScriptBuiltinFunc::MATH_SIGN: {
  737. if (p_inputs[0]->get_type() == Variant::INT) {
  738. int64_t i = *p_inputs[0];
  739. *r_return = i < 0 ? -1 : (i > 0 ? +1 : 0);
  740. } else if (p_inputs[0]->get_type() == Variant::REAL) {
  741. real_t r = *p_inputs[0];
  742. *r_return = r < 0.0 ? -1.0 : (r > 0.0 ? +1.0 : 0.0);
  743. } else {
  744. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  745. r_error.argument = 0;
  746. r_error.expected = Variant::REAL;
  747. }
  748. } break;
  749. case VisualScriptBuiltinFunc::MATH_POW: {
  750. VALIDATE_ARG_NUM(0);
  751. VALIDATE_ARG_NUM(1);
  752. *r_return = Math::pow((double)*p_inputs[0], (double)*p_inputs[1]);
  753. } break;
  754. case VisualScriptBuiltinFunc::MATH_LOG: {
  755. VALIDATE_ARG_NUM(0);
  756. *r_return = Math::log((double)*p_inputs[0]);
  757. } break;
  758. case VisualScriptBuiltinFunc::MATH_EXP: {
  759. VALIDATE_ARG_NUM(0);
  760. *r_return = Math::exp((double)*p_inputs[0]);
  761. } break;
  762. case VisualScriptBuiltinFunc::MATH_ISNAN: {
  763. VALIDATE_ARG_NUM(0);
  764. *r_return = Math::is_nan((double)*p_inputs[0]);
  765. } break;
  766. case VisualScriptBuiltinFunc::MATH_ISINF: {
  767. VALIDATE_ARG_NUM(0);
  768. *r_return = Math::is_inf((double)*p_inputs[0]);
  769. } break;
  770. case VisualScriptBuiltinFunc::MATH_EASE: {
  771. VALIDATE_ARG_NUM(0);
  772. VALIDATE_ARG_NUM(1);
  773. *r_return = Math::ease((double)*p_inputs[0], (double)*p_inputs[1]);
  774. } break;
  775. case VisualScriptBuiltinFunc::MATH_DECIMALS: {
  776. VALIDATE_ARG_NUM(0);
  777. *r_return = Math::step_decimals((double)*p_inputs[0]);
  778. } break;
  779. case VisualScriptBuiltinFunc::MATH_STEPIFY: {
  780. VALIDATE_ARG_NUM(0);
  781. VALIDATE_ARG_NUM(1);
  782. *r_return = Math::stepify((double)*p_inputs[0], (double)*p_inputs[1]);
  783. } break;
  784. case VisualScriptBuiltinFunc::MATH_LERP: {
  785. VALIDATE_ARG_NUM(0);
  786. VALIDATE_ARG_NUM(1);
  787. VALIDATE_ARG_NUM(2);
  788. *r_return = Math::lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  789. } break;
  790. case VisualScriptBuiltinFunc::MATH_LERP_ANGLE: {
  791. VALIDATE_ARG_NUM(0);
  792. VALIDATE_ARG_NUM(1);
  793. VALIDATE_ARG_NUM(2);
  794. *r_return = Math::lerp_angle((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  795. } break;
  796. case VisualScriptBuiltinFunc::MATH_INVERSE_LERP: {
  797. VALIDATE_ARG_NUM(0);
  798. VALIDATE_ARG_NUM(1);
  799. VALIDATE_ARG_NUM(2);
  800. *r_return = Math::inverse_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  801. } break;
  802. case VisualScriptBuiltinFunc::MATH_RANGE_LERP: {
  803. VALIDATE_ARG_NUM(0);
  804. VALIDATE_ARG_NUM(1);
  805. VALIDATE_ARG_NUM(2);
  806. VALIDATE_ARG_NUM(3);
  807. VALIDATE_ARG_NUM(4);
  808. *r_return = Math::range_lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2], (double)*p_inputs[3], (double)*p_inputs[4]);
  809. } break;
  810. case VisualScriptBuiltinFunc::MATH_SMOOTHSTEP: {
  811. VALIDATE_ARG_NUM(0);
  812. VALIDATE_ARG_NUM(1);
  813. VALIDATE_ARG_NUM(2);
  814. *r_return = Math::smoothstep((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  815. } break;
  816. case VisualScriptBuiltinFunc::MATH_MOVE_TOWARD: {
  817. VALIDATE_ARG_NUM(0);
  818. VALIDATE_ARG_NUM(1);
  819. VALIDATE_ARG_NUM(2);
  820. *r_return = Math::move_toward((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  821. } break;
  822. case VisualScriptBuiltinFunc::MATH_DECTIME: {
  823. VALIDATE_ARG_NUM(0);
  824. VALIDATE_ARG_NUM(1);
  825. VALIDATE_ARG_NUM(2);
  826. *r_return = Math::dectime((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  827. } break;
  828. case VisualScriptBuiltinFunc::MATH_RANDOMIZE: {
  829. Math::randomize();
  830. } break;
  831. case VisualScriptBuiltinFunc::MATH_RAND: {
  832. *r_return = Math::rand();
  833. } break;
  834. case VisualScriptBuiltinFunc::MATH_RANDF: {
  835. *r_return = Math::randf();
  836. } break;
  837. case VisualScriptBuiltinFunc::MATH_RANDOM: {
  838. VALIDATE_ARG_NUM(0);
  839. VALIDATE_ARG_NUM(1);
  840. *r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]);
  841. } break;
  842. case VisualScriptBuiltinFunc::MATH_SEED: {
  843. VALIDATE_ARG_NUM(0);
  844. uint64_t seed = *p_inputs[0];
  845. Math::seed(seed);
  846. } break;
  847. case VisualScriptBuiltinFunc::MATH_RANDSEED: {
  848. VALIDATE_ARG_NUM(0);
  849. uint64_t seed = *p_inputs[0];
  850. int ret = Math::rand_from_seed(&seed);
  851. Array reta;
  852. reta.push_back(ret);
  853. reta.push_back(seed);
  854. *r_return = reta;
  855. } break;
  856. case VisualScriptBuiltinFunc::MATH_DEG2RAD: {
  857. VALIDATE_ARG_NUM(0);
  858. *r_return = Math::deg2rad((double)*p_inputs[0]);
  859. } break;
  860. case VisualScriptBuiltinFunc::MATH_RAD2DEG: {
  861. VALIDATE_ARG_NUM(0);
  862. *r_return = Math::rad2deg((double)*p_inputs[0]);
  863. } break;
  864. case VisualScriptBuiltinFunc::MATH_LINEAR2DB: {
  865. VALIDATE_ARG_NUM(0);
  866. *r_return = Math::linear2db((double)*p_inputs[0]);
  867. } break;
  868. case VisualScriptBuiltinFunc::MATH_DB2LINEAR: {
  869. VALIDATE_ARG_NUM(0);
  870. *r_return = Math::db2linear((double)*p_inputs[0]);
  871. } break;
  872. case VisualScriptBuiltinFunc::MATH_POLAR2CARTESIAN: {
  873. VALIDATE_ARG_NUM(0);
  874. VALIDATE_ARG_NUM(1);
  875. double r = *p_inputs[0];
  876. double th = *p_inputs[1];
  877. *r_return = Vector2(r * Math::cos(th), r * Math::sin(th));
  878. } break;
  879. case VisualScriptBuiltinFunc::MATH_CARTESIAN2POLAR: {
  880. VALIDATE_ARG_NUM(0);
  881. VALIDATE_ARG_NUM(1);
  882. double x = *p_inputs[0];
  883. double y = *p_inputs[1];
  884. *r_return = Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x));
  885. } break;
  886. case VisualScriptBuiltinFunc::MATH_WRAP: {
  887. VALIDATE_ARG_NUM(0);
  888. VALIDATE_ARG_NUM(1);
  889. VALIDATE_ARG_NUM(2);
  890. *r_return = Math::wrapi((int64_t)*p_inputs[0], (int64_t)*p_inputs[1], (int64_t)*p_inputs[2]);
  891. } break;
  892. case VisualScriptBuiltinFunc::MATH_WRAPF: {
  893. VALIDATE_ARG_NUM(0);
  894. VALIDATE_ARG_NUM(1);
  895. VALIDATE_ARG_NUM(2);
  896. *r_return = Math::wrapf((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
  897. } break;
  898. case VisualScriptBuiltinFunc::LOGIC_MAX: {
  899. if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) {
  900. int64_t a = *p_inputs[0];
  901. int64_t b = *p_inputs[1];
  902. *r_return = MAX(a, b);
  903. } else {
  904. VALIDATE_ARG_NUM(0);
  905. VALIDATE_ARG_NUM(1);
  906. real_t a = *p_inputs[0];
  907. real_t b = *p_inputs[1];
  908. *r_return = MAX(a, b);
  909. }
  910. } break;
  911. case VisualScriptBuiltinFunc::LOGIC_MIN: {
  912. if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT) {
  913. int64_t a = *p_inputs[0];
  914. int64_t b = *p_inputs[1];
  915. *r_return = MIN(a, b);
  916. } else {
  917. VALIDATE_ARG_NUM(0);
  918. VALIDATE_ARG_NUM(1);
  919. real_t a = *p_inputs[0];
  920. real_t b = *p_inputs[1];
  921. *r_return = MIN(a, b);
  922. }
  923. } break;
  924. case VisualScriptBuiltinFunc::LOGIC_CLAMP: {
  925. if (p_inputs[0]->get_type() == Variant::INT && p_inputs[1]->get_type() == Variant::INT && p_inputs[2]->get_type() == Variant::INT) {
  926. int64_t a = *p_inputs[0];
  927. int64_t b = *p_inputs[1];
  928. int64_t c = *p_inputs[2];
  929. *r_return = CLAMP(a, b, c);
  930. } else {
  931. VALIDATE_ARG_NUM(0);
  932. VALIDATE_ARG_NUM(1);
  933. VALIDATE_ARG_NUM(2);
  934. real_t a = *p_inputs[0];
  935. real_t b = *p_inputs[1];
  936. real_t c = *p_inputs[2];
  937. *r_return = CLAMP(a, b, c);
  938. }
  939. } break;
  940. case VisualScriptBuiltinFunc::LOGIC_NEAREST_PO2: {
  941. VALIDATE_ARG_NUM(0);
  942. int64_t num = *p_inputs[0];
  943. *r_return = next_power_of_2(num);
  944. } break;
  945. case VisualScriptBuiltinFunc::OBJ_WEAKREF: {
  946. if (p_inputs[0]->get_type() != Variant::OBJECT) {
  947. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  948. r_error.argument = 0;
  949. r_error.expected = Variant::OBJECT;
  950. return;
  951. }
  952. if (p_inputs[0]->is_ref()) {
  953. REF r = *p_inputs[0];
  954. if (!r.is_valid()) {
  955. return;
  956. }
  957. Ref<WeakRef> wref = memnew(WeakRef);
  958. wref->set_ref(r);
  959. *r_return = wref;
  960. } else {
  961. Object *obj = *p_inputs[0];
  962. if (!obj) {
  963. return;
  964. }
  965. Ref<WeakRef> wref = memnew(WeakRef);
  966. wref->set_obj(obj);
  967. *r_return = wref;
  968. }
  969. } break;
  970. case VisualScriptBuiltinFunc::FUNC_FUNCREF: {
  971. if (p_inputs[0]->get_type() != Variant::OBJECT) {
  972. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  973. r_error.argument = 0;
  974. r_error.expected = Variant::OBJECT;
  975. return;
  976. }
  977. if (p_inputs[1]->get_type() != Variant::STRING && p_inputs[1]->get_type() != Variant::NODE_PATH) {
  978. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  979. r_error.argument = 1;
  980. r_error.expected = Variant::STRING;
  981. return;
  982. }
  983. Ref<FuncRef> fr = memnew(FuncRef);
  984. fr->set_instance(*p_inputs[0]);
  985. fr->set_function(*p_inputs[1]);
  986. *r_return = fr;
  987. } break;
  988. case VisualScriptBuiltinFunc::TYPE_CONVERT: {
  989. VALIDATE_ARG_NUM(1);
  990. int type = *p_inputs[1];
  991. if (type < 0 || type >= Variant::VARIANT_MAX) {
  992. r_error_str = RTR("Invalid type argument to convert(), use TYPE_* constants.");
  993. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  994. r_error.argument = 0;
  995. r_error.expected = Variant::INT;
  996. return;
  997. } else {
  998. *r_return = Variant::construct(Variant::Type(type), p_inputs, 1, r_error);
  999. }
  1000. } break;
  1001. case VisualScriptBuiltinFunc::TYPE_OF: {
  1002. *r_return = p_inputs[0]->get_type();
  1003. } break;
  1004. case VisualScriptBuiltinFunc::TYPE_EXISTS: {
  1005. *r_return = ClassDB::class_exists(*p_inputs[0]);
  1006. } break;
  1007. case VisualScriptBuiltinFunc::TEXT_CHAR: {
  1008. CharType result[2] = { *p_inputs[0], 0 };
  1009. *r_return = String(result);
  1010. } break;
  1011. case VisualScriptBuiltinFunc::TEXT_ORD: {
  1012. if (p_inputs[0]->get_type() != Variant::STRING) {
  1013. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1014. r_error.argument = 0;
  1015. r_error.expected = Variant::STRING;
  1016. return;
  1017. }
  1018. String str = p_inputs[0]->operator String();
  1019. if (str.length() != 1) {
  1020. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1021. r_error.argument = 0;
  1022. r_error.expected = Variant::STRING;
  1023. *r_return = "Expected a string of length 1 (a character).";
  1024. return;
  1025. }
  1026. *r_return = str.get(0);
  1027. } break;
  1028. case VisualScriptBuiltinFunc::TEXT_STR: {
  1029. String str = *p_inputs[0];
  1030. *r_return = str;
  1031. } break;
  1032. case VisualScriptBuiltinFunc::TEXT_PRINT: {
  1033. String str = *p_inputs[0];
  1034. print_line(str);
  1035. } break;
  1036. case VisualScriptBuiltinFunc::TEXT_PRINTERR: {
  1037. String str = *p_inputs[0];
  1038. print_error(str);
  1039. } break;
  1040. case VisualScriptBuiltinFunc::TEXT_PRINTRAW: {
  1041. String str = *p_inputs[0];
  1042. OS::get_singleton()->print("%s", str.utf8().get_data());
  1043. } break;
  1044. case VisualScriptBuiltinFunc::VAR_TO_STR: {
  1045. String vars;
  1046. VariantWriter::write_to_string(*p_inputs[0], vars);
  1047. *r_return = vars;
  1048. } break;
  1049. case VisualScriptBuiltinFunc::STR_TO_VAR: {
  1050. if (p_inputs[0]->get_type() != Variant::STRING) {
  1051. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1052. r_error.argument = 0;
  1053. r_error.expected = Variant::STRING;
  1054. return;
  1055. }
  1056. VariantParser::StreamString ss;
  1057. ss.s = *p_inputs[0];
  1058. String errs;
  1059. int line;
  1060. Error err = VariantParser::parse(&ss, *r_return, errs, line);
  1061. if (err != OK) {
  1062. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1063. r_error.argument = 0;
  1064. r_error.expected = Variant::STRING;
  1065. *r_return = "Parse error at line " + itos(line) + ": " + errs;
  1066. return;
  1067. }
  1068. } break;
  1069. case VisualScriptBuiltinFunc::VAR_TO_BYTES: {
  1070. if (p_inputs[1]->get_type() != Variant::BOOL) {
  1071. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1072. r_error.argument = 1;
  1073. r_error.expected = Variant::BOOL;
  1074. return;
  1075. }
  1076. PoolByteArray barr;
  1077. int len;
  1078. bool full_objects = *p_inputs[1];
  1079. Error err = encode_variant(*p_inputs[0], nullptr, len, full_objects);
  1080. if (err) {
  1081. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1082. r_error.argument = 0;
  1083. r_error.expected = Variant::NIL;
  1084. r_error_str = "Unexpected error encoding variable to bytes, likely unserializable type found (Object or RID).";
  1085. return;
  1086. }
  1087. barr.resize(len);
  1088. {
  1089. PoolByteArray::Write w = barr.write();
  1090. encode_variant(*p_inputs[0], w.ptr(), len, full_objects);
  1091. }
  1092. *r_return = barr;
  1093. } break;
  1094. case VisualScriptBuiltinFunc::BYTES_TO_VAR: {
  1095. if (p_inputs[0]->get_type() != Variant::POOL_BYTE_ARRAY) {
  1096. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1097. r_error.argument = 0;
  1098. r_error.expected = Variant::POOL_BYTE_ARRAY;
  1099. return;
  1100. }
  1101. if (p_inputs[1]->get_type() != Variant::BOOL) {
  1102. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1103. r_error.argument = 1;
  1104. r_error.expected = Variant::BOOL;
  1105. return;
  1106. }
  1107. PoolByteArray varr = *p_inputs[0];
  1108. bool allow_objects = *p_inputs[1];
  1109. Variant ret;
  1110. {
  1111. PoolByteArray::Read r = varr.read();
  1112. Error err = decode_variant(ret, r.ptr(), varr.size(), nullptr, allow_objects);
  1113. if (err != OK) {
  1114. r_error_str = RTR("Not enough bytes for decoding bytes, or invalid format.");
  1115. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1116. r_error.argument = 0;
  1117. r_error.expected = Variant::POOL_BYTE_ARRAY;
  1118. return;
  1119. }
  1120. }
  1121. *r_return = ret;
  1122. } break;
  1123. case VisualScriptBuiltinFunc::COLORN: {
  1124. VALIDATE_ARG_NUM(1);
  1125. Color color = Color::named(*p_inputs[0]);
  1126. color.a = *p_inputs[1];
  1127. *r_return = String(color);
  1128. } break;
  1129. default: {
  1130. }
  1131. }
  1132. }
  1133. class VisualScriptNodeInstanceBuiltinFunc : public VisualScriptNodeInstance {
  1134. public:
  1135. VisualScriptBuiltinFunc *node;
  1136. VisualScriptInstance *instance;
  1137. VisualScriptBuiltinFunc::BuiltinFunc func;
  1138. virtual int step(const Variant **p_inputs, Variant **p_outputs, StartMode p_start_mode, Variant *p_working_mem, Variant::CallError &r_error, String &r_error_str) {
  1139. VisualScriptBuiltinFunc::exec_func(func, p_inputs, p_outputs[0], r_error, r_error_str);
  1140. return 0;
  1141. }
  1142. };
  1143. VisualScriptNodeInstance *VisualScriptBuiltinFunc::instance(VisualScriptInstance *p_instance) {
  1144. VisualScriptNodeInstanceBuiltinFunc *instance = memnew(VisualScriptNodeInstanceBuiltinFunc);
  1145. instance->node = this;
  1146. instance->instance = p_instance;
  1147. instance->func = func;
  1148. return instance;
  1149. }
  1150. void VisualScriptBuiltinFunc::_bind_methods() {
  1151. ClassDB::bind_method(D_METHOD("set_func", "which"), &VisualScriptBuiltinFunc::set_func);
  1152. ClassDB::bind_method(D_METHOD("get_func"), &VisualScriptBuiltinFunc::get_func);
  1153. String cc;
  1154. for (int i = 0; i < FUNC_MAX; i++) {
  1155. if (i > 0) {
  1156. cc += ",";
  1157. }
  1158. cc += func_name[i];
  1159. }
  1160. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, cc), "set_func", "get_func");
  1161. BIND_ENUM_CONSTANT(MATH_SIN);
  1162. BIND_ENUM_CONSTANT(MATH_COS);
  1163. BIND_ENUM_CONSTANT(MATH_TAN);
  1164. BIND_ENUM_CONSTANT(MATH_SINH);
  1165. BIND_ENUM_CONSTANT(MATH_COSH);
  1166. BIND_ENUM_CONSTANT(MATH_TANH);
  1167. BIND_ENUM_CONSTANT(MATH_ASIN);
  1168. BIND_ENUM_CONSTANT(MATH_ACOS);
  1169. BIND_ENUM_CONSTANT(MATH_ATAN);
  1170. BIND_ENUM_CONSTANT(MATH_ATAN2);
  1171. BIND_ENUM_CONSTANT(MATH_SQRT);
  1172. BIND_ENUM_CONSTANT(MATH_FMOD);
  1173. BIND_ENUM_CONSTANT(MATH_FPOSMOD);
  1174. BIND_ENUM_CONSTANT(MATH_FLOOR);
  1175. BIND_ENUM_CONSTANT(MATH_CEIL);
  1176. BIND_ENUM_CONSTANT(MATH_ROUND);
  1177. BIND_ENUM_CONSTANT(MATH_ABS);
  1178. BIND_ENUM_CONSTANT(MATH_SIGN);
  1179. BIND_ENUM_CONSTANT(MATH_POW);
  1180. BIND_ENUM_CONSTANT(MATH_LOG);
  1181. BIND_ENUM_CONSTANT(MATH_EXP);
  1182. BIND_ENUM_CONSTANT(MATH_ISNAN);
  1183. BIND_ENUM_CONSTANT(MATH_ISINF);
  1184. BIND_ENUM_CONSTANT(MATH_EASE);
  1185. BIND_ENUM_CONSTANT(MATH_DECIMALS);
  1186. BIND_ENUM_CONSTANT(MATH_STEPIFY);
  1187. BIND_ENUM_CONSTANT(MATH_LERP);
  1188. BIND_ENUM_CONSTANT(MATH_INVERSE_LERP);
  1189. BIND_ENUM_CONSTANT(MATH_RANGE_LERP);
  1190. BIND_ENUM_CONSTANT(MATH_MOVE_TOWARD);
  1191. BIND_ENUM_CONSTANT(MATH_DECTIME);
  1192. BIND_ENUM_CONSTANT(MATH_RANDOMIZE);
  1193. BIND_ENUM_CONSTANT(MATH_RAND);
  1194. BIND_ENUM_CONSTANT(MATH_RANDF);
  1195. BIND_ENUM_CONSTANT(MATH_RANDOM);
  1196. BIND_ENUM_CONSTANT(MATH_SEED);
  1197. BIND_ENUM_CONSTANT(MATH_RANDSEED);
  1198. BIND_ENUM_CONSTANT(MATH_DEG2RAD);
  1199. BIND_ENUM_CONSTANT(MATH_RAD2DEG);
  1200. BIND_ENUM_CONSTANT(MATH_LINEAR2DB);
  1201. BIND_ENUM_CONSTANT(MATH_DB2LINEAR);
  1202. BIND_ENUM_CONSTANT(MATH_POLAR2CARTESIAN);
  1203. BIND_ENUM_CONSTANT(MATH_CARTESIAN2POLAR);
  1204. BIND_ENUM_CONSTANT(MATH_WRAP);
  1205. BIND_ENUM_CONSTANT(MATH_WRAPF);
  1206. BIND_ENUM_CONSTANT(LOGIC_MAX);
  1207. BIND_ENUM_CONSTANT(LOGIC_MIN);
  1208. BIND_ENUM_CONSTANT(LOGIC_CLAMP);
  1209. BIND_ENUM_CONSTANT(LOGIC_NEAREST_PO2);
  1210. BIND_ENUM_CONSTANT(OBJ_WEAKREF);
  1211. BIND_ENUM_CONSTANT(FUNC_FUNCREF);
  1212. BIND_ENUM_CONSTANT(TYPE_CONVERT);
  1213. BIND_ENUM_CONSTANT(TYPE_OF);
  1214. BIND_ENUM_CONSTANT(TYPE_EXISTS);
  1215. BIND_ENUM_CONSTANT(TEXT_CHAR);
  1216. BIND_ENUM_CONSTANT(TEXT_STR);
  1217. BIND_ENUM_CONSTANT(TEXT_PRINT);
  1218. BIND_ENUM_CONSTANT(TEXT_PRINTERR);
  1219. BIND_ENUM_CONSTANT(TEXT_PRINTRAW);
  1220. BIND_ENUM_CONSTANT(VAR_TO_STR);
  1221. BIND_ENUM_CONSTANT(STR_TO_VAR);
  1222. BIND_ENUM_CONSTANT(VAR_TO_BYTES);
  1223. BIND_ENUM_CONSTANT(BYTES_TO_VAR);
  1224. BIND_ENUM_CONSTANT(COLORN);
  1225. BIND_ENUM_CONSTANT(MATH_SMOOTHSTEP);
  1226. BIND_ENUM_CONSTANT(MATH_POSMOD);
  1227. BIND_ENUM_CONSTANT(MATH_LERP_ANGLE);
  1228. BIND_ENUM_CONSTANT(TEXT_ORD);
  1229. BIND_ENUM_CONSTANT(FUNC_MAX);
  1230. }
  1231. VisualScriptBuiltinFunc::VisualScriptBuiltinFunc(VisualScriptBuiltinFunc::BuiltinFunc func) {
  1232. this->func = func;
  1233. }
  1234. VisualScriptBuiltinFunc::VisualScriptBuiltinFunc() {
  1235. func = MATH_SIN;
  1236. }
  1237. template <VisualScriptBuiltinFunc::BuiltinFunc func>
  1238. static Ref<VisualScriptNode> create_builtin_func_node(const String &p_name) {
  1239. Ref<VisualScriptBuiltinFunc> node = memnew(VisualScriptBuiltinFunc(func));
  1240. return node;
  1241. }
  1242. void register_visual_script_builtin_func_node() {
  1243. VisualScriptLanguage::singleton->add_register_func("functions/built_in/sin", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SIN>);
  1244. VisualScriptLanguage::singleton->add_register_func("functions/built_in/cos", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_COS>);
  1245. VisualScriptLanguage::singleton->add_register_func("functions/built_in/tan", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_TAN>);
  1246. VisualScriptLanguage::singleton->add_register_func("functions/built_in/sinh", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SINH>);
  1247. VisualScriptLanguage::singleton->add_register_func("functions/built_in/cosh", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_COSH>);
  1248. VisualScriptLanguage::singleton->add_register_func("functions/built_in/tanh", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_TANH>);
  1249. VisualScriptLanguage::singleton->add_register_func("functions/built_in/asin", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ASIN>);
  1250. VisualScriptLanguage::singleton->add_register_func("functions/built_in/acos", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ACOS>);
  1251. VisualScriptLanguage::singleton->add_register_func("functions/built_in/atan", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ATAN>);
  1252. VisualScriptLanguage::singleton->add_register_func("functions/built_in/atan2", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ATAN2>);
  1253. VisualScriptLanguage::singleton->add_register_func("functions/built_in/sqrt", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SQRT>);
  1254. VisualScriptLanguage::singleton->add_register_func("functions/built_in/fmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FMOD>);
  1255. VisualScriptLanguage::singleton->add_register_func("functions/built_in/fposmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FPOSMOD>);
  1256. VisualScriptLanguage::singleton->add_register_func("functions/built_in/posmod", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_POSMOD>);
  1257. VisualScriptLanguage::singleton->add_register_func("functions/built_in/floor", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_FLOOR>);
  1258. VisualScriptLanguage::singleton->add_register_func("functions/built_in/ceil", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_CEIL>);
  1259. VisualScriptLanguage::singleton->add_register_func("functions/built_in/round", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ROUND>);
  1260. VisualScriptLanguage::singleton->add_register_func("functions/built_in/abs", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ABS>);
  1261. VisualScriptLanguage::singleton->add_register_func("functions/built_in/sign", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SIGN>);
  1262. VisualScriptLanguage::singleton->add_register_func("functions/built_in/pow", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_POW>);
  1263. VisualScriptLanguage::singleton->add_register_func("functions/built_in/log", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LOG>);
  1264. VisualScriptLanguage::singleton->add_register_func("functions/built_in/exp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_EXP>);
  1265. VisualScriptLanguage::singleton->add_register_func("functions/built_in/isnan", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ISNAN>);
  1266. VisualScriptLanguage::singleton->add_register_func("functions/built_in/isinf", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_ISINF>);
  1267. VisualScriptLanguage::singleton->add_register_func("functions/built_in/ease", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_EASE>);
  1268. VisualScriptLanguage::singleton->add_register_func("functions/built_in/decimals", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECIMALS>);
  1269. VisualScriptLanguage::singleton->add_register_func("functions/built_in/stepify", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_STEPIFY>);
  1270. VisualScriptLanguage::singleton->add_register_func("functions/built_in/lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LERP>);
  1271. VisualScriptLanguage::singleton->add_register_func("functions/built_in/lerp_angle", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LERP_ANGLE>);
  1272. VisualScriptLanguage::singleton->add_register_func("functions/built_in/inverse_lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_INVERSE_LERP>);
  1273. VisualScriptLanguage::singleton->add_register_func("functions/built_in/range_lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANGE_LERP>);
  1274. VisualScriptLanguage::singleton->add_register_func("functions/built_in/smoothstep", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SMOOTHSTEP>);
  1275. VisualScriptLanguage::singleton->add_register_func("functions/built_in/move_toward", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_MOVE_TOWARD>);
  1276. VisualScriptLanguage::singleton->add_register_func("functions/built_in/dectime", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECTIME>);
  1277. VisualScriptLanguage::singleton->add_register_func("functions/built_in/randomize", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOMIZE>);
  1278. VisualScriptLanguage::singleton->add_register_func("functions/built_in/rand", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RAND>);
  1279. VisualScriptLanguage::singleton->add_register_func("functions/built_in/randf", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDF>);
  1280. VisualScriptLanguage::singleton->add_register_func("functions/built_in/random", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDOM>);
  1281. VisualScriptLanguage::singleton->add_register_func("functions/built_in/seed", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SEED>);
  1282. VisualScriptLanguage::singleton->add_register_func("functions/built_in/randseed", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANDSEED>);
  1283. VisualScriptLanguage::singleton->add_register_func("functions/built_in/deg2rad", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DEG2RAD>);
  1284. VisualScriptLanguage::singleton->add_register_func("functions/built_in/rad2deg", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RAD2DEG>);
  1285. VisualScriptLanguage::singleton->add_register_func("functions/built_in/linear2db", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LINEAR2DB>);
  1286. VisualScriptLanguage::singleton->add_register_func("functions/built_in/db2linear", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DB2LINEAR>);
  1287. VisualScriptLanguage::singleton->add_register_func("functions/built_in/polar2cartesian", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_POLAR2CARTESIAN>);
  1288. VisualScriptLanguage::singleton->add_register_func("functions/built_in/cartesian2polar", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_CARTESIAN2POLAR>);
  1289. VisualScriptLanguage::singleton->add_register_func("functions/built_in/wrapi", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_WRAP>);
  1290. VisualScriptLanguage::singleton->add_register_func("functions/built_in/wrapf", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_WRAPF>);
  1291. VisualScriptLanguage::singleton->add_register_func("functions/built_in/max", create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_MAX>);
  1292. VisualScriptLanguage::singleton->add_register_func("functions/built_in/min", create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_MIN>);
  1293. VisualScriptLanguage::singleton->add_register_func("functions/built_in/clamp", create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_CLAMP>);
  1294. VisualScriptLanguage::singleton->add_register_func("functions/built_in/nearest_po2", create_builtin_func_node<VisualScriptBuiltinFunc::LOGIC_NEAREST_PO2>);
  1295. VisualScriptLanguage::singleton->add_register_func("functions/built_in/weakref", create_builtin_func_node<VisualScriptBuiltinFunc::OBJ_WEAKREF>);
  1296. VisualScriptLanguage::singleton->add_register_func("functions/built_in/funcref", create_builtin_func_node<VisualScriptBuiltinFunc::FUNC_FUNCREF>);
  1297. VisualScriptLanguage::singleton->add_register_func("functions/built_in/convert", create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_CONVERT>);
  1298. VisualScriptLanguage::singleton->add_register_func("functions/built_in/typeof", create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_OF>);
  1299. VisualScriptLanguage::singleton->add_register_func("functions/built_in/type_exists", create_builtin_func_node<VisualScriptBuiltinFunc::TYPE_EXISTS>);
  1300. VisualScriptLanguage::singleton->add_register_func("functions/built_in/char", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_CHAR>);
  1301. VisualScriptLanguage::singleton->add_register_func("functions/built_in/ord", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_ORD>);
  1302. VisualScriptLanguage::singleton->add_register_func("functions/built_in/str", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_STR>);
  1303. VisualScriptLanguage::singleton->add_register_func("functions/built_in/print", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINT>);
  1304. VisualScriptLanguage::singleton->add_register_func("functions/built_in/printerr", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTERR>);
  1305. VisualScriptLanguage::singleton->add_register_func("functions/built_in/printraw", create_builtin_func_node<VisualScriptBuiltinFunc::TEXT_PRINTRAW>);
  1306. VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2str", create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_STR>);
  1307. VisualScriptLanguage::singleton->add_register_func("functions/built_in/str2var", create_builtin_func_node<VisualScriptBuiltinFunc::STR_TO_VAR>);
  1308. VisualScriptLanguage::singleton->add_register_func("functions/built_in/var2bytes", create_builtin_func_node<VisualScriptBuiltinFunc::VAR_TO_BYTES>);
  1309. VisualScriptLanguage::singleton->add_register_func("functions/built_in/bytes2var", create_builtin_func_node<VisualScriptBuiltinFunc::BYTES_TO_VAR>);
  1310. VisualScriptLanguage::singleton->add_register_func("functions/built_in/color_named", create_builtin_func_node<VisualScriptBuiltinFunc::COLORN>);
  1311. }