ID:264819
 
Code:
mob/NPC
icon='mob.dmi'
npc=1
Resource_trader
icon_state="trader"
DblClick()
if(usr.oz)
if(usr.oz==6)
var/a=input("Would you like to buy resources?") in list ("Yes","No")
switch(a)
if("Yes")
for(var/obj/consoles/ships_computer/S in world)
if(S.z==usr.oz)
if(c1slots.len==0)
for(var/obj/slots/cargo/cargo1/c in world)
if(c.z==S.z&&c.contents.len==0)
c1slots.Add(c)
if(c2slots.len==0)
for(var/obj/slots/cargo/cargo2/c in world)
if(c.z==S.z&&c.contents.len==0)
c2slots.Add(c)
if(c3slots.len==0)
for(var/obj/slots/cargo/cargo3/c in world)
if(c.z==S.z&&c.contents.len==0)
c3slots.Add(c)
if(c4slots.len==0)
for(var/obj/slots/cargo/cargo4/c in world)
if(c.z==S.z&&c.contents.len==0)
c4slots.Add(c)
var/b=input("Which resource do you want to buy?") in list ("plastic level 1","plastic level 2","plastic level 3","steel","aluminum","tungsten","carbon fiber","coolant","fuel","oil","uranium","rubber")
switch(b)
if("plastic level 1")
var/c=input("How much of the resource do you want to buy?") in list ("One cargo bay","Two cargo bays","Three cargo bays","Cancel")
switch(c)
if("One cargo bay")
if(c1slots.len>=1)
if(c1contents.len==0)
for(var/obj/slots/cargo/cargo1/c1 in c1slots)
//text2path
var/string=b
var/replace="_"
var/find=" "
var/length = lentext(find)
var/pos = findtextEx(string, find)
string = copytext(string, 1, pos)+replace+copytext(string, pos+length)
length = lentext(find)
pos = findtextEx(string, find)
string = copytext(string, 1, pos)+replace+copytext(string, pos+length)
string=text2path(string)
var/P
P=text2path("/obj/items/resources/[string]/")
if(ispath(P,/obj/items/resources/plastic_level_1/))
usr<<"Path true."
c1contents.Add(P)
else
usr<<"Not a path"
//var/obj/items/resources/plastic_level_1/P = new /obj/items/resources/plastic_level_1
usr<<"Thank you for purchasing [b]."


Problem description:

I'm trying to simplify my resource system with text2path and it isn't producing valid paths. I've been working on it for a while and nothing seems to be working. I got the replace text code from another thread, [link] , and tweaked it to my needs. The replacement works. It changes the spaces into underscores. However, the variable is not outputted as a valid path. I followed the example of text2path from the DM reference. I pasted much more code than was needed, but I have had major problems in the past from only posting snippets and the thread gets out of hand fast. The commented out part at the end was the old way I was doing it, and it required a ton of copy and pasting for every resource. So I wanted to simplify it.
Use an associated list rather than futzing around with text strings and type paths. Like so:

var/list/resource_names = list(
"plastic level 1" = /obj/items/resources/plastic_level_1,
"plastic level 2" = /obj/items/resources/plastic_level_2
)


Then, solicit input from the list. resource_names[input] will then give you the type path of the selection.
In response to Garthor
Thank you, for the suggestion. I actually used an associated list recently, with the sensor system integrated into the weapons console, but I just was getting very frustrated that this was not working and didn't know what else to do.
In response to Justin Knight
They're not too easy to pick up on, but read into it, they open up a lot of abilities when coding.
Remember, while coding, F1 is your best friend.
In response to Bravo1
I know how to use associated lists. I said I already used them before. I've taken Data Structures before. It reminded me of that class. I was going to mention it but I didn't want to make too long of a post.
In response to Justin Knight
Yup. The more common term for the data structure would be "map".
In response to Garthor
After consulting the DM reference on how they work with list associations and working it into my own stuff to simplify it, I got most of it done and working fine, but I can't figure out the last part. That is, taking the associated part and using it in the type path. I know how to take out the associated part. I figured that out from the DM reference. I see that a type path is a read-only variable, though. It's not like I can just plug in the variable at the end to give me the type path. It gives me a runtime error "bad type" that way.

var/list/rlist = list(
"plastic level 1" = /obj/items/resources/plastic_level_1,
"plastic level 2" = /obj/items/resources/plastic_level_2,
"plastic level 3" = /obj/items/resources/plastic_level_3,
"steel" = /obj/items/resources/steel,
"aluminum" = /obj/items/resources/aluminum,
"tungsten" = /obj/items/resources/tungsten,
"carbon fiber" = /obj/items/resources/carbon_fiber,
"coolant" = /obj/items/resources/coolant,
"fuel" = /obj/items/resources/fuel,
"oil" = /obj/items/resources/oil,
"uranium" = /obj/items/resources/uranium,
"rubber" = /obj/items/resources/rubber,)

var/b=input("Which resource do you want to buy?") in rlist
switch(b)
if("plastic level 1")
var/c=input("How much of the resource do you want to buy?") in list ("One cargo bay","Two cargo bays","Three cargo bays","Cancel")
switch(c)
if("One cargo bay")
if(c1slots.len>=1)
if(c1contents.len==0)
for(var/obj/c1 in c1slots)
/*var
i=0
p=0
for(i=1,i<=1,i++)
p = rlist[i]*/

//usr << "p: [p] = rlist(p): [rlist[p]]"

usr<<"Thank you for purchasing [b]."


By the way, I had no idea what you meant when you said the proper term for data structures was "map". I was referring to Java, and all the different uses of arrays, stacks, queues, stuff like that. This was a college course I was talking about. It had nothing to do with BYOND. You really confused me when you mentioned the term "map".
In response to Justin Knight
If you store your selection in the variable "selection", then to create an object of the desired type, you would use

new rlist[selection](usr)


As for the data structure... I'm not saying all data structures are called maps, I'm saying that the data structure in use right here is called a Map.
In response to Garthor
Thanks for the help, Garthor, and the speedy reply.

I had the syntax almost exactly the same as you had it there, except for the (usr) at the end. However, the resources are not being added to the user's contents. They are being added to the ship.

for(var/obj/c1 in c1slots)
var/obj/items/resources/plastic_level_1/P = new /obj/items/resources/plastic_level_1
c1contents.Add(P)
P.loc=c1.loc


That's how I did it before I wanted to simplify the system.

The data structures thing is my fault. I thought you were referring to BYOND map files, .dmp's. We might not have gone over the name for that in the class, and if we did I might have forgotten. The class was very fast paced and we went through a lot of masterial. Plus, I took the class maybe 2 years ago so I don't really remember it that well.
In response to Justin Knight
Then you'd do:

var/obj/O = new rlist[selection]()
O.loc = whatever
In response to Garthor
Garthor wrote:
Then you'd do:

var/obj/O = new rlist[selection]()
> O.loc = whatever


for(var/obj/c1 in c1slots)
var
i=0
selection=0
for(i=1,i<=1,i++)
selection = rlist[i]
//usr << "p: [p] = rlist(p): [rlist[p]]"
var/obj/O = new rlist[selection] ()
c1contents.Add(O)
O.loc=c1.loc


It gives me an error on the line with the new rlist[selection] () saying invalid expression.
In response to Justin Knight
Store the value in a temporary variable and use that instead. BYOND has a few problems with accessing data in lists directly.
var/class_to_initialise = rlist[selection]
var/obj/O = new class_to_initialise(parameters_like_location)
In response to Schnitzelnagler
Thank you for your help. The system works now, and I can simplify it greatly. Now I just have to do it for the other 12 resources and their 3 different quantities.