Extending GameMode-object + getHours() and getMinutes()

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Extending GameMode-object + getHours() and getMinutes()

Post by JKos »

I noticed something pretty cool, and I hope AH will not "fix" this, but it is possible to extend the GameMode-object with properties and methods AND IT IS SAVE GAME COMPATIBLE. Tested it a couple of times because I didn't believe it at first :). So I added a couple of useful methods to my GameMode-object which probably are useful for many of you.

Just copy paste this to any script entity

Code: Select all

GameMode.getHours = function()
	local hour = math.floor(GameMode.getTimeOfDay()/(2/24))+6 -- add 6 hours because 0.0 = 6:00
	return hour%24
end
GameMode.getMinutes = function()
	return math.floor(GameMode.getTimeOfDay()/(2/24/60)%60)
end
Now you can get Grimrock-time converted to real hours and minutes by calling those methods.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Extending GameMode-object + getHours() and getMinutes()

Post by cromcrom »

Niiiiice, thanks a lot. Introducing Scroll Watch ?
A trip of a thousand leagues starts with a step.
Batty
Posts: 509
Joined: Sun Apr 15, 2012 7:04 pm

Re: Extending GameMode-object + getHours() and getMinutes()

Post by Batty »

Yes, that's a good one, thanks JKos. :)

Can we create methods with all the objects or just GameMode?
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Extending GameMode-object + getHours() and getMinutes()

Post by cromcrom »

Thanks a lot again JKos, using this baby for my sleepiness system, and crediting you oc.
A trip of a thousand leagues starts with a step.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Extending GameMode-object + getHours() and getMinutes()

Post by minmay »

Using this as part of a hack, I discovered there is a nasty caveat: when you add a field to GameMode (and probably many other global objects), it stays there even after restarting dungeon preview or loading a different mod. To reset GameMode, you have to go back to the main menu. Which makes sense, but is potentially inconvenient from a scripting perspective.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Extending GameMode-object + getHours() and getMinutes()

Post by minmay »

I'm a year late here, but what do you mean by "IT IS SAVE GAME COMPATIBLE"? If you save the game, GameMode's fields don't get saved. If you close Grimrock 2 and load your saved game, any changes you made to GameMode or other global tables will be gone. The objects persist across game sessions but this doesn't mean they are "SAVE GAME COMPATIBLE": those objects simply aren't touched by saving/loading in the first place. Try it yourself, go into some dungeon and set GameMode.asdf = 1, then load some other save game and print GameMode.asdf - you'll get 1. This is the opposite of save game compatible as far as I'm concerned.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
JKos
Posts: 464
Joined: Wed Sep 12, 2012 10:03 pm
Location: Finland
Contact:

Re: Extending GameMode-object + getHours() and getMinutes()

Post by JKos »

Obviously I was mistaken. I didn't know at that time that the game objects do persists inside of a game session, if you don't close the game entirely. So It's NOT SAVE GAME COMPATIBLE, don't use it, put those methods to a some other object instead.
- LoG Framework 2http://sites.google.com/site/jkoslog2 Define hooks in runtime by entity.name or entity.id + multiple hooks support.
- cloneObject viewtopic.php?f=22&t=8450
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Extending GameMode-object + getHours() and getMinutes()

Post by minmay »

Well, it depends on what you're doing. If you wanted to add getHours() and getMinutes() functions for example, you could just do it in your mod's init.lua. That way they'll be added every time the mod is loaded.
I can even think of places where this specific behaviour is useful. For example, maybe you have a huge lookup table. You could make your init.lua store it in one of the global tables if the mod is loaded and it doesn't already exist. Then the game wouldn't have to serialize the huge lookup table, whereas it would have to serialize it if you stored it in a script entity.
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Post Reply