How to add to and call value of an integer?

Rankin87
19 Nov 2023, 03:54

It's pretty straightforward this time. I made an integer attribute in an object. The variable is supposed to increase each turn you use one of the verbs of the object. After ten turn counts, the player is meant to move to a different room.
So how would I do a code that would add 1 to the integer when run, and how do I do an if statement on how current value of the integer to know when it has turned ten to move the player object?


DavyB
20 Nov 2023, 11:43

Just create a function, say INCREMENT and call it from within the script of each verb. Inside INCREMENT, add 1 to your integer attribute and when = 10 move the player to the required room.


Rankin87
20 Nov 2023, 19:54

Thanks, that is a big help, but I still need to know how to call the value of a variable like an integer and add to it in code. I figure it's very simple but I could not find the answer on google.


DavyB
21 Nov 2023, 07:34

object.attribute = object.attribute + 1
If object.attribute = 10 move player


Rankin87
21 Nov 2023, 20:05

That's exactly what I am looking for. Thank you, so much.


Rankin87
22 Nov 2023, 18:48

Just one last question. Is there a way in code to have the if statement asking if the integer is equal to ten, instead be greater than nine?


mrangel
23 Nov 2023, 16:00

Just one last question. Is there a way in code to have the if statement asking if the integer is equal to ten, instead be greater than nine?

You would probably learn a lot of these answers if you did the tutorials.
But, in this case, there are a 6 operators which compare two numeric values:

  • = - equal to
  • > - greater than
  • >= - greater than or equal to
  • < - less than
  • <= - less than or equal to
  • <> - different to

For the case where you want an attribute to be greater than nine, you could use any of these:

if (objectname.attributename > 9) {
if (objectname.attributename >= 10) {
if (not objectname.attributename <= 9) {
if (not objectname.attributename < 10) {

or even …

if (9 < objectname.attributename) {
if (10 <= objectname.attributename) {
if (not 9 >= objectname.attributename) {
if (not 10 > objectname.attributename) {

Note that = and <> will work on most types of attribute, as long as they are the same type. So you can use them to compare two numeric types, two strings, or two objects.

The other four only work on numbers, and will cause an error if the attribute isn't numeric.