ID:1643809
 
Keywords: auto, code, ranks, tab, updated
(See the best response by Pirion.)

I need help making a Ranks Tab. one where it shows ranks to everyone such as:

Knight Leader-(person name here)
Mage Leader-(person)
Warrior Leader-(person)
I've had no luck so far :/

-Alpha


Best response
Well, you may want to show us what you've had no luck with. Seeing that, we can probably get you in the right direction.
Look at the Statpanel and stat proc to create tabs and use vars for thoes other things
Well, here's my crack at it. I can confirm that it is working, since I actually tested it. I can't confirm that it's the best way, because I don't think there is one. :p

Look at the following code, and I'll tell you about it as I go along. It should be plug and play, but obviously there is nothing to gain if I don't explain what I'm doing.

First, I decided that I'd use a datum for this purpose. Why? We are storing information, and would like to be able to store it in a way that allows us to access it easily, create more at runtime (I imagine), and have it be extensible + versatile.

So, here is the datum I made.

/datum/rank
var/holder
var/title

// Both Holder and Rank should be of type 'text'.
New(Rank, Holder)
..()
title = Rank
holder = Holder


Note how I created the variables 'holder' and 'title'. I also overrode the default New() proc and added two things in it. That allows functionality by passing in parameters to the New() proc when you create a new object. Let's go ahead and go to the next part.

var/list/ranks = list()
proc/InitRanks()
ranks.Add(new /datum/rank("Knight Leader", "Rank Holder"))
ranks.Add(new /datum/rank("Mage Leader", "Rank Holder"))
ranks.Add(new /datum/rank("Warrior Leader", "Rank Holder"))


Above, you'll see that I created an empty list and added to it three new datums of type /datum/rank. Notice to the right, where I made certain to include the name of the rank, and then "Rank Holder"-- This is the important part to you, and what you can edit to get more. However, someone clever might notice that this hasn't actually been called...

So I create this!

world/New()
..()
InitRanks()


I override the default world/New() proc, go ahead and call the other overrides just in case you already have some overrides in place, and then I call the 'InitRanks()' proc that I made in the last one.

If you already have a proc which initializes things or sets stuff up made, I'd recommend moving this bit of code to that proc for code cleanliness.

Now we go on to the root of the matter, since we have constructed our datums and placed them inside of the appropriate list, allowing us to easily access them.

/mob/Stat()
if(statpanel("Ranks"))
var/datum/rank/index
for (index in ranks)
stat("[index.title] -- [index.holder]")


The above is the actual statpanel, as well as the 'stat' that ti shows-- specifically, for each rank first the title and then the person who holds it right beside it.

---------------------------------

The code used, altogether (and plug and play directly), is as follows:

world/New()
..()
InitRanks()


/datum/rank
var/holder
var/title


// Both Holder and Rank should be of type 'text'.
New(Rank, Holder)
..()
title = Rank
holder = Holder



var/list/ranks = list()
proc/InitRanks()
ranks.Add(new /datum/rank("Knight Leader", "Rank Holder"))
ranks.Add(new /datum/rank("Mage Leader", "Rank Holder"))
ranks.Add(new /datum/rank("Warrior Leader", "Rank Holder"))



/mob/Stat()
if(statpanel("Ranks"))
var/datum/rank/index
for (index in ranks)
stat("[index.title] -- [index.holder]")


However, it is only the minimum of a versatile system to get done. I'd recommend seeing about if you can use it to make it a bit more useful. Some things you might wish to include..

A modify verb, that allows you to add and remove from the list of ranks, as well as edit the people who are currently in it.

ASSERT statements to check the types of input and ensure they are correct, and if they aren't, crash the proc. Specifically, funny things will happen if you pass non-text input into the New() proc for /datum/rank objects.

For bonus points, text files which load this up at creation, allow modification, and export it before a reboot, so that it can remain after reboots easily and allow the host to edit it at will in the text file.

Note: I am not an expert, and I sometimes make mistakes. There might be glaring ones in this code. I appreciate pointing them out if you see them, specifically so I can fix it. :p