Sorting objects?

KiraxSummers
Hi everyone,

I have been tinkering around with Quest for some time and have some trouble with functions. I have a decent C++ background which I'm finding is mostly incompatible with Quest XD
Anyway, I was trying to create a function setOrder which would create an ObjectList of six objects sorted in descending order of each object's "speed" attribute (which is an integer).
I tried to use a bubble sort method in which I loaded each object into the list at random and sorted it in the next step but it gave me a template error (template 'j' not found). j was my inner loop control variable. I tried declaring j outside the loop but I got the same error.
I have a few questions...

a) Is this the best way to achieve what I'm trying to do?
b) Is it even possible to do this? If so, what am I doing wrong?
b) Are ObjectLists like arrays? If my object list was named 'Order', could I access individual objects like Order[0], Order[1], etc?
c) If not, is there a way to create arrays?

Thanks for the help, I love Quest, it's a really fun software :) I hope the questions aren't too weird... :oops:

HegemonKhan
sorting coding is still beyond my noob code ability, but you can check the quest wiki, it has two built-in sorting functions that you can use, but as for creating your own, someone else will surely help you, and I think there was also a recent thread with posts on and with custom sorting code for you to look at (or maybe it's a thread in the 'Libraries+Code' board section of this site ~ meh).

I had+have trouble understanding arrays, but I believe that lists and~or dictionaries are arrays (aka tables, right?).

I understand quest's lists and dictionaries well, and can help explain them to you or help you with them, but as for advanced coding design (fancy coding stuff) with them, I can't help with that, laughs.

I'm not sure if quest coding allows for the Order(0), Order(1), etc... you can try looking at the quest wiki for 'using lists' and 'using dictionaries', as it might inform you of whether you can do this or not.

also, you can try reading the posts in this thread too, for about lists and dictionaries:

viewtopic.php?f=10&t=4279
(I talk about lists and dictionaries at about half way down within my first big post)

HegemonKhan
found it (this is directly about~on sorting), hehe:

viewtopic.php?f=10&t=4199

I hope this helps you! :D

KiraxSummers
Thanks so much for the quick reply.
That topic will be useful; I'll go through it!
I'll definitely go look at those resources and post back here if I find a suitable solution!
Arrays are simple to understand, they're kinda like a tray of cups that hold variables in adjoining memory spaces (that's from HeadFirst Java, love that book <3). But if Object lists are like arrays, it would be hard if not impossible to access its members without indexes (list[0], list[1]....). Maybe the syntax is different in Quest?
Edit:
Hmm... so dictionaries are easier to work with, they seem to have index keys. Will try to create an object dictionary and see how that goes.

Thanks again!

HegemonKhan
Lists (string and object) conceptually are just a "split" big string into smaller strings, which operate as choices to select:

string: redblueyellow
string list: (1) red, (2) blue, or (3) yellow

string -> "split" function -> string list
string list -> "join" function -> string

in actual code usage:

split ("string_item_1;string_item_2;string_item_3;etc", "your separator character: in this example, I'm using the semicolon")

show menu ("What is your favorite primary color?", split ("red;blue;yellow", ";"), false) { script }

but as you should know, for a list item retrieval~reference, the items start at 0, not 1, so:

while there's 3 items, they're ordered as...

0: red
1: blue
2: yellow

StringListItem (String List, 0) -> red
StringListItem (String List, 1) -> blue
StringListItem (String List, 2) -> yellow

------------

whereas, dictionaries are:

String Dictionary: string = string; string = string; string = string; etc
Object Dictionary: string = object; string = object; string = object; etc
Script Dictionary: string = script; string = script; string = script; etc

conceptually:

think conversion and algebraic substitution

String Dictionary: fire=water (fire -> water); water=fire (water -> fire); earth=air (earth -> air); air=water (air -> earth)

if (input:fire), then output: water
if (input:water), then output: fire
if (input:earth), then output: air
if (input:air), then output: earth

an example (pseodo code for concept):

String Dictionary: water=fire

monster.elemental = "fire"
your_spell.elemental = "water"

if (monster.elemental = StringDictionaryItem (String Dictionary, "water") ), then you do double the damage, as the fire monster is weak against your water spell

if ("fire" = ("water" -> "fire") ), then... x2 damage

if ("fire" = "fire"), then... x2 damage

other examples:

String Dictionary: jan = winter; feb = winter; march = spring; etc

if (you born in jan), you are born in the season of winter
etc etc etc

String Dictionary: jan = 01; 01 = jan

if (input: 01), you born in output: jan
if (input: jan), you birth date is output: 01-DD-YYYY

Object and Script, are the exact same... an example of a Script Dictionary:

Script Dictionary: "dragon" = msg ("The evil dragon kidnapped the princess"); "sword" = msg ("Only the dragon slayer sword can kill the dragon"); "princess" = msg ("If you rescue her from the dragon, you'll be the new king, and have many kids with her, happily ever after.")

jaynabonne
Check out ObjectIstSort: http://quest5.net/wiki/ObjectListSortht ... ctListSort

You can sort a list of objects based on a specific attribute (speed in your case).

Lists in Quest are *sort of* like arrays (vectors) in other languages. The drawback in Quest is that you can't arbitrarily assign to them. You can read from an arbitrary index, but you can only add items on the end. That makes some things a bit difficult, to say the least.

I discovered that the expression parser does support the "[]" notation for accessing elements. So if you have a list called "myList", you can use "myList[2]" (or myList[someIndex]) to access values. But you can't do "myList[3] = someValue".

Quest also uses the bracket notation for template strings [ATemplate], so that probably explains your error message, if you used it in an "unacceptable" way.

Dictionaries correspond to maps in other languages (aka "associative arrays"). The key must be a string, but if you create a generic dictionary (use NewDictionary instead of something like NewStringDictionary), then you can assign any type to a value. The drawback to dictionaries in Quest is that you can't set a value if the value has already been set - you must remove it first, which can be a real pain in some cases.

I have a C++ background as well, but Quest feels more like JavaScript, in that you don't define classes and all that. You just have objects with attributes, and attributes can even change type through the life of the program, morphing to the value you assign to them. It's different but also liberating. :) If you stick with it, you might not look at C++ the same again.

KiraxSummers
Thanks, you guys, you've all been very helpful. :)
I'll sit down and see what I can do... Quest has some... unique features, haha :D
Yeah, I'm only just starting with Java but I'm already stunned with the power of the language. I'm definitely going to focus on it from now on but as of right now, I'm a Java noob XD

Hey, would it be alright if I asked another question here or do I have to create another thread? It seems silly to make so many threads for my noobish questions...

jaynabonne
Ask away. :) If you think it's an entirely new subject, it might be better to start a new thread, especially if it might help others down the line. It also might get more eyes being a fresh question. I'm not sure how much people are drawn to ever-growing threads (as it might seem like a handled discussion).

(And just in case there was any confusion: Java <> JavaScript, not even close. :) )

KiraxSummers
Yeah, no, the question is pretty dumb; I must be missing something simple...

I made a function called dmg(stat, value)
The stat value takes an attribute like player.health (originally 100) and the function has only one line:
stat = stat-value
After this, I printed player.hp and found that it was unchanged (o/p: 100)
Why is this happening? I suppose the value isn't being carried back from the function so how can I make this happen?

So I've heard XD Still, I thought I'd learn Java before JavaScript ;) Both are immensely powerful, doubtlessly :D

jaynabonne
You have the right idea about why it's not working. Some things in Quest are passed by reference (objects, lists, dictionaries, etc) and the rest are passed by value. If you call:

dmg(player.heath, value)

then the dmg function gets as its parameter the *value* of player.health as a local variable. If you add onto that, it just changes the local variable, not the original. The easiest way to work around this is to have it be in-and-out:

player.health = dmg(player.health, value)


where dmg has the right return type and :

return (stat-value)

Since objects are passed by reference, you can do it a different way:

<function name="dmg" parameters="object,field,value">
stat = GetAttribute(object, field)
set(object, field, stat-value)
</function>

Then you would call it with:

dmg(player, "health", value)


Hope that helps!

KiraxSummers
Yep that helps a lot!
That's it for my noobish questions but I will probably be back soon!
Thanks for helping out the baby programmer XD

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums