View previous topic :: View next topic |
Author |
Message |
Filipe_Br Master Cheater Reputation: 3
Joined: 07 Jan 2016 Posts: 272 Location: My house
|
Posted: Sat Jul 01, 2017 9:09 pm Post subject: How do I know if an address is allocated? |
|
|
In Lua I have a list with addresses, what interests me in them is to read its value with 'readInteger ()', however it has a problem, some of these addresses are sometimes not allocated, that is, they get the value = ??.
When I make a comparison to the value of these invalid addresses I get an error.
How can I check if this address is allocated and stop receiving errors?
_________________
... |
|
Back to top |
|
|
Zanzer I post too much Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Sun Jul 02, 2017 12:43 pm Post subject: |
|
|
Code: | local value = readInteger(address)
if value and value > 0 then
-- yay
end |
|
|
Back to top |
|
|
FreeER Grandmaster Cheater Supreme Reputation: 53
Joined: 09 Aug 2013 Posts: 1091
|
Posted: Sun Jul 02, 2017 5:22 pm Post subject: |
|
|
@Zanzer, why value > 0? After all normal values could legitimately be negative...
example explanation stuff:
Code: | address = "5"
local value = readInteger(address)
print(tostring(value))
|
where an address of 5 is obviously bad shows that the return value is nil (and doesn't create an error that stops the script unlike, say, getAddress("[5]")) which would be caught by the if value check since nil is treated as a false value.
Alternatively if you have a memory record and access it's value then it will be "??"
Code: | al = getAddressList()
mr = al.getMemoryRecord(0) -- get first record in list for testing, again address of 5
print(tostring(mr.value)) -- ??
print(tostring(mr.value == "??")) -- true
|
|
|
Back to top |
|
|
Zanzer I post too much Reputation: 126
Joined: 09 Jun 2013 Posts: 3278
|
Posted: Mon Jul 03, 2017 2:23 pm Post subject: |
|
|
You said you wanted to "make a comparison" so value > 0 was an example of a comparison.
The important part was before it. It checks to make sure it's not nil. Code: | if value then
-- do stuff
end |
|
|
Back to top |
|
|
|