[SCRIPT] LUA os.clock() and os.time()

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
YpsiNine
Posts: 36
Joined: Wed Oct 10, 2012 1:08 am

[SCRIPT] LUA os.clock() and os.time()

Post by YpsiNine »

Hi,

I guess these won't work within Grimrock as they don't belong to the math category?
I am doing some random number generation and would need a good seed for the math.randomseed function but the above doesn't seem to be implemented.

Thanks.
Last edited by YpsiNine on Fri Oct 12, 2012 7:24 pm, edited 1 time in total.
YpsiNine
Posts: 36
Joined: Wed Oct 10, 2012 1:08 am

Re: LUA os.clock() and os.time()

Post by YpsiNine »

.
I came up with a solution of my own, yay!
The result is a random seed, but not as huge as os.clock() or os.time() would give I suppose, but it's better than nothing.

If you simply do random without a seed there will be very bad variation, like this:

Code: Select all

math.random(1,3)
Will typically produce:
3
3
3
1
3
3
3
3

Now with my code however, more randomness is to be expected. :)

Code: Select all

function randomize(x, y)
  seed = spawn("dagger", 1, 1, 1, 1)  --spawns a dagger at a place where the party can't interfere
  math.randomseed(seed.id)            --uses the ID of the spawned dagger as a random seed
  result = math.random(x, y)          --here we do the random number generation
  print(result)                       --to see the number it picked while debugging, else just remove this line
  print(seed.id)                      --to see the seed it got while debugging, else just remove this line
  seed:destroy()                      --then we destroy the dagger
  return result                       --we need to return the result to whatever asks for it
end
Will typically produce:
2
3
1
1
2
3
1
2

Use the function for example like this:

Code: Select all

variable = randomize(1,3)
if variable == 1 then
  hudPrint("I got a 1")
elseif variable == 2 then
  hudPrint("I got a 2")
elseif variable == 3 then)
  hudPrint("I got a 3")
end
YpsiNine
Posts: 36
Joined: Wed Oct 10, 2012 1:08 am

Re: [SCRIPT] LUA os.clock() and os.time()

Post by YpsiNine »

Bump if someone wants to include this script in the Useful scripts repository thread.
User avatar
Komag
Posts: 3659
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: [SCRIPT] LUA os.clock() and os.time()

Post by Komag »

I think it's good to add it there because it's quite unique and will surely be useful in the future - you can just add a post to that thread yourself and put it in
Finished Dungeons - complete mods to play
YpsiNine
Posts: 36
Joined: Wed Oct 10, 2012 1:08 am

Re: [SCRIPT] LUA os.clock() and os.time()

Post by YpsiNine »

Komag wrote:I think it's good to add it there because it's quite unique and will surely be useful in the future - you can just add a post to that thread yourself and put it in
Ok, done. :)
Post Reply