Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Can I have score not be below 0?
Started by david.hutyra.stp Feb 11 2020 01:58 PM

3 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

david.hutyra.stp

[GLOBAL: userInfoPane.html]
david.hutyra.stp
  • Observer

  • 12 posts
  • Corona SDK

I want to have a money system and I set everything else up to remove or add money but I don't know how to lock it so that it doesn't go below 0 can someone help me?



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,697 posts
  • Enterprise

Where ever you deduct value from your score variable (whatever you call it, put this if statement after it, changing the variables of course.

if score < 0 then
    score = 0
end


[TOPIC: post.html]
#3

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,271 posts
  • Corona SDK

I'd like to refer you to my other answer to another post of yours: https://forums.coronalabs.com/topic/77147-can-i-have-a-collision-answer/#entry407089

 

That said, in this particular issue all you are looking for is a conditional check to see if the player has enough money to do whatever it is that they want to do.

local money = 50
local cost = 100

if money >= cost then
  print("you've got enough money!")
  -- then you could deduct whatever the cost was from the money, i.e.
  money = money - cost
else
  print("you don't have enough money!")
end

It really seems like you just need to start learning the basics. Happy coding!



[TOPIC: post.html]
#4

Develephant

[GLOBAL: userInfoPane.html]
Develephant
  • Corona Geek

  • 1,475 posts
  • Corona SDK

Hi,

 

You could also clamp it, just as another option.

score = math.max(score, 0)

-dev




[topic_controls]
[/topic_controls]