I’ve made an extension with useful utility blocks, there are blocks for things like JSON and text. To use it open the extensions menu, put in https://github.com/usertheusernamer/pxt-utilities inside the top textbox, and click on the extension.
I’d like to add two new blocks that act like try and try-catch statements (Handles errors and prevents crashing), but it seems like try-catch statements aren’t supported. If you have any more block ideas, tell me and I’ll add them.
I have a utility idea! Long strings of if/else can get confusing, perhaps implementing something like the The ‘case’ function in swift that can have one output for each possible input without long lines of if/else.
Sounds like a great idea but after thinking a while, I don’t think it’s possible to implement that as an extension and wouldn’t it look like the same as if-else blocks? Maybe you are placing separate if-else blocks into other if-else blocks? You could just click the plus icon on an if statement and that expands into an if-else statement, another click and it turns into a if-elseif-else statement. It’s not really necessary to implement a switch-case block (also seems impossible due to extension limitations, not the code but how the block behaves like how to expand it) and a large if statement (No nests, just a single giant if-elseif-elseif-elseif-elseif-else block.) would basically be the same if I ever implemented a switch-case block. Sorry if my English is not that easy to read and understand but I like the idea.
True, it would work almost exactly like a long if/elseif statement, but cases are different. One input is put up top and each case compares to that. So, instead of having something like ‘If x=0 then this, if x=1 then this, if x=2 then this’ you could have ‘case x: equals 0 then this, equals 1 then this, equals 2 then this’! I’m guessing it probably still can’t be done but thanks for explaining and considering!! : D
These are inside the utilities namespace in the extension, I’ve put a block in that causes an error and that did not work even when my code was like this:
Nice, I might add all these blocks for arrays into my extension:
include - <array [array] includes [any]?>
join - (join [array] with [text])
concat - (concat [array] and [array])
indexOf - (index of [any] in [array])
unshift - (unshift [any] into [array])
slice - (slice [array] with [number] items)
sort - (sort [array] by [ascending, descending v])
I’m making a List extension that will add a custom datatype: Lists, similar to those in Python.
The methods implemented at the moment are the following:
Item operations
get - get item at index
set - set item at index
delete - delete item at index
push - push item to the end of list
pop - remove and return item at index
insert - insert item at index
find - return index of item if found
remove - remove the first occurence of an item
swap - swap items at two indexes
replace - replace all occurences of item with value
random - return random item from list
count - count item repetitions in list
List operations
extend - append other list to the end of list
flatten - unpack all lists within the list recurively (depth=0 is infinite depth)
slice - return list items from start to end
isList - static method; return true if item is of list type
inRange - return if number is within length of the list
contains - return true if item is in list
compress - remove all undefined values
patch - fill all undefined values with static value
fill - fill with static value from start to end
zip - return pairs of elements from two lists (until smaller length is exceeded)
reverse - reverse the list
forEach - iterate over the list items with value and index
clear - remove all items from list
copy - return new instance with matching items
isEmpty - return true if list contains no elements
all - return true if all items in list are true
any - return true if any item in list is true
union - return items that appear in both lists
purge - remove all duplicates from list
Some of these are inspired by Python methods, some by JS methods and some are my own. These are not final, I might change or update them in the future. Yeahhh, I kind of spoiled my next project after the mini game jam, if anyone’s reading this.
I’m still planning to implement splice and some other missing methods.
At the moment, i’m working on porting all of the methods to blocks, so I can release it as an extension at last.
That’s a lot! Pretty impressive, I’m sure a lot of people will use that. Are you gonna define all these blocks into a single main.ts file or are you gonna make them apart like maybe every category has it’s own file?