Forum ActionScript 3.0 How to deal with static data?

Problems and discussions relating to ActionScript 3.0 here.

How to deal with static data?

Postby cog » December 16th, 2012, 1:00 pm

Hi,

This is an open ended question on how to deal with "game data". By this I mean things like:
- level maps
- mission dialogues
- item/unit stats, description, etc.
...
In my case, I have to a lot of creatures (it's what the game revolves around) which all have their description, stats, skills, etc. Quite a few attribute in total.

The question is: how to deal with all this data? Do you make a class per "thing"? Or do you store/access in some way from a file in a more direct format?

Cheers,
Arnaud
cog
 
Posts: 74
Joined: November 17th, 2012, 1:40 pm

Re: How to deal with static data?

Postby cog » December 16th, 2012, 7:43 pm

The only way I found so far is kind of supercomplicated :/

I have an interface "Creature":
Code: Select all
public interface Creature
{
   function picture() :Bitmap;
   function description() :String;
   function health() :int;
   function spirit() :int;
   //...
}


Then, for each creature, a class which implements it:
Code: Select all
public class Lion implements Creature
{
   [Embed(source = "{...}/Lion.png")]
   private const ImgClass :Class;
   private const img :Bitmap = new ImgClass();
   public function picture() {
      return img;
   }
   //...
}


Then a "registry":
Code: Select all
public class AllCreatures
{
   static const lion :Lion = new Lion();
   //...

   static function getByName(name :String) :Creature {
      // ...
   }
}



...but as you see, it's extremely heavy!!! I'm sure there are simpler ways to store the stats of all creatures!

After all, I just want to get the picture and the basic stats of a creature!!! Going through so many hoops definitely seems wrong ...or at least extremely cumbersome.
cog
 
Posts: 74
Joined: November 17th, 2012, 1:40 pm

Re: How to deal with static data?

Postby dreamora » December 17th, 2012, 9:21 am

1. Cache the data on GameRoom
2. Pass the data to all classes that need to use it or provide a reference to the game room to access it.

Simply put you need to implement a pseudo singleton pattern of some kind, be it either by passing the singleton or only passing a reference to the specific data container
dreamora
 
Posts: 225
Joined: March 2nd, 2012, 9:58 am

Re: How to deal with static data?

Postby Henrik » December 17th, 2012, 2:15 pm

What's wrong with dynamic objects, or just making instances of a single Creature class and putting all of them into a container? Making a subclass for each unique creature sounds completely wrong to me, but I'm not very familiar with AS3. :-)
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How to deal with static data?

Postby cog » December 17th, 2012, 2:38 pm

Hi,
That's exactly the way to do it. I just wasn't aware it could be done in AS3, I'm pretty new to it.
The way to do it is simply:
public const Lion = {
health: 20,
spirit: 5,
speed: 5,
// ...
attacks: [
{
name: "Claw Attack",
target: "melee",
damageMin: 3,
damageMax: 6,
damageType: "physical",
cost: 0
}
]
}


...I know, it's pretty straightforward and stupid ...just didn't know it could be done.
cog
 
Posts: 74
Joined: November 17th, 2012, 1:40 pm

Re: How to deal with static data?

Postby seand88 » April 9th, 2013, 7:38 pm

I think hardcoding these in as3 is not very smart.

You can use a data format like json, or xml to store these and then load them in so when you want to change it, can just edit a few values easy, and someone who is not a coder could do it.

for example

<creatures>
<creature name="Lion" health="30" spirit="5" speed="5" />
<creature name="Bear" health="300" spirit="10" speed="3" />
</creatures>

Then just load in these values into an array. When you want to get one of them you can call getCreature("bear"), or w/e creature you are trying to get info about just returns a creature object.

Class Creature
String name;
int health;
int spirit;
int speed;

This class in as3 code obviously, but i think you get the idea.
seand88
 
Posts: 3
Joined: February 6th, 2013, 11:40 pm


Return to ActionScript 3.0



cron