Enemy Health

tfamaze
This might seem simple to most of you, but for the life of me, I cannot figure out how to deal damage to enemies by simply subtracting from a variable. I can generate the random integer, but I cannot figure out how to set the variable (dragonHealth in this case) to dragonHealth - damage. I would be extremely grateful if someone could help me with this as I am doing it as a project for a class.

HegemonKhan
here's an example:

orc.life = orc.life - player.damage

conceptually of how it works:

initial values (for this example):

orc.life = 100
player.damage = 20

the expression (formula~equation):

orc.life - player.damage

--

old value: orc.life = 100

orc.life (new) = orc.life (old: 100) - player.damage (20)
orc.life (new) = (100) - (20)
orc.life (new) = 80

new value: orc.life = 80

---

old value: orc.life = 80

orc.life (new) = orc.life (old:80) - player.damage (20)
orc.life (new ) = (80) - (20)
orc.life (new) = 60

new value: orc.life = 60

--

etc etc etc

---------------------------

the confusion, is that programming is doing ASSIGNMENT, not math's equals.

you're literally plugging~assigning~setting the result of the expression ( on the right side of the ASSIGNMENT operator: = ) to the VARIABLE (on the left side of the ASSIGNMENT operator: = )

you know algebra, right? algebraic substitution:

x = 10
y = x + 5

y = (x:10) + (5)
y = 15

that's the same as programming's VARIABLES:

x = 10

msg (x)
// outputs: 10

msg("The number is " + x + ".")
// outputs: The number is 10.

except, instead of using 'x' (though you can), we are more descriptive:

orc.life = 10

instead of 'x' we're using 'orc.life'

msg("The number is " + orc.life + ".")
outputs: The number is 10.

-------

the other difference is that we're doing ASSIGNMENT:

literally setting the result of the expression on the right side of the ASSIGNMENT Operator (=) to the VARIABLE on the left side of the ASSIGNMENT Operator (=):

x = 10

'10' is being plugged into (set~assigned~etc) 'x'

x = "Hi, my name is HK."

the 'Hi, my name is HK.' string is assigned into~to the variable 'x'

so, now do you understand the:

orc.life = orc.life - player.damage

the result of the expression 'orc.life - player.damage' is ASSIGNED into~to the VARIABLE 'orc.life'

------------

see this link for more info:

viewtopic.php?f=18&t=5559

ask if confused by anything or if need help with anything

-------

oh, in the GUI~Editor, you use the:

run as script -> add new script -> variables -> 'set a variable or attribute' Script

to do what I shown in code above.

-----------------

my own template of general syntax:

Object_name.Attribute_name = Value_or_Expression

examples:

1) player.strength = player.strength + 5
2) player.strength = player.endurance + player.endurance
3) player.damage = (player.weapon.damage + player.weapon.damage * player.strength / 100) - (orc.armor.resistance + orc.armor.resistance * orc.endurance / 100)

Addition (simple):

Object_name.Attribute_name = Object_name.Attribute_name + Value
player.strength = player.strength + 5

Subtraction (simple):

Object_name.Attribute_name = Object_name.Attribute_name - Value
player.strength = player.strength - 5

Multiplication (simple):

Object_name.Attribute_name = Object_name.Attribute_name * Value
player.strength = player.strength * 5

Division (getting the quotient) (simple):

Object_name.Attribute_name = Object_name.Attribute_name / Value
player.strength = player.strength / 5

Modulus (Division, but getting the remainder) (simple):

Object_name.Attribute_name = Object_name.Attribute_name % Value
player.strength = player.strength % 5

---------

for a complex expression, see above my example number: '3)' --- it's above the 'Addition (simple)' title

--------

see my link, to see how to do transactions ("buying" and "selling")

tfamaze
The issue I have ran into is the game saying Error running script: Error compiling expression 'dragonHealth + damage': ArithmeticElement: Operation 'Add' is not defined for types 'Element' and 'Int32'. The line of code this references is dragonHealth = dragonHealth + damage

HegemonKhan
in quest, there's 3 types of VARIABLES:

VARIABLES:
-> Variables
-> Attributes
-> Parameters

people new to quest and~or especially to programming, should just work with Attributes (as otherwise, you'd need to learn about scope: 'global' vs 'local' and etc programming stuff).

in the GUI~Editor, to create~add attributes:

'whatever' Object -> 'Attributes' Tab -> Attributes -> Add -> (set it up, see below for example)

(Object Name: player)
Attribute Name: strength
Attribute Type: int (integer)
Attribute Value: 0

and to adjust~manipulate~change your Attributes, in the GUI~EDitor's scripting (add new script), an example:

'strength_elixir' Object -> 'Verbs' Tab -> Add -> (let's call it): drink -> (set it up, see below for example)

run as script -> add new script -> variables -> 'set a variable or attribute' Script -> (see below, for example)

set variable player.strength = [EXPRESSION] player.strength + 5

so, every time you click on the 'drink' Verb button or hyperlink for the 'strength_elixir' Object during game play, your strength increases by 5.

---------

in code, it would look like this (quickly, not full game code):

Attributes are VARIABLES "ATTACHED" (in the GUI~Editor, you "add" the Attributes to the Objects) to Objects:

the "attachment" is done via the dot-period in code:

Object_name.Attribute_name

for example:

player.alias
orc.alias
dragon.alias
dragon.strength
player.strength
orc.dead
orc.strength
dragon.dead
player.current_life
player.maximum_life
dragon.current_life
dragon.maximum_life
player.left_hand
player.right_hand

player.right_hand = sword
player.strength = 30
dragon.strength = 80
orc.dead = false
orc.dead = true
HK.strength = 100
HK.maximum_life = 9999
HK.current_life = 9999
player.alias = "HK"
HK.favorite_color = "black"
etc etc etc

-----------------------------------
whereas, if I just did:

strength = 100

you have a LOCAL VARIABLE

notice that there's no "attachment" to an Object

there's no dot-period
---------------------------------

as long as your Objects ('player' and 'dragon') exist (not removed nor destroyed), you can use these Attributes anywhere (aka, they're: global VARIABLES).

<object name="strength_elixir_1">
<attr name="alias" type="string">strength elixir</attr>
<attr name="drink" type="script">
player.strength = player.strength + 5
</attr>
<attr name="displayverbs" type="simplestringlist">look;take;drink</attr>
<attr name="inventoryverbs" type="simplestringlist">look;drop;drink</attr>
</object>

<object name="player">
<attr name="strength" type="int">0</attr>
</object>

<verb>
<property>drink</property>
<pattern>drink</pattern>
<defaultexpression>You can't drink that.</defaultexpression>
</verb>

The Pixie
tfamaze wrote:The issue I have ran into is the game saying Error running script: Error compiling expression 'dragonHealth + damage': ArithmeticElement: Operation 'Add' is not defined for types 'Element' and 'Int32'. The line of code this references is dragonHealth = dragonHealth + damage

It sounds as though dragonHealth is an object. You need it to be an integer attribute of an object, presumably called "dragon". Attributes are variables that are connected to an object. the dragon object has a name attribute, for example, with the value "dragon". You might have set an alias or a description for it too; these are attributes too. As health belongs to the dragon, you want to set it up as another attribute.

If you are on-line, set it up in your start script. Go to the Scripts tab of the game object, click the seventh icon, Code view, and paste this in:
dragon.health = 40


If you are off-line you can do that, but a better way is to go to the Attributes tab of the dragon object, and create an attribute called "health". Set it to be an integer, and give it a value.

Either way you end up with a health attribute, and your code for the dragon taking damage will now be:
dragon.health = dragon.health + damage

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums