ID:2411000
 
(See the best response by Nadrew.)
Code:
/*
This is the affinity system which the NPC will keep a list of players of whom he likes the most,
whoever is at the top of that list will get a special quest from that NPC or a Item whichever the NPC is willing to give.
*/


mob/NPCs/Interactive
var
affinityListNames = newlist("NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL")//This is the list that every NPC will have that will have the players names stored in.
affinityListAffin = newlist(0,0,0,0,0,0,0,0,0,0)//This is the affinity Levels which corresponds with the location of the name in the other list.
proc
CheckTopAffin()
for(var/i = 1 to affinityListAffin.len)


Problem description:

Im pulling my hair out because i try to follow the guides and im getting errors left and right. idk if its just because i dont really understand this language completely yet but what im trying to do is to have NPCS keep a list of players that they like its an affinity system. And i want them to be checking that list to see who is at the top of the list and when they next talk to the NPC they will recieve something special.

I am getting an error at affinityListAffin.len
Best response
Firstly you need to cast them as lists to access their list variables and functions at compile-time,

var/list/mylist


Next, you don't want newlist() here, just list().

var/list/mylist = list()
// or
var/list/mylist = new()
// or
var/list/mylist[]


You don't need to initialize any elements here since you'll be adding data per-player.

Next, you'll probably want an associative list instead of two lists.

var/list/mylist = list("Name"=10,"Name2"=20)


This would make mylist["Name"] come out as 10, so you'd just add the player's name to the list, then use the associated value to determine the affinity. You can also loop directly over the contents of a list without using another variable.

ShowStuff()
for(var/I in mylist)
src << "[I] = [mylist[I]]"


This would output

Name = 10
Name2 = 20


Using the above list.

For adding and removing from the list, you can simply use +=, but since we're gonna be using associated values, you don't even need to go that far.

AddMe()
mylist[src.name] = 0


Would add src's name to the list with an associated value of 0.

And for changing/setting the value it's as easy as

ChangeValue(newvalue as num)
mylist[src.name] = newvalue


And removing is as easy as

mylist -= src.name
I will put that in later thanks so much for the suggestion.
I will ask more questions later.
My idea works for now as a global variable, Now can i make classes in this language and have each NPC have their own private lists and have access to the procs to change value and to add
Your original method of putting the variables within the /mob/NPCs/interactive was on the right track.

obj
parent
var/some_var = 10

child_one
some_var = 20

child_two
some_var = 15

child_three
some_var = 25

proc/ShowValue(mob/caller)
caller << "[src]'s some_var is [src.some_var]"


Then if you call the object's ShowValue(), passing the player through the argument

mob/verb/Test(obj/parent/P as obj in view())
if(istype(P,/obj/parent)) // just in case
P.ShowValue(usr)


You'll get a different value based on the type of the object, all objects under a parent will share its procs and variables, you can override the procs and call the parent of that proc with ..()
So with that all NPC'S will have their own lists containing players and their affinities towards them? Can you explain a little more please.
Variables defined under a specific type will be individual to objects created of that type, so yes, each NPC would have their own list.

How you save and load that data is up to you though.
What if i saved it in a save file in the server but how would i put it back in the NPC?
You'd either want to save a list of the NPCs (not recommended because you'd have to save them all anytime you needed to save one), or give them some kind of unique identifier and save them the same way you would a player, using that identifier instead of something like ckey.
Can i use their appointed names?
If said name is unique to that NPC and nothing else, and you'd likely want to strip it down to a file-name-friendly format using ckey()
I ended up having the players keep a list of the NPC's names and their affinity towards them. And it will be in the player's save file so the NPCS wont need a save file using more space and more cpu.