Scripts / Global variables and multilevel

Author
Message
Angiel
9
Years of Service
User Offline
Joined: 21st Dec 2014
Location: Italy
Posted: 9th Apr 2017 07:05 Edited at: 9th Apr 2017 07:06
In a game I created two levels.
In the first level I put this script:



.... in the second level this other script:



When I switch from the first to the second level I have this error.

"scriptbank\miotutorial1.lua6: attempt to concatenate.global 'g_temperatura' (a nil value)


There is some solution?
sorry for my bad English, I write from Italy.
lotgd
3D Media Maker
14
Years of Service
User Offline
Joined: 2nd Apr 2010
Location: italy
Posted: 9th Apr 2017 07:30
try like this


My Pc Specs : Win10pro64bit /Msi z97 / i74770k / gtx1070 / 16gb ram / SSD 850Evo
Angiel
9
Years of Service
User Offline
Joined: 21st Dec 2014
Location: Italy
Posted: 9th Apr 2017 08:14 Edited at: 9th Apr 2017 08:16
Thank you for the reply. I had already tried to put:

if g_temperatura == nil then
g_temperatura = 100
end

but the value of "g_temperatura", in the second level, it should be 55, if the player is less than 100 Entity (chest), which is in the first level.
sorry for my bad English, I write from Italy.
Ratall
15
Years of Service
User Offline
Joined: 29th Jun 2008
Location: Not Here
Posted: 9th Apr 2017 10:16
Hi

Currently g_ vars are not transferred between levels.

The standard save and restore routines only work with in a level. I know this is a pain but each level is in effect a different game.
Lee appears to have it setup in the engine to clear everything out before starting a new level.

For now you can get around this by writing your own save and restore routines. So you can save the data you need before you leave a level and restore them once you start a new level.

Quod cito fit, cito perit.
PM
Corno_1
GameGuru Tool Maker
13
Years of Service
User Offline
Joined: 3rd Nov 2010
Location:
Posted: 9th Apr 2017 12:02
Maybe this helps you:
https://forum.game-guru.com/thread/217830#msg2576684
There are better versions in the shop maybe, but it is a good template.
My dream is to develope games, which makes fun when I create it and fun when other people play it.
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 9th Apr 2017 15:37 Edited at: 9th Apr 2017 16:54
Ratail the last I heard g_ variables do transfer over, Lee set them to do that quite a while ago as far as I know.

Edit: Hmn, I could have sworn that was supposed to be the case now, but I've just trued and it's not carrying over for me either

These are my scripts:

testing.lua



collect.lua



Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 9th Apr 2017 20:31
OK, done some checking, I was wrong, my bad, I could have sworn Lee had made them carry over between levels, but I misread something else. There is a way though, apparently you can use smallg's auto-save script, this auto saves your progression into the last save slot, and when you load the next level load the values.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Ratall
15
Years of Service
User Offline
Joined: 29th Jun 2008
Location: Not Here
Posted: 9th Apr 2017 23:32 Edited at: 10th Apr 2017 00:41
@belidos No worries you probably got the wrong idea when Lee released my mod to allowed tables to be saved/restored.

I did consider the problem of passing g_ stuff between levels but it would have require Lee to change quite a bit and it occurred to me that while sticking a g_ on the front in order to ensure the values get saved automating in a level does not mean you would always want that stuff transferred between levels.

Probably best left to individual scriptwriters rather than adding it to the standard code.


Quod cito fit, cito perit.
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 10th Apr 2017 08:06 Edited at: 10th Apr 2017 08:18
This is something i'm going to have to find out about soon, my project (The Island) is survival based, i'm using variables to store items collected like food, water, first aid kits, quest items, etc. and i'm going to need them to transfer to the next level.

Now have an idea on a way to do this but i have no idea how to actually do it, you sound like you know what you're doing ratail, any tips you can give me on how to do the following, or if it's even possible?

Basically the idea is to have two scripts, one for the end of the level, this would save all of my g_ variables to a file, the second would be at the beginning of a level and would read that file and populate my variables, then either delete or empty the file. That way when you load a new game those variables would always be at zero, but moving on to a next level would remember your variables.

Is that possible?
If it is, any idea how i would do it?

I don't need full scripts, just a good description of what to do and why i'm doing it so i can learn it for myself would be great (although full scripts would be welcome ).

i would assume you would use the io write to save them, something like:

local f = assert(io.open("variables.txt", "w"))
f:write(firstVariable, "\n")
f:write(secondVariable, "\n")
f:write(thirdVariable, "\n")
f:close()

and I would assume you would use the io system to read it:

-- Open a file in read
file = io.open("variables.txt", "r")
-- set the default input file as variables.txt
io.input(file)


but i have no idea how to populate my variables with the data it reads.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 10th Apr 2017 09:25 Edited at: 10th Apr 2017 11:16
OK i think i understand the load and save of variables...

Save:


Load:


How would i zero the contents of the file once i've populated my variables without zeroing my variables too or delete the file?

Would it be as simple as adding os.remove ("variables.dat") at the end of the script?

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
lotgd
3D Media Maker
14
Years of Service
User Offline
Joined: 2nd Apr 2010
Location: italy
Posted: 10th Apr 2017 09:58 Edited at: 10th Apr 2017 15:49
os.remove for me dont work.

My Pc Specs : Win10pro64bit /Msi z97 / i74770k / gtx1070 / 16gb ram / SSD 850Evo
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 10th Apr 2017 10:00
The problem is that does the exact opposite of what i want to do lotgd, i want to change my variables to the numbers in the file, then get rid of the number in the file, but keep the numbers in my variables.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 10th Apr 2017 10:07 Edited at: 10th Apr 2017 10:08
Actually, i just thought about it.

The reason i wanted to remove the file was so that when you start a new game it wouldn't read the file and populate the variables with saved ones, but having a d'oh moment i realized that this wouldn't happen anyway because a new game would always be started from the beginning of the first level, and you wouldn't have the script to load the variables in the first level because you are starting with al variables at zero anyway lol.

Now i jst have to wait a whole day to test these scripts when i get home, not sure i can wait that long lol.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Ratall
15
Years of Service
User Offline
Joined: 29th Jun 2008
Location: Not Here
Posted: 10th Apr 2017 10:40 Edited at: 10th Apr 2017 10:49
Edit didn't see you guys were all ready talking on it I am a very slow typist .

Well belidos you had to ask didn't you!

Hear we go and may god have mercy on your mind.

What you need is a stripped down version of gamedata.lua most of gamedata.lua is to deal with the fact most of the stuff that starts with g_ that Lee uses is not saved as it changes between runs. plus that there was no way to tell what variables people would create.

the simplest way is to put the data you want is to throw up to the next level under one table.


then you can use something like this with out worrying about changing it when you think of new stuff to keep. and because it the table starts with g_ it will be preserved in the normal in level saves.


to save the data you of cause will need the save routine.
***if you don't want to put it all in one table just add additional saves after the first



The reason for the _G is tha lua stores "everything" in a metatable call _G
Warning :The save function is recursive and may cause bleeding from your ears when you try to follow it.

If you use this approach you will end up with a file full of assign commands which in theory you should be able to reload with a single dofile call.
I say in theory as I have failed to get it to work that way so I process one line at a time like this.



You will notice the load technique I use does not require you to code the variable name the save save the name and contents for you. If I got it right this if executed at the beginning of a level will not do anything if the file is missing.

I do not guarantee this will work if you put it in verboten as I wrote most of this off the top of my head bar the save function which I grabbed from gamedata.lua and tweeked to make fit.

There are other ways not using the save function, and if you want to use the save function without it all to one table you can make multiple calls to it.

QED

Important notes

I do not guarantee this will work if you put it in verboten as I wrote most of this off the top of my head bar the save function which I grabbed from gamedata.lua and tweaked to make fit.

Be aware the load function is built into lua and actually turns a string into an executable function it can contain anything that a function can but it works outside the normal scoping rules.

The first parameter in save defines the base name used in the restore , the second the actual base variable it grabs the data from to save. If you want to be overly clever you can restore to a different name than you save from provided you specified the change in the save.
Quod cito fit, cito perit.
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 10th Apr 2017 10:48
Firstly, thanks for the tips, much apreciated!

However i didn't understand a word of it LOL, sorry.

I am specifically avoiding tables, i don't want to use tables, i've never been able to understand them, and it would mean a rewrite of about thirty scripts, if i was to use individual variables would the code i posted a couple of posts back work? Or if not how would i write that?

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Ratall
15
Years of Service
User Offline
Joined: 29th Jun 2008
Location: Not Here
Posted: 10th Apr 2017 11:10 Edited at: 10th Apr 2017 11:24
Yes you can do it that way , it should work and I admit it is a lot easier to understand.

I would recommend on the save you use separate writes for each variable rather than one big one as it will make editing easier plus you left off the closing bracket on the big write in your example.



Good luck

ps you do realise you are saving and loading the variables as strings lua is pretty good at automatically converting when required but I wouldn't rely on it .
If you want to do maths better convert on the read. I believe you need to use the tonumber function. just stick the string in the parameter and it will spit out the number.
Quod cito fit, cito perit.
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 10th Apr 2017 11:14 Edited at: 10th Apr 2017 11:23
Awesome, thanks for the help, i wanted to try and do as much as i can myself, but here were just a few things i needed confirming, which you have done, i'll check it out when i get home tonight.

This is the first time i've even looked at the commands to load and save files, i've always been a bit daunted by it, but after looking at a few different articles on the net it seems fairly straight forward.

I will one day jump in and learn tables, but i just don't understand how they work.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 10th Apr 2017 23:31
Everything is lists, _G is a list, if you create a global variable, say myname = 'fred' then you have just added an entry to the _G list with a key of 'myname' and a value of 'fred'. _G is a special list in that if you don't specify a list name then _G is assumed so myname = 'fred' is equivalent to
_G['myname'] = 'fred' (or the alternative syntax _G.myname = 'fred').

_G is the global namespace list which also contains all the standard function name so for example print is really _G.print and math.sin is really _G.math.sin. You can create your own namespace lists as well using _ENV, see my quatlib for an example of its use.

One thing to note that is counter intuitive is that if you define a list and then assign it you don't get a new list, you get another pointer to the same list. So for example:

alist ={}

alist.var1 = 'astring'
alist.var2 = 10

blist = alist

blist.var1 = 'bstring'

Prompt(alist.var1) gives the result 'bstring'

if you wanted blist to be a copy of alist then you would do:

blist = {}

for k,v in pairs(alist) do blist[k] = v end

Hope that helps.
Been there, done that, got all the T-Shirts!
PM
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 11th Apr 2017 08:36
Quote: "Hope that helps. "


Well the headache it gave me took my mind off the toothache i already had LOL.

I just don't understand what any of the table stuff does, i don't understand how to create a table, or read a table, it's just something i can't get my head around.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 11th Apr 2017 09:43
Define a table with {}
supplies = {}
Now to fill the list you use [ ]
supplies[1] = "apple"
supplies[2] = "basket"
Then to read them you just need to call again the list and the desired index (number inside the square brackets)
Prompt (supplies[2])

Think of them like books might help, the table name must be unique (like a book title) and then every number inside the square brackets must be unique to each other (like page numbers, in GG we tend to use 'e' a lot as it is always unique between entities) and finally the stored data is the info on the pages (doesn't have to be strings)

For basic use there isn't any need to worry about the fancy stuff, just using them as a way to store the same sort of info for many objects is already very handy.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 11th Apr 2017 11:33
That actually made a lot of sense to me smallg, thanks for the explanation, i actually understood that.

However, i can't see the benefit of creating a table and listing all the variables inside the table over just listing all the variables, what makes a table better? I'm sure there is a lot, i'm just not seeing it.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
smallg
Community Leader
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 11th Apr 2017 15:20
Its for if you want to store lots of values in 1 variable - I.e. like all the location data for every entity... Rather than needing loads of variables you just use 1 list and give the entities all a separate "page"

It also makes sorting through and grabbing variables much faster as you can easily use loops.

Like (sorry if my syntax is slightly wrong, been using AGK lately which is very similar but not quite the same as lua)
y = 0
for p = 1, 99 do
TextCenterOnX(50,y,1,data[p])
y = y + 2
end

^ will very quickly show all values stored in the list "data" for values 1 to 99 on screen (just an example and of course this way assumes that values 1 to 99 actually exist).

Of course there are other ways to use lists but simply for making your code much smaller and neater is already a win when you start writing large scripts.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Belidos
3D Media Maker
8
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 11th Apr 2017 21:01 Edited at: 12th Apr 2017 08:48
OK, I've now tested saving and loading variables, and got it working, it wasn't quite how I coded it above, there were quite a few errors in there, but with trial and error I got it right!



Note: at the start I have collected 0 barrels, I collect 6 (displayed in the top left), then head for the winzone, when i'm loaded into the next level I still have 6 collected and can collect the 7th, on the second run through I start with 0 again, collect three and head to the winzone, again I have 3 when I load into level 2 and can collect a fourth.

Attached are the scripts and maps I used for the video above, if you want different or more variables, instructions below:

In the savevariables.lua find:



You can either changed g_collected to the variable you need (if you just want a different variable), or add a copy of the line below it and change g_collected to your variable, you can add as many as you like.

In the loadvariables.lua find:



You can either changed g_collected to the variable you need (if you just want a different variable), or add a copy of the line below it and change g_collected to your variable, you can add as many as you like.

To use them is simple, they both go into a trigger zone; savevariables.lua goes into a triggerzone placed at the winzone (i'd place it just in front so it has time to process before the winzone kicks in), and loadvariables.lua goes in a triggerzone over the start marker. You don't need to add loadvariables.lua to the first level as you will never need it there.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.

Attachments

Login to view attachments
Angiel
9
Years of Service
User Offline
Joined: 21st Dec 2014
Location: Italy
Posted: 16th Apr 2017 18:14
Thank you all for answers and solutions.
I've been busy with work but tomorrow I'll try everything.
sorry for my bad English, I write from Italy.

Login to post a reply

Server time is: 2024-04-25 01:22:06
Your offset time is: 2024-04-25 01:22:06