ID:967179
 
(See the best response by DarkCampainger.)

Code:
obj
Unit/var
ID = 0 // To make sure each Unit has its own unique ID upon creation
Name = "" // Name of the Unit
Desc = "" // Brief description on the Unit
Type = "" // Whether the Unit is a Land, Air, or Sea Type
Time = 0 // The Amount of Time to Create a Unit
Owner = "" // What Player owns which Unit
Level = 0 // The Level of a Unit
Power = 0 // The Power of a Unit
Health = 0 // The Current Health of the Unit
MaxHealth = 0 // The Max Health the Unit Starts With
list/Costs = list() // The Resources Cost necessary to create a Unit
list/Targets = list() // Unit-Types which a Unit can attack
list/Vulnerable = list() // Unit-Types which a Unit cannot attack
list/Requirements = list() // The Research Requirements needed to create a Unit

obj/Unit

Infantry
icon = 'Infantry.dmi'
icon_state = "Infantry"
Name = "Infantry" // Name of the Unit
Desc = "A foot soldier armed with a automatic machine gun, trained and used for military purposes"
Type = "Land" // Whether the Unit is a Land, Air, or Sea Type
Time = 30
Power = 3
Health = 10 // The Health of the Unit
MaxHealth = 10
Costs = list("Populace", 1, "Steel", 10, "Oil", 10)
Targets = list("Land") // Unit-Types which a Unit can attack
Vulnerable = list("Air","Sea") // Unit-Types which a Unit cannot attack
Requirements = list(/obj/Research/CodeOfArms, /obj/Research/Gunpowder) // The Research Requirements needed to create a Unit

Click()
if(usr.Action != src)
usr << "Unit Clicked"
usr.Unit_Info(src)
else
usr << "Unit Unselected"
usr.Action = ""

New(var/Location, var/ID)
src.Owner = usr
src.ID = ID
src.loc = locate(Location)
usr.Units.Add(src)
usr << "Unit Created!"

mob/proc
Unit_Info(var/obj/Unit/U)
sd_Alert(src,"Level: [U.Level] | Health: [U.Health]/[U.MaxHealth] || [U.Desc]","[U.Name]: [U.ID]")
src.Action = U

Create_Unit(var/obj/Unit/U, var/obj/Building/B)
usr << "Computations for Creating Unit"
var/idNum = 0
var/String = ""
var/Flag = 0

usr << "Prompting for Surety"
for(var/C in U.Costs)
if(istext(C))
String += "[C] "
else if(isnum(C))
String += "[C] Units, "

var/Response = sd_Alert(src,"Create Unit: [U.Name]? || Requirements: [String]","Create Unit",list("Yes","No"))
if(Response == "No")
return
else
usr << "Ensuring Resources for Unit Cost"
for(var/C=1, C<=U.Costs.len, C++)
if(istext(C))
if(C == "Populace")
if(B.Workers >= U.Costs[C+1])
Flag = 1
else
Flag = 0
sd_Alert(src,"You Do Not Have Enough Workers withinn Building, [B.Name]: [B.ID]","Inadequate Workers to Create Unit")
return
else if(Resources[U.Costs[C]] >= U.Costs[C+1])
Flag = 1
else
Flag = 0
sd_Alert(src,"You Do Not Have Enough Resources","Inadequate Resources to Create Unit")
return

usr << "Deducting Resources for Unit Cost"
if(Flag == 1)
for(var/C=1, C<=U.Costs.len, C++)
if(istext(C))
if(C == "Populace")
B.Workers -= U.Costs[C+1]
else
Resources[U.Costs[C]] -= U.Costs[C+1]

usr << "Delay for Spawning Unit"
spawn(U.Time)
for(var/obj/Unit/T in usr.Units)
if(U.Name == T.Name)
idNum++

usr << "Creating unit..."
var/TypePath = "/obj/Unit/[U.icon_state]"
new TypePath (B.Spawn, idNum)


Problem description:
I'm at a stump concerned where when I try to create a Unit, it is not being created. If you may need more information in trying to understand what is happening, I'm willing to give more, but I'm grateful for any input on how to make this work as intended [which is to say, the Unit is to be created]
Use text2path() around that text.
Even with the addition of that procedure, nothing still occurs, it is the same as before.
Is there anything stopping you from doing this?
new U.type(B.Spawn, idNum)
There is nothing wrong with that line but it is the same result nonetheless.
Are you getting the "Unit Created!" message? How far are your debugging statements getting?
They're are going all the way through. Right down to the "Creating Unit..." message. And yes, I am getting the "Unit Created!" message @ start as well

Edit: Meant to say @ the end**
Pass an argument for the mob into the New() proc instead of using usr.
Well, along with that also not working, I've also gained a runtime error of reading nulls [the new argument passed into New() instead of using usr].
I'm not sure but maybe?

Would it make any difference as my map is currently using an isometric map_format? Would this affect me creating objects in anyway? [I find unlikely though because I can create Buildings very easily using similar procedures to this one]
No. Make sure you are passing something into that argument.
// the object's New() proc
New(mob/m, location, id)
src.owner = m
src.loc = locate(location)
src.ID = id

// in the mob/verb
new U.type(usr, B.Spawn, idNum)
And that is exactly what I've done however, when I build my building first, it prompts me with a runtime error of concerning the New() for the Units. I never thought it necessary to include the code around my /obj/Building/ because that wasn't the problem and it was working fine. Upon seeing this runtime error though, I'm confused because I don't call upon anything concerning the /obj/Unit/ [except for when you click on the building], so I've no idea how the runtime error even occurs then. But yes, exactly what you've typed there is what I've done and thus developed this new error.
Best response
Unless B.spawn is a tag, you're misusing locate(). If B.spawn is an object, you want to ultimately set the loc to B.spawn.loc. If B.spawn is a turf, you want to set it to just B.spawn.
My B.Spawn is simply x, y, z. Not a turf, nor object. It's defined after the /obj/Building/ is created.

So its typically, "locate(x,y,z)"
In response to YoukoSieg
It doesn't work like that. I'm assuming you mean that it is a list, and you can't locate() a list.
No, its not actually. When an /obj/Building is built on the map, in the New() for it, its Spawn is defined based on the Buildings current loc, using x, y, z in a string. Then, when an /obj/Unit is created from a Building, it spawns @ the Building.Spawn. Thinking on it now, it makes me wonder if the problem is that the locate() doesn't work with a string.

Does that help clarify it any?
Passing a string into locate() looks for an object with that tag. You'll need to use text functions to convert that string to coordinates (Which isn't very difficult) and send those to locate().
Ah, so that was the problem. Alright, thanks a lot.
Hmmm, now I know I said before that the string being passed to the locate() was the problem, though it seems I was wrong. In my own laziness the night, I didn't quite try to see if it was the correction needed to be made, and only assumed it, not confirmed it. Upon trying to confirm it now though, I've found that's not the case as I am still without my Unit.

I've instead passed the co-ordinates directly to the location of the Unit, using variables to still be given the same result: Nothing.

(Oddly enough however, should I past the values through in actual numbers, they appear with no flaw or problem)

It would seem I am still in need of your counsel, Albro1, and to anyone else willing enough.
In response to YoukoSieg
Could you show us the updated version of the code, in doing what has been instructed?

The only two things I can think of is something wrong with where you're orriginally calling Create_Unit(); Maybe you're making a basic obj/unit/ type(due to the icon_state being null). OR; the already stated wrongful list rather than loc. I use a spawn list variable myself, locate(spawn[1],spawn[2],spawn[3]) works for my spawning needs.
Page: 1 2