More questions about scopes of variables and GUI elements

Place to get help with not working mods / modding interface.
RedFury
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Dec 31, 2014 6:24 am
Contact:

More questions about scopes of variables and GUI elements

Post by RedFury »

Yo.

I'm just starting out in the modding community and LUA programming in general, and I have a couple of problems, that make my modding life miserable.
I do have a PHP, C++, JS, HTML background (in diminishing order), so I do understand the basics. I'm just not familiar with Factorios game structure enough yet.

First of is the scope of functions and variables from the mod.
For instance, I use the console to help me out a lot and usually start the game with adding the following line to it:

Code: Select all

pr = function(text) game.player.print(text) -- this is an example, don't go all "it's not MP proof" on me :) 
So where would one put this line of code in the mod, so that pr() would be accessible directly from the start (or you know ... load)? And do I have to prefix it with something like local or global(.)? There's mention of parsing a function, but working examples are either non existent or too hard for me to find.

I'm also having trouble getting some of my variables to initialize (most likely, the same problem as above). In particular I'm trying to get a small notes thing going (don't need to keep data after quit) and would like to store strings and some uints locally. By that I mean, if I ever do make it MP proof, I don't need (want) other player to see this array/table or have access to it. Nor do I want to cause any kind of desyncs or save alterations. I'm aware of the "remote.some.things[player_index]" thing, i would just like to know my limitations, before I go down that route. At this point I can't even get a simple variabale like redfury = "Hello" to get passed to the game so I could print it out... but all I get is NIL! And I tried it in on_init, on_load, outside of functions, control and data lua, local, global.redfury and other more or less useless things. The only thing that comes to mind that i didn't try was squishing it into data.raw . But it didn't seem to be the right way anyways. The only other thing i can think of, is creating a hidden frame or label and dumping the strings there. For some reason GUI elements and things linked with the event handler are the only ones working ... tell me there's something i'm missing, or will i have to link all my functions through the event handler and then raise them this way?

So, how do I scope things in the mod in terms of Game world vs local computer data? And also, saved data vs current session data only (i guess global vs local, but i can't get any of them to work either way)? And how do I access functions from other scopes via the console (ie game.SomeMagicHandler.mymod.DoTheThingThatYouNeedToDo(true) ) ?

And while we're on the subject of scoping, my Gui elements are doing the whole CSS IS AWESOME thing to me.
I don't want elements in a frame to just spill out of it. I mean that's why i "framed" them in the first place :?
"maximal_width" seems to help in the sense that they are put in the next row, but maximal_height seems to be ignored and they just spill over. It might be that my guielement structures are wrong, but to be honest, I haven't found any useful tutorials on that yet. Is there a handler(guielement) for the debug window (F4)? I'd like to see how it's structured, to get its effects (like resizing to fit window, creating a scrollbar ...).

But for now the scoping thing please.
May the source-code be with you
Choumiko
Smart Inserter
Smart Inserter
Posts: 1352
Joined: Fri Mar 21, 2014 10:51 pm
Contact:

Re: More questions about scopes of variables and GUI elements

Post by Choumiko »

RedFury wrote:First of is the scope of functions and variables from the mod.
For instance, I use the console to help me out a lot and usually start the game with adding the following line to it:

Code: Select all

pr = function(text) game.player.print(text) -- this is an example, don't go all "it's not MP proof" on me :) 
So where would one put this line of code in the mod, so that pr() would be accessible directly from the start (or you know ... load)? And do I have to prefix it with something like local or global(.)? There's mention of parsing a function, but working examples are either non existent or too hard for me to find.


So, how do I scope things in the mod in terms of Game world vs local computer data? And also, saved data vs current session data only (i guess global vs local, but i can't get any of them to work either way)? And how do I access functions from other scopes via the console (ie game.SomeMagicHandler.mymod.DoTheThingThatYouNeedToDo(true) ) ?
Via console you can only access functions/variables defined in your mod via remote.call. Every mod has his own local/global functions/namespace. I think the console acts as a kind of mod in that sense.
Have you found this already: https://wiki.factorio.com/index.php?tit ... _Lifecycle ? I think it's mostly up to date and should help to grasp the basics.
RedFury wrote:And while we're on the subject of scoping, my Gui elements are doing the whole CSS IS AWESOME thing to me.
I don't want elements in a frame to just spill out of it. I mean that's why i "framed" them in the first place :?
"maximal_width" seems to help in the sense that they are put in the next row, but maximal_height seems to be ignored and they just spill over. It might be that my guielement structures are wrong, but to be honest, I haven't found any useful tutorials on that yet. Is there a handler(guielement) for the debug window (F4)? I'd like to see how it's structured, to get its effects (like resizing to fit window, creating a scrollbar ...)
No scrollbars (yet?). GUI modding is pure pain :? I mostly use tables with colspan, doesn't look really nice but stays mostly in shape and isn't that horrible :D
RedFury
Burner Inserter
Burner Inserter
Posts: 5
Joined: Wed Dec 31, 2014 6:24 am
Contact:

Re: More questions about scopes of variables and GUI elements

Post by RedFury »

Choumiko wrote:Via console you can only access functions/variables defined in your mod via remote.call. Every mod has his own local/global functions/namespace. I think the console acts as a kind of mod in that sense.
Have you found this already: https://wiki.factorio.com/index.php?tit ... _Lifecycle ? I think it's mostly up to date and should help to grasp the basics.
Yeah, I did read it. But since all my variables were supposed to be global, I got confused by the console telling me there were no variables like that in global.
The console as a mod analogy actually clears up most of it. Thanks.
Choumiko wrote:No scrollbars (yet?). GUI modding is pure pain :? I mostly use tables with colspan, doesn't look really nice but stays mostly in shape and isn't that horrible :D
I'm a php programmer, so i know (and hate) the horror of html+css ... i guess this is gonna be similar, just with a hand tied behind my back ... and a leg behind my neck :D
May the source-code be with you
Post Reply

Return to “Modding help”