ID:141184
 
Code:
var/list
Bomb_Atom = list()
Bomb_Hydrogen = list()

obj/bombs
Atom
//...
Hydrogen
//...
New()
..()
var/a = pick("A","B","C")
name = "Grade [a] [name]"

proc/Sort()
if(istype(src, /obj/bombs/))
var/a = "[src.type]"
var/b = copytext(a, 12)
var/c = text2path("Bomb_[b]")
c += src


Problem description:I want to move the object into the appropriate list. I used copytext to grab the name I need but can't find a way to use "var/b" to get the object into the list.

EDIT: A misc note:
Jester619 wrote:
I want to move the object into the appropriate list.

The right words here are more like "put" or "add", not "move". Don't confuse lists with the movement system (or the special contents list) - when you add, say, an object to a list, you're not moving it. Similarly, you can add the same objects into multiple lists, so treating list-adding as moving is misleading. The only time movement would happen is when the special contents list is modified, but that only applies to that one unique list.
If you weren't looking at it like that anyway, sorry, I'm just making sure. =P It's a common misconception.

used copytext to grab the name I need but can't find a way to use "var/b" to get the object into the list.

Well, you just can't do it like that, the way you've tried. Let me show you what your code actually executes like:
>           var/b = "Atom" //for example
//the next line tries to convert "Bomb_Atom" into a path.\
but that's invalid - there is no such path. \
So it just returns null...

> var/c = null
> null += src //this sets c to src
>


First, basically you can't modify anything with only a type path. You need an actual object whom to change. Second, you've went about it entirely wrong here; as said, no such type paths as "Bomb_Atom" will ever exist:
  • That's just a variable name, that's very different from a path.
  • (A valid path must begin in "/" anyway =P)

So, you need another way, one that actually makes sense to the computer. I'd scrap what you currently have and do it quite differently, using the Power of Objects (/OOP).

First, about your current code: your istype(src,/obj/bombs) check is very redundant. Since that proc is one defined on the /obj/bombs type, the code in it will only ever execute on objects of that type - validating that is just a waste of time. In fact, if you're doing things properly, you should pretty much never have to check src's type at all, so keep that in mind.

Anyway, one method to do this could be this:
var/list
all_bombs = new
atom_bombs = new
hydrogen_bombs = new
//note: you should actually NOT initialize those lists \
until they're actually needed (and uninitialize them once \
they're not needed)


obj/bomb //die, plural type node names!
New()
...
src.update_list()
proc/update_list()
all_bombs += src //by default, add self to 'all_bombs'
Hydrogen
update_list() //override the proc for the Hydrogen type...
..() //do the parent proc above
hydrogen_bombs += src //also add self to 'hydrogen_bomb'
Atom
update_list()
..()
atom_bombs += src

You could also do it dynamically for all the bomb types that will ever be in your game with a different sort of method (using a global associative list to keep track of the list of every bomb type). This may or may not be preferred, depending on your game:
var/list/bombs = new
obj/bomb
...
proc/update_list()
bombs["all bombs"] += src //if you want, to keep it similar to the example above

if(!(src.type in bombs))
//if it's not already there, add the type to the list and associate it with a new list
bombs[src.type] = new /list()
bombs[src.type] += src
Hydrogen
Atom

The bombs list would then look like this at runtime, if you had 1 Hydrogen bomb and one Atom one initialized:
bombs = \
list("all bombs"=list(Hydrogen_instance,Atom_instance),
/obj/bomb/Atom=list(Atom_instance),
/obj/bomb/Hydrogen=list(Hydrogen_instance))

Legend: X_instance = a reference to an object instance of that type


Lastly - don't forget to remove the bombs from whatever list(s) they're in when they're deleted (by overriding Del())! This isn't what you'd call 'strictly necessary', but it's a very good idea.
In response to Kaioken
None of those are how I want to do it, although you did give me a lot of useful tips and advice, which I'm very grateful for. <3

I don't have the original codes where I ran into this problem with, so what I posted was a quick remake of the problem. Again, I could probably have clarified things a lot better, so I'll try.

The reason that I was checking the objects type, is because I will be using this Sort() proc for a multitude of objects, all with different paths. What I was looking to do was to pull out the necessary text from the objects path, and use that to add the object to the correct list. Sort of a modular approach, one proc to handle them all. (LotR reference not intended -_-)

So say I have an object, a list and a string, like so:
var/list/Iron_Ore = new

obj/ore
Iron
//...

proc/Sort()
//blah blah, we end up with..
var/a = "Iron"


Is there any way to use var/a to find the appropriate list and add the object to it? Perhaps with the use of vars[]?
In response to Jester619
Jester619 wrote:
None of those are how I want to do it, although you did give me a lot of useful tips and advice, which I'm very grateful for. <3

I don't have the original codes where I ran into this problem with, so what I posted was a quick remake of the problem. Again, I could probably have clarified things a lot better, so I'll try.

The reason that I was checking the objects type, is because I will be using this Sort() proc for a multitude of objects, all with different paths. What I was looking to do was to pull out the necessary text from the objects path, and use that to add the object to the correct list. Sort of a modular approach, one proc to handle them all. (LotR reference not intended -_-)

So say I have an object, a list and a string, like so:
var/list/Iron_Ore = new
>
> obj/ore
> Iron
> //...
>
> proc/Sort()
> //blah blah, we end up with..
> var/a = "Iron"

Is there any way to use var/a to find the appropriate list and add the object to it? Perhaps with the use of vars[]?

There is, but I caution against it. Directly accessing the vars list is usually not the best way to do things and can do a lot of unintended things.

Consider, though, that the vars list of an object is much like any other list in the game. Variable names are stored as text, with their values stored as associated values: list("var"="value").

With that in mind, you should be able to figure out how to turn "Iron" in to something that can be accessed via the vars list.

Now, with that said, I highly recommend finding a different approach. Perhaps storing your lists in a global list?
var/list/big_list = list()
//..
big_list["iron_ore"] = list()
big_list["copper_or"] = list()


You can use the same principles, but you avoid messing with the built in vars list.
In response to Jester619
Jester619 wrote:
None of those are how I want to do it, although you did give me a lot of useful tips and advice, which I'm very grateful for. <3

Sure, happy to do that.

The reason that I was checking the objects type, is because I will be using this Sort() proc for a multitude of objects, all with different paths. What I was looking to do was to pull out the necessary text from the objects path, and use that to add the object to the correct list. Sort of a modular approach, one proc to handle them all.

Sounds like pretty much exactly what the 2nd method I've posted does. And, in any case you don't need to check src's type, because you should just handle that through object proc overrides, as shown in the 1st method I posted.

[...]
     var/a = "Iron"

Is there any way to use var/a to find the appropriate list and add the object to it? Perhaps with the use of vars[]?

Dunno why you're too bent on doing it exactly one way, but yes, you could do it with any associative list, including the vars list, but that one would require a dummy object to hold that list. You just need to manage an associative list where the appropriate text string is associated with a list of the objects of the appropriate type, i.e. equivalent to list("Iron" = list(Iron objects here)). Interestingly, this is almost the same as the 2nd method in my previous post. Also interesting, CriticalBotch posted the same thing. :P