[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUDs
. -> [Folder]  General
. . -> [Subject]  Individual variables vs. table
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Individual variables vs. table

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Baron Sengir   USA  (29 posts)  [Biography] bio
Date Fri 13 Nov 2009 05:18 AM (UTC)
Message
I have a group of powers that all fall under one category for the MUD. Each one individually can be raised from 0 - 5. I see two ways I could do this, and was wondering opinions as to which way would be better.

Option 1:
Create a variable for each individual power. The plus side to this is that it would be rather easy for me to do as I have gotten the hang of manipulating variables. The downside is that it is a lot of different variables to write.

Option 2:
Create an array for a table for each power name. The plus side is that they will be grouped together, and it would be a lot less variables to write out for reading/writing to files. The downside is that I am still learning my way around creating/manipulating tables.

Take for example if I had a group of powers: Elemental Powers (earth, fire, wind, water). I could read/write this to the pfiles in both options.

1 (multiple variables):
int ch->elemental_earth;
int ch->elemental_fire;
int ch->elemental_wind;
int ch->elemental_water;

2 (as an array):
struct elemental_power
{
int earth;
int fire;
int wind;
int water;
}

struct elemental_power elements[3];

and in merc.h have my defines:
#define EP_EARTH 0
#define EP_FIRE 1
#define EP_WIND 2
#define EP_WATER 3

Sorry this got longer than I thought it would when I started. Anyway, my question stands is there any advantage to using one method over another? I can see some pros and cons, but since I am still rather new at this I'd rather hear a little feedback from experienced users/programmers. Also, is one way more memory intensive over the other? Of course, if there is another option that is better than either way, I am completely open to possibilities. Thanks again!

Baron Sengir
aka Co-Admin/Head Coder of "Dark Ages: Legendary Secrets"
Currently in R&D Phase
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Fri 13 Nov 2009 05:44 AM (UTC)
Message
Baron Sengir said:

struct elemental_power elements[3];

and in merc.h have my defines:
#define EP_EARTH 0
#define EP_FIRE 1
#define EP_WIND 2
#define EP_WATER 3


Crash alert! This example defines a table with 3 elements and already you are planning to put 4 things into it. It should be:


struct elemental_power elements[4];


I think I would go with the table myself. It is easier to serialize (read/write to disk) and you can always add another power later on without having to change a whole heap of things.

You may also find it easier later on if you want to make things that affect the powers. For example, a sword might give a +5 to the "fire" power. So that could be expressed as something like:


int increases_power;    // which would be 1 for fire
int by_how_much;        // which would be 5 for +5


Of course, if the sword increases both fire and wind, then you need a different solution, but an array of the things that it increases could still work.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Baron Sengir   USA  (29 posts)  [Biography] bio
Date Reply #2 on Fri 13 Nov 2009 06:04 AM (UTC)
Message
Ah many thanks on the crash catch (completely missed it as I wrote that up 'on the fly' for an example). For some of the other groups of powers, I can see the ease of adding new members to the group as a big plus. However, I really wouldn't need it to increase anything as per items, only through players expending experience to 'level up' the powers as they go. I am going to take a look at how some of the other arrays are serialized (if I'm using that wrong please correct me) to the pfiles. But the table definitely seems to be the route I'm going to go for this. Thanks again!

Baron Sengir
aka Co-Admin/Head Coder of "Dark Ages: Legendary Secrets"
Currently in R&D Phase
[Go to top] top

Posted by ThomasWatts   USA  (66 posts)  [Biography] bio
Date Reply #3 on Fri 13 Nov 2009 06:47 AM (UTC)
Message
You forgot heart in your list of the elements. Captain Planet requires heart.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #4 on Fri 13 Nov 2009 03:37 PM (UTC)
Message
I agree with Nick: having a table lets you treat each entry dynamically. You can refer to a specific entry by "name" (well, by number) which lets you do all kinds of neat things later on. There is relatively little reason to use individual variables, really, other than maybe slightly easier access in code.

e.g.,
ch->earth_power
vs.
ch->power[EP_EARTH]

but personally I don't see those as terribly different, and the array has enough advantages that I'd just stick with it.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Baron Sengir   USA  (29 posts)  [Biography] bio
Date Reply #5 on Fri 13 Nov 2009 05:53 PM (UTC)
Message
ThomasWatts said:

You forgot heart in your list of the elements. Captain Planet requires heart.


I thought about that, but didn't want to be accused of plagiarism. :p

So far I think the array seems to be the best, I was just getting more informed opinions in case there was something I missed. Thanks again guys, and thanks Nick for this site. It is an invaluable tool.

Baron Sengir
aka Co-Admin/Head Coder of "Dark Ages: Legendary Secrets"
Currently in R&D Phase
[Go to top] top

Posted by Wjmurdick   (13 posts)  [Biography] bio
Date Reply #6 on Sat 14 Nov 2009 08:34 PM (UTC)
Message
Just curious, but did you want to create an array of structs? So you'd have 4 entries each with an Earth, Fire, Wind, Water variable?

Cause that is what you will get with this:
Quote:
struct elemental_power elements[4];


I might recommend something such as:


int elemental_powers[4];


Then your #defines could be used as indices.


ch->elemental_powers[EP_WIND]
[Go to top] top

Posted by Baron Sengir   USA  (29 posts)  [Biography] bio
Date Reply #7 on Sat 14 Nov 2009 10:04 PM (UTC)
Message
Actually I wanted to use both arrays of structs and just simple arrays. Reading back on my previous posts I realize how mixed up it actually sounded, and my apologies. For some of the items I will be using just an array, but others will call for more variables, such as a ritual table that will track multiple items, and have the variables for each as name, level, learned/unlearned. As usual, when it seems I get to read/respond to the forum and code in general, it is later at night. I blame the mistakes on borderline exhaustion. haha. But thanks for the insight, I will definitely be using both methods for different things.

Baron Sengir
aka Co-Admin/Head Coder of "Dark Ages: Legendary Secrets"
Currently in R&D Phase
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


5,995 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]