I went ahead and made another fake array script using slices. These are modeled after Python lists, so hopefully the language associated with the commands will come more easily to people used to languages like it.
https://rpg.hamsterrepublic.com/ohrrpgc ... ice_arrays
Of course, these scripts are intended to make Hspeak easier to use and less confusing. If you find it does the opposite or doesn't achieve its goal well enough, let me know what I can do to simplify or streamline things to make them more accessible. Not having access to data structures like this has been my biggest hurdle when writing complex scripts for games. It's my hope that these may make a lot of projects smoother to develop.
New Style of Fake Arrays
Moderators: Bob the Hamster, marionline, SDHawk
New Style of Fake Arrays
My pronouns are they/them
Ps. I love my wife
Ps. I love my wife
I think this is a really nice library and when I want a plain array of numbers I'll probably use it myself instead of the scripts I had been using; it's amazing nobody else had created it already.
I already added another script to the library, and we could do some more if there are any in particular that people want to use. (Sort? Reverse? Sum? Permute?)
One thing that's a concern is that if you have a variable anywhere named "copy" or "index" or "insert", etc, then the scripts by those names will clash, because HamsterSpeak doesn't allow local variables to shadow global variables or scripts.
This is a problem we had in plotscr.hsd itself, resulting in us giving all the local variables in it weird names.
I have been thinking lately that HamsterSpeak ought to switch to allow shadowing, to make it easier to distribute scripts.
When I use these scripts, I might rename them something like "acopy", "aget", "aset", etc
I already added another script to the library, and we could do some more if there are any in particular that people want to use. (Sort? Reverse? Sum? Permute?)
One thing that's a concern is that if you have a variable anywhere named "copy" or "index" or "insert", etc, then the scripts by those names will clash, because HamsterSpeak doesn't allow local variables to shadow global variables or scripts.
This is a problem we had in plotscr.hsd itself, resulting in us giving all the local variables in it weird names.
I have been thinking lately that HamsterSpeak ought to switch to allow shadowing, to make it easier to distribute scripts.
When I use these scripts, I might rename them something like "acopy", "aget", "aset", etc
- Bob the Hamster
- Lord of the Slimes
- Posts: 7660
- Joined: Tue Oct 16, 2007 2:34 pm
- Location: Hamster Republic (Ontario Enclave)
- Contact:
Of course if you allow shadowing it diminishes the compiler's ability to detect mistakes. I wonder whether that's at all worth keeping by doing something a bit more moderate like having file-based namespaces, where local variables can only shadow names in other files, or the same thing but with an explicit namespace() block.
Last edited by TMC on Tue Oct 09, 2018 2:26 am, edited 1 time in total.
- SwordPlay
- Chemical Slime
- Posts: 966
- Joined: Sun Jan 22, 2017 9:32 am
- Location: London, England
- Contact:
it's a table basically (but it could be extended in more than 2 dimensions quite easily)guo wrote:What's an array and what are they used for?
They could be used to store data about objects, compare them, modify/create/delete data, swap data, etc.,
Each bit of data in the array is basically like a variable that you can check the state of.
So it's like creating a table of variables which are located in slices.
In the context of game-making;
You could attach them to NPCs to represent a list of their inventory, a list of their stats, etc.,
But realistically, you can use it to model data however you like if you use it creatively.
You can attach these variables to whatever you like and keep it together as a table (array).
The script provides functions for modifying the arrays and data, and you could easily add your own to extend the table however you like.
"Imagination. Life is your creation."
I hadn't considered making these arrays multidimensional. I guess it's possible by nature, but are the commands clean enough to make that convenient? I'm thinking of changing "getElement" and "setElement" to "getA" and "setA." When using array syntax in other languages, accessing and manipulating data within an array is usually the shortest command (eg. "arrayName[0] = value"). getElement is sort of a mouthful.
My pronouns are they/them
Ps. I love my wife
Ps. I love my wife
I don't think general multidimensional arrays is worth the trouble. You can always store arrays (slice handles) in arrays to nest lists. But maybe 2D arrays where the second index is limited (eg up to 15) would be useful. Then you can use the second index for attributes of objects. E.g. a list of enemies, where each enemy has HP, MP, X, Y, etc. You could do that by combining these scripts with those at https://rpg.hamsterrepublic.com/ohrrpgc ... ice_arrays
getA/setA seem pretty good.
Common Lisp is one language whether access to arrays isn't very concise, and it's pretty annoying.
Oh, and Sword left some comments at https://rpg.hamsterrepublic.com/ohrrpgc ... ice_arrays
"hero array" and "npc array" are an interesting idea.
You can't use "sub array(ao,1,-1)" to copy an array though. That will trim off both the first and the last element. (I guess the position2 arg should default to something like 999999 so that if left out it copies until the end, like Python). Just use "copy".
It might be worth mentioning that when non-integer types get added to HamsterSpeak, you won't be allowed to store them in slice X or other data fields, only in variables, global variables, and extra data fields on slices/NPCs/etc. So for future proofing maybe you should switch to "set slice extra" instead of "set slice X"
getA/setA seem pretty good.
Common Lisp is one language whether access to arrays isn't very concise, and it's pretty annoying.
Oh, and Sword left some comments at https://rpg.hamsterrepublic.com/ohrrpgc ... ice_arrays
"hero array" and "npc array" are an interesting idea.
You can't use "sub array(ao,1,-1)" to copy an array though. That will trim off both the first and the last element. (I guess the position2 arg should default to something like 999999 so that if left out it copies until the end, like Python). Just use "copy".
It might be worth mentioning that when non-integer types get added to HamsterSpeak, you won't be allowed to store them in slice X or other data fields, only in variables, global variables, and extra data fields on slices/NPCs/etc. So for future proofing maybe you should switch to "set slice extra" instead of "set slice X"
Last edited by TMC on Wed Oct 10, 2018 5:03 am, edited 2 times in total.