tables supported?

Kit_sune
21 Oct 2012, 02:13
Being an analyst, I use spreadsheets every day at work. I’ve read in another program that you can code with tables. I couldn’t find support for this in Quest at all, is this possible? Or would it need to be used in a library file if I wanted to try using one?
Why would I want to use one you ask?
Well instead of creating a bunch of templates like I had originally considered, I thought it would be better to create a few tables that could be referenced.
I would just write the code that would make the starting attributes of the player the same as the table.

like,
Attribute / human / elf
health / 50 / 70

Thanks for your time and sorry if that's not clear enough.

The Pixie
21 Oct 2012, 10:10
I have also thought that tables would be a good way to put in data when creating a game, but quest does not even support 2D arrays without some awkward hacking.

jaynabonne
21 Oct 2012, 10:12
I'd say, the closest you'd get in Quest is a string list. But every item in the list would be a string, so the numeric ones would be a bit tricky to handle (you'd have to know which were numbers and call ToString on them, for instance).

As far as coding with tables -that seems like a fairly specialized thing. What it really seems that you want is "data import using tables", but there is nothing directly in Quest for that. In my opinion, that's a bit esoteric and not something the majority of people would want to do ever. (Yes - you're special! :) )

What would be handy (especially given Pixie's response) would be some form of serialized object format...

Kit_sune
21 Oct 2012, 15:38
Data importing using tables would be cool, but I know that's not available, lol.

So after searching a bit, I realize that tables are mostly a "Lua" language thing. (That's the language I started with) And the resource I was reading references their likeness to arrays, since most programmers are familiar with those.


From: http://www.lua.org/pil/11.html
Lua
11 – Data Structures

Tables in Lua are not a data structure; they are the data structure. All structures that other languages offer---arrays, records, lists, queues, sets---are represented with tables in Lua. More to the point, tables implement all these structures efficiently.

In traditional languages, such as C and Pascal, we implement most data structures with arrays and lists (where lists = records + pointers). Although we can implement arrays and lists using Lua tables (and sometimes we do that), tables are more powerful than arrays and lists; many algorithms are simplified to the point of triviality with the use of tables. For instance, you seldom write a search in Lua, because tables offer direct access to any type.



I guess what I'm really looking for is something that looks like this:

 
LISTrace = {
human = { race = human, hp = 50, maxhp = 50, str = 4, atk = 5},
foxdemon = { race = fox, hp = 75, maxhp = 75, str = 3, atk = 4},
catdemon = { race = cat, hp = 60, maxhp = 60, str = 2, atk = 4},
}


Of course, this is just what I'm looking for. Let me reiterate - This is what I work with for my job, tables. I realize I wouldn't really know how to call the values from these arrays once I figured out how to make it work this way, lol.

Thanks for the input,

jaynabonne
21 Oct 2012, 18:46
Well I'm always up for a challenge... Attached is an implementation that re-implements your other test app, but uses a string for each race with format:

name=value, name=value, name=value, ...

There is a function called "ApplyAttributes", which takes an object (e.g. the player) and a string like the above, and for each name/value pair, assigns an attribute to the object with the given value.

For example, you can list the "Human" attributes as "race=human, maxhp=50, speed=10, stamina=45", and if you ApplyAttributes to the player with this string, then player.race will equal "human" (as a string), maxhp will equal 50 (as a number), etc.

This is a more compact format, and is in line (I think) with what you want.

The test app has a string attribute for each race stored in the game. The list to be shown is built up directly, and the menu shown. Then the selected race's attributes are applied to the player.

See how that is!

Kit_sune
21 Oct 2012, 19:39
This is probably the closest I could get to what I wanted, and I'm happy + excited to use this format!

You had,

<HumanRace>race=human, maxhp=50, speed=10, stamina=45</HumanRace>
<DogRace>race=dog, maxhp=15, speed=20, stamina=20</DogRace>
<CatRace>race=cat, maxhp=8, speed=15, stamina=18</CatRace>


Would there be any way to make it so that these lists (HumanRace, DogRace, CatRace) were inside of another list? (like a races list) For instance, so that maybe the dictionary choices could read as follows:

dictionary add (choices, game.races.HumanRace, "Human")
dictionary add (choices, game.races.CatRace, "Cat")
dictionary add (choices, game.races.DogRace, "Dog")


It might just be complicating things a little too much. I am aware that there really wouldn't be much of a difference to do it either way, if it was possible, but just something that I am very very curious about.

jaynabonne
21 Oct 2012, 19:51
The only way you can have the dot notation is with an object. So in your case, if you had "game.races.xxx", then "races" would need to be an object. Or, more correctly, an object *reference* since it's an attribute of another object - which means you need an object to refer to. So you can do it like this:

Create an object to hold the race strings.

<object name="Races">
<HumanRace>race=human, maxhp=50, speed=10, stamina=45</HumanRace>
<DogRace>race=dog, maxhp=15, speed=20, stamina=20</DogRace>
<CatRace>race=cat, maxhp=8, speed=15, stamina=18</CatRace>
...
</object>


And then in your game object have:

<game ...>
....
<races type="object">Races</races>
...
</game>


Then you can either use "game.races.HumanRace" or just "Races.HumanRace". If you prefer the second format, then don't even bother with the game "races" attribute. :)

Believe me, I understand the motivation for this, that desire to organize things!

Kit_sune
21 Oct 2012, 21:30
Awesome, that worked like a charm!

So the function ApplyAttributes does all these nifty things for me behind the scenes, I like it! Now if only I knew exactly how it works.
I get the jist of it, but it would take me a long time to come up with something like that.

jaynabonne
21 Oct 2012, 22:47
Basically, it starts at the beginning of the string and looks for an equals sign and then takes the word between the start and there as the attribute name. Then it looks past the equals sign for either a comma or end of string, and uses everything in between as the value. It uses trim to trim off any white space. Then it looks at the value. If it's numeric, it converts it to an int and assigns it to the object. Otherwise, it just assigns it to the object as a string.

It does that over and over, marching along, until it runs out of string. Of course, the devil is in the details... I've written code like that far too many times. :)

Glad it's working for you.

jaynabonne
23 Oct 2012, 23:12
Just a followup note... I realized that the StringDictionary format is almost identical to the string format used in this sample, except a StringDictionary uses semicolons instead of commas. So there already is a compact way to specify some forms of data in Quest. It would be straightforward (and less code!) to rewrite "ApplyAttributes" to use a string dictionary instead of parsing a string. If anyone wants it, let me know.

Kit_sune
24 Oct 2012, 00:56
Heck,

Even if it does nearly the same thing as the ApplyAttributes function, I'd be more than willing to see how the StringDictionary version of it works so I could learn some more. It could come in useful.

Just a quick question for whoever reads it, did you all go to a programing course/class/school?

I've had to self teach myself a lot of things, and programing has been by far the toughest thing to learn. I've been returning to it for over 13 years (when I was 13 years old) with various different programs that allow you to manipulate code. I've always been fascinated by it, but I've never had the fortune to take any classes, and most tutorials I've poked around in were really confusing with how they bounce around.

I don't want anyone here to get the idea that I'm trying to get you all to write all my code for me - I really aim to learn this stuff. But it's kind of embarrasing asking for so much help because I know you all have your own projects, and have been to school, so why should I get any special treatment? I'm just so grateful with all the help I've received so far that for once I feel like I might actually be begining to understand how this stuff works.

I'm the kind of person who needs to understand every . little . detail about how something works before it all clicks into place. It's like putting all the cogs together. If I'm missing even one cog, they won't function properly!!!

Ok I'm done with my story XD
If TLDR I'm sorry !!
Kitsune

The Pixie
24 Oct 2012, 08:06
I wondered why you did not use a string dictionary.

Here is an alternatve version. ApplyAttributes is much smaller, but other parts of the code got bigger. I found the easiest way was to have each race template as an object, with stringdictionary attached.

<!--Saved by Quest 5.2.4515.34846-->
<asl version="520">
<include ref="English.aslx" />
<include ref="Core.aslx" />
<game name="ApplyAttributes">
<gameid>e4c54dda-b7a0-470f-abbc-2d603f94a18e</gameid>
<version>1.0</version>
<start type="script">
choices = NewStringList()
races = NewObjectList()
list add (races, HumanRace)
list add (races, CatRace)
list add (races, DogRace)
foreach (race, races) {
list add (choices, race.name)
}
msg(" ")
show menu ("What is your race?", choices, false) {
// Copy all the attributes, using the "attributes" string list as key.
ApplyAttributes(player, GetObject (result))

msg("player.race = " + player.race)
msg("player.maxhp = " + player.maxhp)
msg("player.speed = " + player.speed)
msg("player.stamina = " + player.stamina)
}
</start>
</game>
<object name="HumanRace">
<stats type="stringdictionary">race=Human; maxhp=50; speed=10; stamina=45</stats>
</object>
<object name="DogRace">
<stats type="stringdictionary">race=Dog; maxhp=15; speed=20; stamina=20</stats>
</object>
<object name="CatRace">
<stats type="stringdictionary">race=Cat; maxhp=8; speed=15; stamina=18</stats>
</object>
<object name="room">
<inherit name="editor_room" />
<object name="player">
<inherit name="defaultplayer" />
</object>
</object>
<function name="ApplyAttributes" parameters="object, race"><![CDATA[
foreach (attribute, race.stats) {
value = StringDictionaryItem (race.stats, attribute)
if (IsNumeric(value)) {
set (object, attribute, ToInt(value))
} else {
set (object, attribute, value)
}
}
]]></function>
</asl>

jaynabonne
24 Oct 2012, 09:12
That looks great. Thanks, Pixie! (Is "Pixie" ok, or do you prefer "Pix" or "The Pixie" or "The Pix" or..? :) )