What's the simplest way to declare a int variable that can't be below zero? In python you would just type x >= -1

But in gdscript it doesn't work. What's the simplest alternative?

I haven't seen that feature in GDScript.

The simplest alternative is to use an int, and avoid assigning negative values to it.

You could define a class that has an int property, and define the constructor and setter so that negative values are forced to be nonnegative, or trigger an error condition.

You can also define a setter for an ordinary variable using the setget keyword, but the setter can be overridden within the file.

I guess I'll have to define a class as non negative int. Was hoping there was something simpler in gdscript.

What's the simplest way to declare a int variable that can't be below zero? In python you would just type x >= -1

This does not declare a positive variable in both Python 2.7 and 3.9:

❯ python
Python 3.9.0 (default, Oct 28 2020, 15:15:16) 
[GCC 10.2.1 20201005 (Red Hat 10.2.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x >= -1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

❯ python2   
Python 2.7.18 (default, Feb  1 2021, 00:00:00) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x >= -1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

@Calinou could he not declare an exported variable and specify the range there?

@dotted said: @Calinou could he not declare an exported variable and specify the range there?

You could do it this way, but it would just prevent it from being set to negative in the Godot editor. Through code it would still be possible to set it to a negative value.

Probably the easiest way, though not most efficient, way to ensure an integer stays positive is to either define a function to set the value and always the function to set the variable or to use the abs function:

# for the editor:
export (int, 0, 100) var positive_int = 0

func _ready():
	positive_int = abs(-10)
	print (positive_int) # should print '10'
	positive_int = set_positive(-10)
	print (positive_int) # should print 0

func set_positive(value):
	if (value < 0):
		return 0
	return value

@Calinou said: This does not declare a positive variable in both Python 2.7 and 3.9:

Yeah I tried playing around with it, seems not to produce the result I read in some threads it would.

@TwistedTwigleg The absolute values are an insightful thing. But I really need the actual values to not go below zero. Not that -10 becomes 10.

I'll probably just go with running a function every few seconds like.


if variable < 0
	variable = 0

I hope it won't cause issues.

Actually my solution is still a poor one. Cause I need multiple variables to not go below zero. I'll have to create a class to not go below zero to be frank, then assign it to all the variables. Oh well.

In @TwistedTwigleg's example see the function starting at line 10. If the value isn't greater than 10 0 then it returns 0 effectively clamping it to positive numbers only.

You mean this?

func set_positive(value):
              if (value < 0):
              return 0
              return value

Actually I figured out, that since I have only a few things that subtract from the variables. If I only run this code (or similar to it) on the things that subtract from the variable, I can save myself a lot of trouble.

Thanks for the help guys. Appreciate it.

Generally though the generic way to ensure a property is valid is to use a setter (setget) functions. At least this works when changing the property in external contexts (scripts). It's the principal of abstraction.

Internal to the script you are working with the property directly, so here you must use the function directly.

2 years later