ID:174222
 
I have a feeling I am going to become a pain before this is over :) Used to program in BASIC and QBASIC so thought this wouldn't be too hard to pick up, but maybe I am just too old.

At any rate. I am having a problem with variables...here are the sections of code involved;

obj/MenSoldiers
Stat()
var
msattack = 4
msarch = 0
msdef = 4
msmoral = 23
icon = 'mensoldier.dmi'
name = "Men Soldiers"

If I am correct this assigns the variables to the object MEN SOLDIERS.

Now, when I try to "call" the variables;

client/Click(atom/O, turf/T)
if(!isturf(T)) // click in statpanel; T is a string here
return
// O might be an obj, or T itself
// find if there's an object on T
O = locate(/obj) in T
if(O)
usr << "Move [O]"
usr << "Attack Rating [msattack]"
else
usr << "Can't move [T]"

The problem I am getting is...when I compile the porgram it returns the error and warning;

fantasy.dm:120:error:msattack:undefined var
fantasy.dm:78:msattack :warning: variable defined but not used

Of course, I get the defined but not used for the variables I am not calling. But how can MSATTACK be defined and not used, and not defined??

Thanks,

Mark

P.S.: Not only is this a great program, but it has one of the most supportive group of users I have ever met!
Your problem is your useing a Stat() proc, a stat proc is used when you want to make a stat pannel, now im really not good at explaning things and such, but to define variables do something like:
obj/MenSoldiers
var
msattack = 4
msarch = 0
msdef = 4
msmoral = 23
icon = 'mensoldier.dmi'
name = "Men Soldiers"


I hope that helps, im in school and im tired, so i might've messed up in there some where or not helped at all o.o;;

~Kros
In response to K'ros Trikare
Not that it makes any difference, but I prefer to write it more like this :

obj/Mensoldiers
icon = 'men.dmi'
icon_state = "MenSold"
var
msattack = 4
msarch = 0
msdef = 4
msmoral = 23

~GokuSS4Neo~
In response to K'ros Trikare
If I do it that way (which was the way I originally had it) it does not give me the defined but not used error, but it still says it is undefined when I call it.

Mark
In response to Gokuss4neo
You're right...I tried it that way and it didn't make a difference :)

Is there any particular reason you prefer that way though? Trying to learn here :)

Mark
In response to AdminBiff
I just think it looks neater and more legible. Because when you get to making the big time giant programs, the last thing you want is for it to be scruffy and confusing!
Also I like to have the icon bit first, then density, then any other predefined vars, then the rest!

Also, put lots of comments. I can see you already have a lot, but always keep it in mind!

~GokuSS4Neo~
the reason it says msattack is undefined, is because you didn't tell which atom to call the msattack with. Since you didn't put anything, it defaults to src, which is the client. you should try making the vars a client/var instead of obj/var
In response to Airjoe
the reason it says msattack is undefined, is because you didn't tell which atom to call the msattack with. Since you didn't put anything, it defaults to src, which is the client. you should try making the vars a client/var instead of obj/var

OK, and how do I make it a client/var connected to the obect. (If that even makes sense :))

What would the syntax be?

Thanks,

Mark
In response to AdminBiff
well, if you want it to apply to basically everyone, you could use

var/VARHERE //everyone
var/global/VARHERE //world var
atom/var/VARHERE //all areas, turfs, mobs, and objects have the var

etc.

Get it? =)

<-Airjoe->
In response to AdminBiff
AdminBiff wrote:
the reason it says msattack is undefined, is because you didn't tell which atom to call the msattack with. Since you didn't put anything, it defaults to src, which is the client. you should try making the vars a client/var instead of obj/var

OK, and how do I make it a client/var connected to the obect. (If that even makes sense :))

You don't, and not much. What you need to do is type-cast O from client/Click() into a var/obj/MenSoldiers first, then use X.msattack (where X is whatever var name you pick).

I'd expound on this with some code, but it looks like you've taken an example I gave you and stretched it out of its original context. If you're looking to handle clicks for some kind of strategy game, I suggest redirecting the click to the object, and letting each object handle the click:
O = locate(/obj) in T
if(O)
O.Click()
However K'ros was also correct in that defining msattack and other vars inside Stat() was wrong; then those vars are only local to the Stat() proc, and don't belong to the object itself. Take that var block outside of Stat().

Lummox JR
/* The problem is likely that you are not telling the compiler which atom the variable you are trying to use belongs to. For instance, say I have the following mob: */

mob/soldier
var
attack = 1

/* If I want to refer to the soldier's attack variable, I first have to tell the compiler that I want to use a variable which belongs to the soldier mob. Assuming the following lines are appearing inside a verb or proc: */

var/mob/soldier/S //a new variable which references a mob\
of the soldier type. So if I wanted\
to show usr (assuming usr is also\
the player, which it often is not!) the\
soldier's attack stat, I would use\
something like this:

usr << "The soldier\'s attack is [S.attack]."

/* In the example above "S" is a soldier mob, "attack" is the variable, and the "." tells the compiler I am referencing a variable, verb, or proc which belongs to the atom which precedes it. Hope that clears things up a bit.*/

Nevermind, guess I was too late :/
In response to Lummox JR
Exactly what I did and am trying to do :)

Since your code worked so well to begin with :)

OK, thanks for the help I will give it a try.

Mark
In response to AdminBiff
I'll try to put it in words as simple as possible.

Pay attention to the variable's scope. (Scope, simply put... uhh, what it's availible to.)

If you define a variable under "obj", it can ONLY be used be reference to objects. So if you try to use it in say "client", you'll need to difine the reference as that type. (EX: MyProc(/obj/VarName)) If you define it under "atom", it is like typing it multiple times in "area", "turf", "mob", and "obj". But it will only be accecible to thoes types. ("client", "world", etc. not included) If you do have a variable defined there, you can reference it as an "atom". (EX: MyProc(/atom/VarName)) If you try to access it in a scope that doesn't have that var defines in it, it will return the "undefined var" error.

That is the basics. There's a little more to know, but this should answer your question in full. I hope. =]