Data Structures?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Data Structures?

Post by kcorey » Tue Dec 20, 2011 5:22 pm

What kind of data structures do people use in Livecode? I've got a concept of how things might work, but wanted to run things by you all and see what comments came up. Note that I posted this here (in "Complete Beginners") on purpose. If you're coming along later and reading this, use a *large* grain of salt before believing what I type below.

Also, I *love* hearing "RTFM" if you'll just tell me which "FM" to read (extra points for links to relevant articles).

In building a game, I'd like to encapsulate a bunch of items together to form a sprite. (Things like the location, hot point, frame number, and a list of images that comprise the sprite, perhaps alpha, xAccel, yAccel, dxAccel, dyAccel, movement handler, and lots of other things).

It seems that true OOP isn't available, so as a substitute, I was going to make an array of items to be processed.

Each time through the event loop I can iterate once through this array, handling each item at a time.

Each item in the array is itself an array containing the elements I'd like to bundle:
type
loc
hotpointloc
curframe
alpha
xAccel
yAccel
handler
...

Loading each such "sprite" will be a pain I suppose:

put img "sprite_baddie1_fr1" into images[1]
put img "sprite_baddie1_fr2" into images[2]
put img "sprite_baddie1_fr3" into images[3]
put img "sprite_baddie1_fr4" into images[4]
put images into spriteframes["frames"]

put "baddie1" into item["type"]
put "1" into item["curframe"]
...

Finally, I put this object into the object array which is iterated over during frame draw time:

put item into objectArray[index]
set index = index+1

But looping to handle it should be "relatively" straightforward, yes?

So, to draw the next iteration of a sprite (in rough pseudo code at this point)
-- call movement handler for sprite at index 3
call objectArray[3]["handler"],3
-- check for collisions, and react (including changing this sprite into another type).
collide 3
-- do the drawing of the sprite, if still viable (draw the frames based on the sprite "type")
drawsprite 3

My thinking is that the index of the sprite upon which to act is sent to the different functions, which do the right thing.

Oh, and I was thinking that a sprites' type can be changed on the fly, so that the sprite could be "ship", with its frames, and if it gets hit by a bullet could be changed into a "fireball" type sprite, without having to load/change the velocity, movements, or anything.

Is there a better way to do this sort of thing in LiveCode? Am I over-engineering here?

Thanks,

-Ken

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Data Structures?

Post by mwieder » Tue Dec 20, 2011 6:37 pm

Also, I *love* hearing "RTFM" if you'll just tell me which "FM" to read (extra points for links to relevant articles).
LOL.
Is there a better way to do this sort of thing in LiveCode? Am I over-engineering here?
My first take on this is that you're not over-engineering things, but you might want to:

1) post this subject in the Games forum
2) checkout Malte's Animation Engine, which seems to have what you want already packaged up. Malte's done the over-engineering for you ahead of time. <g>.

I also think that some of what you're thinking of putting into arrays of arrays might be better handled as custom properties of the sprite objects. It's more OOP and I think will save you from getting into some spaghetti coding and maintenance headaches as you go on.

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Data Structures?

Post by kcorey » Tue Dec 20, 2011 6:59 pm

Hiya,

I posted here because I thought it would be an incredibly naive approach. Is that the best way to attack this puzzle?

When you're talking about 'sprite' objects...uh...what do you mean? I don't see how to create an object of type "sprite".

Do I create a template object with the properties I want, and then clone it?

Thanks!

-Ken

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Data Structures?

Post by mwieder » Tue Dec 20, 2011 7:20 pm

Do I create a template object with the properties I want, and then clone it?
LOL again... well, *you're* the one who mentioned sprites in the first place.

I haven't done game development in many years, so I'm not the right person to take on questions in that area, that's why I suggested posting in the games section. But you did talk about a group of images that make up a sprite. In that case, I'd assume that the group would be the sprite object and from that assumption you could say

Code: Select all

-- assuming a sprite group "Sprite1"

set the xAccel of group "Sprite1" to tNewValue
set the curFrame of group "Sprite1" to the curFrame of group "Sprite1" + 1
put the type of group "Sprite1" into tSpriteType
-- etc

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Data Structures?

Post by kcorey » Tue Dec 20, 2011 11:24 pm

Wait a minute! I thought a "group" was a collection of UI elements (radio buttons, images, text fields, etc) that are considered by LiveCode to be a single entity. Specifically, in VB you can group several of these elements together in a box sort of thing on a dialog box.

I was not thinking of 'group' as a language construct with which to build data structures.

Please don't get hung up on the 'game' aspect of this. I'm simply trying to explore the data gathering capabilities of LiveCode. I could just as well be building an inventory app for what it's worth. Instead of 'sprite' call it 'bundle'...call it 'category'...call it 'floralgenus'.

The way you've described it, I'd declare a 'group' as a container...so would I need to organise getprop and setprop for each of my special properties (you demonstrate xAccel, curFrame and type above)?

-Ken

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Data Structures?

Post by mwieder » Tue Dec 20, 2011 11:45 pm

Ken-

Maybe I'm the one over-engineering things here.

I'm thinking of a group as a collection of objects. But a group can have properties as well. To save you from having to create a parallel array structure and maintain it along with the group (i.e., move the group of objects, then update the arrays), I'm suggesting that it may be easier to skip the bit about the arrays and keep all the data in the group itself. It's not so much a language structure per se, but this way of dealing with things is about as close as LC comes to OOP encapsulation. You wouldn't need to define getProp and setProp handlers unless you wanted to something special (see below).

So instead of

Code: Select all

class category {
  name
  type
  color
}

category1 = new category;
x = category1.name;
y - category1.type;
category1.color = "blue";
you could have

Code: Select all

  get the categoryName of group "category1"
  get the categoryType of group "category1"
  set the categoryColor of group "category1" to "blue"
  -- etc.
You could get fancy and implement setProp handlers in the group script which would further the OOP analogy:

Code: Select all

setProp categoryType pType
  set the label of me to pType
  set the categoryType of me to pType
end categoryType

but I wasn't even suggesting that before. I think we'd have to take this discussion out of the beginner's area. <g>

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Data Structures?

Post by dave_probertGA6e24 » Wed Jan 11, 2012 8:01 am

Groups are very useful for encapsulating multiple 'controls' and/or graphics together. Once they are grouped then they can be 'cloned' or whatever and given new 'short' names for ease of identification.

You can also create groups via the code at runtime. You can also change aspect of parts of a group by referring to the subcomponent by it's name too.
eg.

Code: Select all

set the foregroundcolour of field "bar" of group "foo" to "#ff0000"
Adding a bunch of properties/variables to any object is as simple as doing:

Code: Select all

set "jimbob" of group "foo" to "wibble"
now the group has a property "jimbob" (or whatever you want)

Much more info can be found here regarding properties: http://lessons.runrev.com/m/4071/l/11900

I too miss the option of class or struct stuff, but there are ways around it.

Enjoy,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Data Structures?

Post by Klaus » Wed Jan 11, 2012 3:18 pm

Hi guys,

little correction:

Code: Select all

...
set "jimbob" of group "foo" to "wibble"
...
is wrong and will not create a custom property named jimbob but rahter generate an error!

Names of custom properties must not be in QUOTES and the keyword THE will also not hurt!
This is correct:

Code: Select all

...
set the jimbob of group "foo" to "wibble"
...
Best

Klaus

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Data Structures?

Post by dave_probertGA6e24 » Sat Jan 14, 2012 6:56 pm

Hi Klaus,

I didn't know that. Thanks. It's nice to learn something new :)

Cheers.
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

kcorey
Posts: 62
Joined: Fri Nov 25, 2011 6:06 pm

Re: Data Structures?

Post by kcorey » Mon Jan 16, 2012 6:03 pm

Okay, I've played with custom properties now. Wow, they're powerful.

If only we could allow for simple inheritance!

*smile*

Perhaps that world "simple" should be in quotes.

-Ken

Post Reply