ID:2176036
 
(See the best response by Kaiochao.)
Is there a better way to handle skillcards? I mean looping through all_skills list every time a skill is added is a real pain, especially if the game has over 500 skills. I'm just trying to figure out how to return a skill type without having to loop through the list in order to find it.

var/list/all_skills=list()

world/New()
for(var/type in typesof(/skill))
all_skills += new type()

mob/proc/
AddSkill(id)
var/skill_type = SkillType(id)
var/skill/skill
if(!skill_type)
skill = new /skill()
skill.id = id
skill.name = ""
else
skill = new skill_type()
skills += skill
new /skillcard(src, skill)
return skill

proc
SkillType(id)
for(var/skill/skill in all_skills)
if(skill.id == id)
return skill.type




Use an associative list. Assuming id isn't a number, you can associate the id to the skill with that id (id_to_skill[id] = skill), then get the skill associated to the id (id_to_skill[id]).
In response to Kaiochao
The id is a number. Here are ids converted to text using ters method. How do I create a skill card though, because technically the id is still a number represented by a text. I have no idea how to get information from the list like a skills cooldown/name etc. So when the savefile tries to check a skills name I get errors like "Cannot read "1004".name or 1004.name.
AddSkill(skill/skill)
var/idstr="[skill]"
if(!skills)skills=list()
if(skills[idstr]) return skills[idstr]
skills[idstr]=skill

return skill
In response to Mav472
var skill/skill = skills[idstr]
world << skill.name
In response to Kaiochao
That returns as an error Cannot read "1".name
In response to Mav472
That's probably because you're passing the ID value to AddSkill() instead of the actual skill object.
In response to Kaiochao
I'm not sure how to create the skill object based on the associative list. Is there a way to return the parent object using its id value?
Best response
In the first place, why are you bothering to do this? Why do skills have IDs? Why not identify skills by type?
var list/all_skills

proc
locate_skill(type)
var skill/skill = locate(type)
if(!skill)
if(!all_skills) all_skills = new
skill = new type
all_skills += skill
return skill

skill
var
name
a
name = "a"
b
name = "b"
c
name = "c"

// example
mob/Login()
src << "[locate_skill(/skill/a)]"
src << "[locate_skill(/skill/b)]"
src << "[locate_skill(/skill/c)]"
In response to Kaiochao
I'm honestly really sorry Kaiochao. I just need an efficient way to handle skills; adding skills to a players list, creating skill cards, saving/loading information from the skills like cooldowns and uses. The skill ids handle skill purchases. I'll use this method but it doesn't seem anymore efficient. I'll still have to loop through long lists to retrieve information.
In response to Mav472
I'll take a step back and start from the code you gave in the original post, because I think I misinterpreted something along the way.

Original:
var/list/all_skills=list()

world/New()
for(var/type in typesof(/skill))
all_skills += new type()

mob/proc/
AddSkill(id)
var/skill_type = SkillType(id)
var/skill/skill
if(!skill_type)
skill = new /skill()
skill.id = id
skill.name = ""
else
skill = new skill_type()
skills += skill
new /skillcard(src, skill)
return skill

proc
SkillType(id)
for(var/skill/skill in all_skills)
if(skill.id == id)
return skill.type

Here's what it could look like with an associative list:
// this is changed to map ids to types
var list/skill_id_to_type = new

world/New()
for(var/type in typesof(/skill))
var skill/skill = new type
skill_id_to_type["[skill.id]"] = type

// this is untouched
mob/proc
AddSkill(id)
var skill_type = SkillType(id)
var skill/skill
if(!skill_type)
skill = new /skill
skill.id = id
skill.name = ""
else
skill = new skill_type
skills += skill
new /skillcard (src, skill)
return skill

// this no longer uses a loop
proc
SkillType(id)
return skill_id_to_type["[id]"]

A bonus here is that skill_id_to_type is also effectively a list of all Skill IDs that exist:
mob/verb/list_all_skill_ids()
for(var/id in skill_id_to_type)
src << "Skill ID: [id], Type: [skill_id_to_type[id]]"
In response to Kaiochao


This is great though. Thanks a ton bro
In response to Kaiochao
skill_id_to_type[skill.id] = type
getting an index of of bounds here
In response to Mav472
I forgot you were using numbers for id... You'll have to convert to text like before. I edited my previous post.