Closed KiloOscarSix closed 2 years ago
Need to use try
/except ValueError:
instead of isdecimal()
Will look into this. I note that this doesn't happen if I drop it into a 7.4.x build, though this doesn't help much as most novels either were or are still being built with 7.3.5 or below. That .isdecimal() check is supposed to prevent int() throwing an exception if the string is NaN (including an empty string) - is there a better/standard way of dealing with this?
The best way with dealing with possible exceptions is using exception handling.
https://www.w3schools.com/python/python_try_except.asp
Looking at your current code I've made some edits but a couple places it might be a bit more fiddling to fix.
Okay, so that's useful for testing code, in case something goes wrong; it can gracefully tell you about it and, if handled correctly, potentially recover without crashing.
In this case, though, we know something's going wrong: a python method that should be available on the variable (because it's a string) is not available. We know it's a string, because that is what is returned: https://www.renpy.org/doc/html/screen_actions.html#FileCurrentPage
I even tested it by explicitly casting the returned value to a string: currentpage = str(FileCurrentPage())
...so why is the method not available?
I am not arguing, btw. In any case, I could write a SafeInt() function that does the checking for me (and might do just that). I do want to know why what is there is not working when dropped into a 7.3.5 build, but works just fine in a 7.4.x build, and whether I can do something more elegant than shrug and develop a workaround (which is what I've done so far lol)
I've read that it could work if we cast the string to a Unicode object. I'm back home tonight so can do some testing.
On another note, it's better practise to use error handling than if statements. The saying is "if it quacks like a duck, looks like a duck, it is a duck"
This can be better explained when I have a couple examples tonight.
Also don't worry about questioning/arguing. The more we think and discuss problems the better the solution we'll come up with.
Good to know :)
While I am here - I have to take a child to an appointment shortly - I should let you know that I am typically available between 15:00-21:00 GMT. I work early in the morning, which is unfortunate because you guys seem to be geared for evening/night hours (actually, I am, too - my work hours suck balls)
Example Before:
if lastpage == None: lastpage = int(currentpage) if currentpage.isdecimal() else 1
# Preserve the last numeric page viewed
currentpage = int(currentpage) if currentpage.isdecimal() else lastpage
After:
if lastpage is None:
try: lastpage = int(currentpage)
except ValueError: lastpage = 1
# Preserve the last numeric page viewed
try: currentpage = int(currentpage)
except ValueError: currentpage = lastpage
Been fixed with #4
Yeah, dropping it into 7.3.5 presents this crash. Will have to fix all that for compatibility, later
Fixed it.
# Conditional invalidation based on purpose of input can go here:
if u"{}".format(currentstring).isdecimal():
# If 'targetaction' is "newplaythroughname" or "changeplaythroughname", we need to prevent it from being an integer (because these are Ren'Py save system Page names)
# The `.isdecimal()` method is only available on unicode objects. `.isdigit()` is a problem because e.g. exponents, like ², are also considered to be a digit.
# Explicitly converting to unicode resolves compat with 7.3.5. However, Py3 uses unicode natively for strings and might get upset about converting to itself
# TODO: Test on 7.5
# TODO: Test on 8
if targetaction in ("newplaythroughname", "changeplaythroughname"):
isvalid = False
feedback = _("{b}The Playthrough name may not be a whole number{/b}")
elif invalid:
# Since Playthrough names must be unique, it is invalid if it already exists in the list
if currentstring in invalid:
isvalid = False
feedback = _("{b}This Playthrough name already exists{/b}")