No way to change data prior to Main?

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.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #12417

    Abe _King_
    Participant

    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?

    I’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 sure

    This would be a nice addition to the language if possible…

    #12418

    Jesse
    Participant

    you want it to be an array or a Map?

    [/crayon]

    if 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.

    #12419

    Abe _King_
    Participant

    Thanks 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:

    Thanks for your time!

    #12420

    AdamStrange
    Participant

    Couldn’t you just do something like this:

    [/crayon]

    That way you can change the controls to any key at any time. none of the ‘map’ stuff getting in the way?

    #12421

    abakobo
    Participant

    can 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..

    #12422

    Abe _King_
    Participant

    I 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!

    #12423

    Abe _King_
    Participant

    Wow! 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

    #12424

    abakobo
    Participant

    Mmm 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!

    #12425

    AdamStrange
    Participant

    just remember that by using
    Controls[KEY_LEFT] = Key.Left

    you 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, etc

    You 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

    #12428

    Abe _King_
    Participant

    Hey 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…

    #12429

    Mark Sibly
    Keymaster

    ‘Lazy initialization’ can help here:

    #12431

    Abe _King_
    Participant

    That’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!

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.