About Monkey 2 › Forums › Monkey 2 Programming Help › No way to change data prior to Main?
This topic contains 11 replies, has 5 voices, and was last updated by
Abe _King_
1 year, 3 months ago.
-
AuthorPosts
-
December 22, 2017 at 11:23 pm #12417
I’m trying to setup some data in the global scope but it seems that may be an unsightly limitation of Monkey2 … Is it possible to do this?
Monkey123456Enum DirectionU, D, L, REndGlobal Controls := New Map<Direction, Key>Controls[Direction.U] = Key.Up 'unexpected token ControlsI’ve done similar and smaller examples but this pretty much gets the point across.
If this isn’t possible, I’ve got 2 different solutions.
1. Create an initialization method called at the beginning of main – doable
2. Create an extension for map which can take an array of keys and values. – not sureThis would be a nice addition to the language if possible…
December 23, 2017 at 3:37 am #12418you want it to be an array or a Map?
[/crayon]Monkey123456789101112131415[crayon-5cb9b8c924276101250563 inline="true" ]Enum DirectionU, D, L, REndGlobal Controls := New Map<Direction, Key>Function Main()Controls.Add(Direction.U,Key.Up)If Keyboard.KeyDown(Controls.Get(Direction.U))Controls.Update(Direction.U,Key.W)endifEnd Functionif You understand that a Map is a class you will also understand that a map can not be indexed as you did in line 6 of your example which is reserved for arrays.
December 23, 2017 at 7:52 am #12419Thanks for the reply! That isn’t the problem, I’m almost certain we can’t change certain data outside of the main call. It’s valid code, the map class is operator overloaded so that the key inside of the brackets and the read value on the right of the equal sign will be done as you’ve written Controls.Add(...).
Currently I have this and it’s working fine:
Monkey12345678Global Controls := New Map<Direction, Key>Function InitControls()Controls[Direction.U] = Key.UpControls[Direction.D] = Key.DownControls[Direction.R] = Key.RightControls[Direction.L] = Key.LeftEndThanks for your time!
December 23, 2017 at 8:25 am #12420Couldn’t you just do something like this:
[/crayon]Monkey12345678[crayon-5cb9b8c92b5e6131003117 inline="true" ]const KEY_LEFT:int = 0field Controls:Key[] = New Key[6]method New()Controls[KEY_LEFT] = Key.Leftetc...That way you can change the controls to any key at any time. none of the ‘map’ stuff getting in the way?
December 23, 2017 at 9:40 am #12421can not be indexed
the []= operator can be user defined (it’s used in json, map,…)
To initialize your classes you’ll have to do some of the work in a Function. But note that monkey can have several mains! You can import a .monkey2 file like init.monkey2 and in that file make all you initializations in “function main”. The main of init.monkey2 will run before the main.monkey2’s main. I think it’s the cleanest way to init you program. Or you could just have a InitGlobals() function called in main so you main remain clean..
December 23, 2017 at 9:50 am #12422I forgot exactly why I decided to use a map in the first place. . . For some reason, I believe had this impression that I was going to be using discontinuous keys for the input so that’s why I went with a map. Being that I’m using consecutive values starting from 0 it definitely makes more sense to use an array! Although I guess its time to realize that defining some data in the global scope simply isn’t a feature of the language. Not a problem of course. Thanks for the insight, Adam!
December 23, 2017 at 9:55 am #12423Wow! I’m not sure how I missed your post, must have been writing mine while you did yours. Thanks Abakobo, that helps a ton. I would have never thought that would be possible . . . It would be a nice abstraction in my program to make use of that to hide away some of the setup stuff happening in the background. That covers just about everything then
Much thanks, everyone
December 23, 2017 at 11:08 am #12424Mmm testing it gives duplicate Main()… could be reserved to modules! will check and try to find the docs where a read this..
EDIT: yep it’s for modules only so it can be a bit of work just for a simple init…
But it’s great design!
December 23, 2017 at 11:34 am #12425just remember that by using
Controls[KEY_LEFT] = Key.Leftyou can redefine a key at any time with:
Controls[KEY_LEFT] = Key.Right
Controls[KEY_LEFT] = Key.A
Controls[KEY_LEFT] = a key picked by the user, etcYou always reference Controls[KEY_LEFT], so what it is mapped to becomes irrelevant….
and you can also use:
If Keyboard.KeyDown( Controls[KEY_LEFT] ) Then do something
to trap and minitor if the key is being pressed
December 23, 2017 at 7:34 pm #12428Hey Adam and Abakobo, thanks for the updates. Still very helpful of both of you! Would be nice if we could have a few mains in our own project to practice a few of our own abstractions. And huh, that’s quite unfortunate about the limitation of the map. Those were my intentions to begin with! I would have never assumed that you couldn’t “reassign” using the index operator of the map. That wouldn’t be the case in other languages I believe…
December 23, 2017 at 8:50 pm #12429‘Lazy initialization’ can help here:
Monkey123456789101112131415161718Function GetControls:IntMap<Key>()Global controls:IntMap<Key>If Not controlscontrols=New IntMap<Key>controls[Direction.U] = Key.Upcontrols[Direction.D] = Key.Downcontrols[Direction.R] = Key.Rightcontrols[Direction.L] = Key.LeftEndifReturn controlsEndDecember 23, 2017 at 9:06 pm #12431That’s a very useful approach, I like that a lot!! Thanks, Mark!
— edit:
I can’t believe how much of a mess up I’ve been on this post haha. Apparently Map’s don’t have a limitation with being able to reset change values at all. Just another glitch I found in my own program. Well, at least now I know maps are completely fine! -
AuthorPosts
You must be logged in to reply to this topic.