ID:2472513
 
(See the best response by Ter13.)
Code: Damage Calculator
var/totdmg
var/overallspd // diffence between dodge speed and attack speed
var/atkdmg //attackers damage "grade"
var/atkspd //attackers speed
var/defend //defenders endurance
var/defspd //defenders speed
var/atkgrade

proc/calcgrades()
if(atkdmg== "E")
atkdmg=0
if(atkdmg== "E+")
atkdmg=1
if(atkdmg== "D-")
atkdmg=2
if(atkdmg== "D")
atkdmg=3
if(atkdmg== "D+")
atkdmg=4
if(atkdmg== "C-")
atkdmg=5
if(atkdmg== "C")
atkdmg=6
if(atkdmg== "C+")
atkdmg=7
if(atkdmg== "B-")
atkdmg=8
if(atkdmg== "B")
atkdmg=9
if(atkdmg== "B+")
atkdmg=10
if(atkdmg== "A-")
atkdmg=11
if(atkdmg== "A")
atkdmg=12
if(atkdmg== "A+")
atkdmg=13
if(atkdmg== "S-")
atkdmg=14
if(atkdmg== "S")
atkdmg=15
if(atkdmg== "S+")
atkdmg=16


if(atkspd== "E")
atkspd=0
if(atkspd== "E+")
atkspd=1
if(atkspd== "D-")
atkspd=2
if(atkspd== "D")
atkspd=3
if(atkspd== "D+")
atkspd=4
if(atkspd== "C-")
atkspd=5
if(atkspd== "C")
atkspd=6
if(atkspd== "C+")
atkspd=7
if(atkspd== "B-")
atkspd=8
if(atkspd== "B")
atkspd=9
if(atkspd== "B+")
atkspd=10
if(atkspd== "A-")
atkspd=11
if(atkspd== "A")
atkspd=12
if(atkspd== "A+")
atkspd=13
if(atkspd== "S-")
atkspd=14
if(atkspd== "S")
atkspd=15
if(atkspd== "S+")
atkspd=16


if(defend== "E")
defend=0
if(defend== "E+")
defend=1
if(defend== "D-")
defend=2
if(defend== "D")
defend=3
if(defend== "D+")
defend=4
if(defend== "C-")
defend=5
if(defend== "C")
defend=6
if(defend== "C+")
defend=7
if(defend== "B-")
defend=8
if(defend== "B")
defend=9
if(defend== "B+")
defend=10
if(defend== "A-")
defend=11
if(defend== "A")
defend=12
if(defend== "A+")
defend=13
if(defend== "S-")
defend=14
if(defend== "S")
defend=15
if(defend== "S+")
defend=16

proc/defspds()
if(defspd== "E")
defspd=0
if(defspd== "E+")
defspd=1
if(defspd== "D-")
defspd=2
if(defspd== "D")
defspd=3
if(defspd== "D+")
defspd=4
if(defspd== "C-")
defspd=5
if(defspd== "C")
defspd=6
if(defspd== "C+")
defspd=7
if(defspd== "B-")
defspd=8
if(defspd== "B")
defspd=9
if(defspd== "B+")
defspd=10
if(defspd== "A-")
defspd=11
if(defspd== "A")
defspd=12
if(defspd== "A+")
defspd=13
if(defspd== "S-")
defspd=14
if(defspd== "S")
defspd=15
if(defspd== "S+")
defspd=16

proc/calculatedamage()
totdmg = defend - atkdmg
return
..()
proc/calculateevade()
overallspd = defspd - atkspd
return
..()


// Light damage = 1
// Moderate damage = 2
// Major damage = 4
// Heavy damage = 8
// Instant kill = 16


Problem description:I just want the text input to be transferred to a variable and be calculated later on

If possible contact me on discord Keishi#5713
Best response
Couple tools for you:

Associative lists allow you associate a string, type, to any value. This creates key-value pairs:
var/list/__grade2num = list("E"=0,"E+"=1,
"D-"=2,"D"=3,"D+"=4,
"C-"=5,"C"=6,"C+"=7,
"B-"=8,"B"=9,"B+"=10,
"A-"=11,"A"=12,"A+"=13,
"S-"=14,"S"=15,"S+"=16)
#define grade2num(g) (__grade2num[g]||0)


You can index a list by number, allowing you to convert a number into a string by creating a lookup table:
var/list/__num2grade = list("E","E+","D-","D","D+","C-","C","C+","B-","B","B+","A-","A","A+","S-","S","S+")

#define num2grade(n) (__num2grade[n+1])


With the above defines, you can store your stats as numbers, and then show them to the player as grade strings.

Since you are going to be working with the numbers, your game doesn't need to store everything in grades. Just use the numbers, then show the grades to the player based on the number when you build the UI.

In response to Ter13
Ter13 wrote:
Couple tools for you:

Associative lists allow you associate a string, type, to any value. This creates key-value pairs:
> var/list/__grade2num = list("E"=0,"E+"=1,
> "D-"=2,"D"=3,"D+"=4,
> "C-"=5,"C"=6,"C+"=7,
> "B-"=8,"B"=9,"B+"=10,
> "A-"=11,"A"=12,"A+"=13,
> "S-"=14,"S"=15,"S+"=16)
> #define grade2num(g) (__grade2num[g]||0)
>

You can index a list by number, allowing you to convert a number into a string by creating a lookup table:
> var/list/__num2grade = list("E","E+","D-","D","D+","C-","C","C+","B-","B","B+","A-","A","A+","S-","S","S+")
>
> #define num2grade(n) (__num2grade[n+1])
>

With the above defines, you can store your stats as numbers, and then show them to the player as grade strings.

Since you are going to be working with the numbers, your game doesn't need to store everything in grades. Just use the numbers, then show the grades to the player based on the number when you build the UI.


Curious because even though I've managed to make that list of code I really have no idea what I'm doing. I manage to make it work but the "_ _" (underscores) what are they for?
@ter13? Also thank you for your aid.
In response to Ter13
So I manage to get everything to work but now my issue is getting a verb to calculate the total damage "totdmg"

This is how I fixed everything - before I saw the previous posts.

Are you able to put a process in a verb? Is that why my "verb/calculatedamge()" and "verb/calculateevade()" doesn't work?
Final Code




/////////////////////////////////////////////
// THIS IS ONLY FOR THE STAT PANEL VIEWING //
/////////////////////////////////////////////
mob/Stat()
stat("Atk Dmg","[src.atkdmg]/[src.atkdmgnum]")
stat("Atk Spd","[src.atkspd]/[src.atkspdnum]")
stat("Def End","[src.defend]/[src.defendnum]")
stat("Def Spd","[src.defspd]/[src.defspdnum]")
stat("TOTAL DAMAGE", "Result [src.totdmg]")
stat("OVERALL SPEED", "Result [src.overallspd]")
if(src == usr) statpanel("inventory",src.contents)
////////////////////////////////
// THIS IS THE CORE VARIABLES //
////////////////////////////////
mob/var
atkdmg = ""
atkdmgnum = "none"

atkspd = ""
atkspdnum = "none"

defend = ""
defendnum = "none"

defspd = ""
defspdnum = "none"

totdmg = "none"
overallspd = ""
////////////////////////////////////
// VERBS TO TRIGGER THE FUNCTIONS //
////////////////////////////////////
mob/verb/Attack_Damage()
set category = "Attack"
src.atkdmg = input("Set attack damage","",src.atkdmg)
if(atkdmg== "E")
atkdmgnum=0
if(atkdmg== "E+")
atkdmgnum=1
if(atkdmg== "D-")
atkdmgnum=2
if(atkdmg== "D")
atkdmgnum=3
if(atkdmg== "D+")
atkdmgnum=4
if(atkdmg== "C-")
atkdmgnum=5
if(atkdmg== "C")
atkdmgnum=6
if(atkdmg== "C+")
atkdmgnum=7
if(atkdmg== "B-")
atkdmgnum=8
if(atkdmg== "B")
atkdmgnum=9
if(atkdmg== "B+")
atkdmgnum=10
if(atkdmg== "A-")
atkdmgnum=11
if(atkdmg== "A")
atkdmgnum=12
if(atkdmg== "A+")
atkdmgnum=13
if(atkdmg== "S-")
atkdmgnum=14
if(atkdmg== "S")
atkdmgnum=15
if(atkdmg== "S+")
atkdmgnum=16


mob/verb/Attack_Speed()
set category = "Attack"
src.atkspd = input("Set attack speed","",src.atkspd)
if(atkspd== "E")
atkspdnum=0
if(atkspd== "E+")
atkspdnum=1
if(atkspd== "D-")
atkspdnum=2
if(atkspd== "D")
atkspdnum=3
if(atkspd== "D+")
atkspdnum=4
if(atkspd== "C-")
atkspdnum=5
if(atkspd== "C")
atkspdnum=6
if(atkspd== "C+")
atkspdnum=7
if(atkspd== "B-")
atkspdnum=8
if(atkspd== "B")
atkspdnum=9
if(atkspd== "B+")
atkspdnum=10
if(atkspd== "A-")
atkspdnum=11
if(atkspd== "A")
atkspdnum=12
if(atkspd== "A+")
atkspdnum=13
if(atkspd== "S-")
atkspdnum=14
if(atkspd== "S")
atkspdnum=15
if(atkspd== "S+")
atkspdnum=16



mob/verb/Defense_Endurance()
set category = "Attack"
src.defend = input("Set defense endurance","",src.defend)
if(defend== "E")
defendnum=0
if(defend== "E+")
defendnum=1
if(defend== "D-")
defendnum=2
if(defend== "D")
defendnum=3
if(defend== "D+")
defendnum=4
if(defend== "C-")
defendnum=5
if(defend== "C")
defendnum=6
if(defend== "C+")
defendnum=7
if(defend== "B-")
defendnum=8
if(defend== "B")
defendnum=9
if(defend== "B+")
defendnum=10
if(defend== "A-")
defendnum=11
if(defend== "A")
defendnum=12
if(defend== "A+")
defendnum=13
if(defend== "S-")
defendnum=14
if(defend== "S")
defendnum=15
if(defend== "S+")
defendnum=16


mob/verb/Defense_Dodge()
set category = "Attack"
src.defspd = input("Set defense speed","",src.defspd)
if(defspd== "E")
defspdnum=0
if(defspd== "E+")
defspdnum=1
if(defspd== "D-")
defspdnum=2
if(defspd== "D")
defspdnum=3
if(defspd== "D+")
defspdnum=4
if(defspd== "C-")
defspdnum=5
if(defspd== "C")
defspdnum=6
if(defspd== "C+")
defspdnum=7
if(defspd== "B-")
defspdnum=8
if(defspd== "B")
defspdnum=9
if(defspd== "B+")
defspdnum=10
if(defspd== "A-")
defspdnum=11
if(defspd== "A")
defspdnum=12
if(defspd== "A+")
defspdnum=13
if(defspd== "S-")
defspdnum=14
if(defspd== "S")
defspdnum=15
if(defspd== "S+")
defspdnum=16



var/totdmg
var/overallspd // diffence between dodge speed and attack speed

var/atkdmg //attackers damage "grade"
var/atkdmgnum

var/atkspd //attackers speed
var/atkspdnum

var/defend //defenders endurance
var/defendnum

var/defspd //defenders speed
var/defspdnum

var/atkgrade
var/atkgradenum


proc/calculatedamage()
usr.totdmg = usr.defendnum - usr.atkdmgnum
world << "Speed damage was calculated"


proc/calculateevade()
usr.overallspd = usr.defspdnum - usr.atkspdnum
world << "Speed evation was calculated"
return
..()

mob/verb/calculatedamages()
calculatedamage()
grade_check()

mob/verb/calculateevades()
calculateevade()
grade_check()



mob/proc/grade_check()
switch(atkspd)
if("E") return "0"// E-
if("E+") return "1"// E
if("D-") return "2"// E+
if("D") return "3"// D-
if("D+") return "4"// D
if("C-") return "5"//D+
if("C") return "6"// C-
if("C+") return "7"// C
if("B-") return "8"// B-
if("B") return "9"// B
if("B+") return "10"//B+
if("A-") return "11"// A-
if("A") return "12"//A
if("A+") return "13"//A+
if("S-") return "14"//S-
if("S") return "15"//S
if("S+") return "16"//S+
switch(atkdmg)
if("E") return "0"// E-
if("E+") return "1"// E
if("D-") return "2"// E+
if("D") return "3"// D-
if("D+") return "4"// D
if("C-") return "5"//D+
if("C") return "6"// C-
if("C+") return "7"// C
if("B-") return "8"// B-
if("B") return "9"// B
if("B+") return "10"//B+
if("A-") return "11"// A-
if("A") return "12"//A
if("A+") return "13"//A+
if("S-") return "14"//S-
if("S") return "15"//S
if("S+") return "16"//S+
switch(defend)
if("E") return "0"// E-
if("E+") return "1"// E
if("D-") return "2"// E+
if("D") return "3"// D-
if("D+") return "4"// D
if("C-") return "5"//D+
if("C") return "6"// C-
if("C+") return "7"// C
if("B-") return "8"// B-
if("B") return "9"// B
if("B+") return "10"//B+
if("A-") return "11"// A-
if("A") return "12"//A
if("A+") return "13"//A+
if("S-") return "14"//S-
if("S") return "15"//S
if("S+") return "16"//S+
switch(defspd)
if("E") return "0"// E-
if("E+") return "1"// E
if("D-") return "2"// E+
if("D") return "3"// D-
if("D+") return "4"// D
if("C-") return "5"//D+
if("C") return "6"// C-
if("C+") return "7"// C
if("B-") return "8"// B-
if("B") return "9"// B
if("B+") return "10"//B+
if("A-") return "11"// A-
if("A") return "12"//A
if("A+") return "13"//A+
if("S-") return "14"//S-
if("S") return "15"//S
if("S+") return "16"//S+
The double-underscore is part of the name of the global variable. I use single underscores to denote read-only, and double-underscores to denote that you shouldn't ever under any circumstances touch something.

Since I wrote a lookup macro to alias those global lists behind, you shouldn't need to touch the variables. Hence the double-underscore.

As for your code, you are making things way too excessively complicated for yourself. I have no idea what you are even trying to acheive with all of that, and to be honest, I think you need to start reading entry-level programming tutorials, like the guide so you can get a better handle at using logic. Even then, the overall combat system design outlined by your code is deeply flawed and poorly designed even if we ignore the problems with the code itself.

Your code is just long and complicated for zero reason. Let me show you:

var/list/__grade2num = list("E"=0,"E+"=1,
"D-"=2,"D"=3,"D+"=4,
"C-"=5,"C"=6,"C+"=7,
"B-"=8,"B"=9,"B+"=10,
"A-"=11,"A"=12,"A+"=13,
"S-"=14,"S"=15,"S+"=16)
#define grade2num(g) (__grade2num[g]||0)

var/list/__num2grade = list("E","E+","D-","D","D+","C-","C","C+","B-","B","B+","A-","A","A+","S-","S","S+")

#define num2grade(n) (__num2grade[n+1])

/////////////////////////////////////////////
// THIS IS ONLY FOR THE STAT PANEL VIEWING //
/////////////////////////////////////////////
mob/Stat()
stat("Atk Dmg","[num2grade(atkdmg)]/[atkdmg]")
stat("Atk Spd","[num2grade(atkspd)]/[atkspd]")
stat("Def End","[num2grade(defend)]/[defend]")
stat("Def Spd","[num2grade(defspd)]/[defspd]")
if(src == usr) statpanel("inventory",src.contents)

////////////////////////////////
// THIS IS THE CORE VARIABLES //
////////////////////////////////
mob/var
atkdmg = 0
atkspd = 0
defend = 0
defspd = 0

////////////////////////////////////
// VERBS TO TRIGGER THE FUNCTIONS //
////////////////////////////////////
mob/verb/Attack_Damage()
set category = "Attack"
var/value = input("Set attack damage","",src.atkdmg)
atkdmg = grade2num(value)

mob/verb/Attack_Speed()
set category = "Attack"
var/value = input("Set attack speed","",src.atkspd)
atkspd = grade2num(value)

mob/verb/Defense_Endurance()
set category = "Attack"
var/value = input("Set defense endurance","",src.defend)
defend = grade2num(value)

mob/verb/Defense_Dodge()
set category = "Attack"
var/value = input("Set defense speed","",src.defspd)
defspd = grade2num(value)

mob/proc/calculatedamage(mob/defender)
return atkdmg - defender.defend

mob/proc/calculateevade(mob/defender)
return atkspd - defender.defspd


The above is more or less everything you wrote, but cut down to its most basic parts, all the junk that you don't need to be doing thrown away. It's not that I've done something a "different way", and these aren't stylistic choices that I've made. It's not that I have a different way of writing code, or don't get how your system works. It's that your logic is full of redundant actions, redundant values that are a waste of time/resources to work with, and bugged/botched implementations. None of what I'm doing is advanced programming. This is all beginner level stuff that you should know going into things.


Not only that, but there are just numerous bugs:

switch(atkspd)
if("E") return "0"// E-
if("E+") return "1"// E
if("D-") return "2"// E+
if("D") return "3"// D-
if("D+") return "4"// D
if("C-") return "5"//D+
if("C") return "6"// C-
if("C+") return "7"// C
if("B-") return "8"// B-
if("B") return "9"// B
if("B+") return "10"//B+
if("A-") return "11"// A-
if("A") return "12"//A
if("A+") return "13"//A+
if("S-") return "14"//S-
if("S") return "15"//S
if("S+") return "16"//S+


Everything after the first switch is effectively unreachable. If you didn't have the remaining 3 switch statements there, it would make no difference to the outcome of the proc.

proc/calculatedamage()
usr.totdmg = usr.defendnum - usr.atkdmgnum
world << "Speed damage was calculated"


proc/calculateevade()
usr.overallspd = usr.defspdnum - usr.atkspdnum
world << "Speed evation was calculated"
return
..()


These are just wrong. you are calculating attack and defense on the same mob, rather than using the attack/defense of two different mobs. usr is just wrong here, and this has little business being in global scope.

var/totdmg
var/overallspd // diffence between dodge speed and attack speed

var/atkdmg //attackers damage "grade"
var/atkdmgnum

var/atkspd //attackers speed
var/atkspdnum

var/defend //defenders endurance
var/defendnum

var/defspd //defenders speed
var/defspdnum

var/atkgrade
var/atkgradenum


Again, there's no reason for any of this to be in the global scope.

mob/var
atkdmg = ""
atkdmgnum = "none"

atkspd = ""
atkspdnum = "none"

defend = ""
defendnum = "none"

defspd = ""
defspdnum = "none"

totdmg = "none"
overallspd = ""


There's no reason for better than half of these variables to exist. You don't need to store the grade as text, you can use a lookup to convert it on demand. Storing the grade as text on the backend is just a waste of time, because it's meaningless.


You are very seriously missing a huge number of concepts that you need to understand before you start trying to make systems on your own. You need to stop and read through the guide at least one more time. You need to start taking basic programming primers to get started. You don't have a good enough grasp on basic programming logic to be writing your own systems yet.