ID:2082119
 
Code:
HasSkill(id)
set background = 1
for(var/skill/skill in skills)
if(skill.id == id)
return 1
return 0


Basically this proc goes through a players list of skills and returns 1 if the id matches. I was wondering if there was a more efficient way of doing, maybe without using for()?

proc/hasSkill(id)
if(skills[id]) return 1
return 0
In response to GreatPirateEra
That assumes a list structure there is no evidence for. It can be set up that way, but you should at least explain how to set it up as such.
In response to Popisfizzy
Assuming
var/list/skills = list("id","anotherID")

In response to GreatPirateEra
If that were how it was set up, then the proc you defined would always return 0.
It is set up as

mob/var/skills[0]
In response to Mav472
Mav472 wrote:
It is set up as

mob/var/skills[0]


The [0] is not a good idea; you don't want to initialize the list until it's needed. Just use mob/var/list/skills instead, and don't create the skills list until it's time to add skills. This way, your NPCs and monsters won't be creating a lot of useless lists.

There are a few ways to handle your skills list. One of them is to just add skills directly to it.

if(!skills) skills = new
skills += new_skill

That's one way to go. Another way is to use an associative list. In that format, all of the items in the list are just the IDs, but each one is associated with a skill datum.

if(!skills) skills = new
skills[new_skill.id] = new_skill

Then if you want to see if a given skill is in the list:

return skills && skills[id]
In response to Mav472
Lists generally should be defined as something like
mob/var/list/skills = new
// or
mob/var/list/skills = list()

The specific structure you would need for something like this is,
mob/var/list/skills = list(
"hunting" = new /skill/hunting,
"fishing" = new /skill/fishing,
// etc
)

The important part is the associative list structure, not the exact structure of your /skill class.
In response to Popisfizzy
Popisfizzy wrote:
Lists generally should be defined as something like
mob/var/list/skills = new
// or
mob/var/list/skills = list()


Except they shouldn't be initialized there. That creates a hidden init proc, which you probably don't want running for all mobs in the world. You also probably don't need this list for most mobs.

Far better is to define the list, but leave it null until you're ready to use it--and to return it to null if it ever empties out.
In response to Lummox JR
Very good point. It's probably pretty obvious I don't actually write code for games ever!
I've taken to doing the first part of that, but I never really bother to set it back to null when it goes empty, adding that extra check everywhere I remove items doesn't seem worth the trade off (mostly being lazy not thinking about optimization)
The skills are added to the players list
Each skill in the list has its own variable. Now how do I create a proc that returns 1 if the id matches one of the skills in the players list without using for()
This is completely wrong but I have no idea.
mob/var/list/skills=list()

mob/proc/Acquire_Skill(id)
var/list/skill = skills
if(skill.id = id) return 1
return 0
Associativity.

TL;DR: Set up your list to use strings as indexes:

mob/proc/addSkill(skill/skill)
if(!skills)
skills = list()
if(skills[skill.id]) return skills[skill.id]
skills[skill.id] = skill
return skill

mob/proc/getSkill(id)
if(skills) return skills[id]
return null


skill
var
id = "skill"
testskill
id = "testskill"


Lists can use strings as indexes. That's called associative indexing. It's much faster than looping through the list to just use an associative index to look up the skill. If the list doesn't have an index by that name, the index operation will evaluate to null.
I remember a day when string indexes weren't the normal, then Lummox comes along and says "if(list[index])" is the fastest way to check if some is in a list and I had to rethink the way I setup lists entirely.
then Lummox comes along

The mindblow I got when I found out that object instances, prototypes, appearances, etc. could be used as list indexes. Also Lummox's doing.
It really makes me wish I had gotten my training from Dan later in my life when I knew more already, he was full of these little unknown facts about DM that just rocked and I couldn't appreciate it back then.

Some of my old how-to articles shed light on a few of the ones I did remember but I imagine there's a ton of tricks buried in the language that nobody but Dan knows about.
In response to Ter13
does this work even if the ids are numbers?
does this work even if the ids are numbers?

I wouldn't recommend numeric ids.
In response to Mav472
Mav472 wrote:
does this work even if the ids are numbers?

No; associative lists can't use numbers. It's not because the internals wouldn't allow it, but because syntactically DM treats a number as a direct list index, and anything else as an associative index.
In response to Mav472
Mav472 wrote:
does this work even if the ids are numbers?

var/list/L = list(
"A" = 4,
"1" = 2,
)

L[1] = "A"
L["1"] = 2
L["A"] = 4

L[L[1]] = "4" // L[1] = "A" , L["A"] = 4
Page: 1 2