Data Structures?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Data Structures?
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
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
Re: Data Structures?
LOL.Also, I *love* hearing "RTFM" if you'll just tell me which "FM" to read (extra points for links to relevant articles).
My first take on this is that you're not over-engineering things, but you might want to:Is there a better way to do this sort of thing in LiveCode? Am I over-engineering here?
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.
Re: Data Structures?
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
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
Re: Data Structures?
LOL again... well, *you're* the one who mentioned sprites in the first place.Do I create a template object with the properties I want, and then clone it?
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
Re: Data Structures?
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
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
Re: Data Structures?
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
you could have
You could get fancy and implement setProp handlers in the group script which would further the OOP analogy:
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";
Code: Select all
get the categoryName of group "category1"
get the categoryType of group "category1"
set the categoryColor of group "category1" to "blue"
-- etc.
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>
-
- Livecode Opensource Backer
- Posts: 328
- Joined: Mon Dec 05, 2011 5:34 pm
- Contact:
Re: Data Structures?
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.
Adding a bunch of properties/variables to any object is as simple as doing:
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
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"
Code: Select all
set "jimbob" of group "foo" to "wibble"
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.
Visit http://electronic-apps.info for released App information.
Re: Data Structures?
Hi guys,
little correction:
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:
Best
Klaus
little correction:
Code: Select all
...
set "jimbob" of group "foo" to "wibble"
...
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"
...
Klaus
-
- Livecode Opensource Backer
- Posts: 328
- Joined: Mon Dec 05, 2011 5:34 pm
- Contact:
Re: Data Structures?
Hi Klaus,
I didn't know that. Thanks. It's nice to learn something new
Cheers.
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.
Visit http://electronic-apps.info for released App information.
Re: Data Structures?
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
If only we could allow for simple inheritance!
*smile*
Perhaps that world "simple" should be in quotes.
-Ken