GameMaker: Studio

GameMaker: Studio

查看统计:
Monolith Tyriss 2017 年 10 月 17 日 上午 10:53
Global variables not working globally!
Just like the title says. I have some code that tried to create globals called like 'global.playerkey_left' to define keys, so I could later add a key config fairly easily. Well, no matter WHERE I put the declaration for those global variables, I'd get the 'used before read' global variable error. Unless, very specifically, I put the global definition into the same code blocks that also used the globals. I made sure to put 'global.' in front of all uses of the variables, triple-checked the spelling. I can't see what's wrong! I have a screenshot of what my code looks like as well, here: https://prntscr.com/gykt5v . Is it possible that I'm hitting the error because this is a trial version of Studio 2? It didn't say anything about global variables being limited, so I'm not sure what's wrong!
< >
正在显示第 1 - 7 条,共 7 条留言
Blind 2017 年 10 月 17 日 上午 11:21 
globals work fine, the problem is the object you have your declaration in is not being called first. The normal way to fix this is to make sure your objects run in a specific order, but that can be complicated.

The easiest way to fix this is to quite simply make sure that your declaration event happens first no matter the scenario... one way to do this is to declare your globals in the room itself (the room is an object), another way is to have a small splash screen with only one object before your main game that declares ALL the variables and once done, warp to your desired first screen.
Monolith Tyriss 2017 年 10 月 17 日 上午 11:50 
Alright I changed it so that a single object existed in Room0, which executes the code that contains the 4 global.playerkey_ variable declarations, then it executes a 'go to next room' command. When I run it, again. It still has the fatal error 'global variable name 'playerkey_left' index (100002) not set before reading it.' Which comes up right after the switch case statement that uses playerkey_left. Everything I do, that keeps coming up unless the 'global' variables are declared in the same event action where they're used! So, I've only got two ideas left as to what's wrong: It's something to do with this being a trial version, or it's something to do with the switch: case statement having no code in it except breaks. Which is PISSING ME OFF because I don't know how to setup what I want for that bit of code! I want to get something running dangit!

Now, I did find a 'partial' solution. I went into the room creation code for room1, which is where the player object is. And in - there - I put the global variable declarations, and then it worked fine. So.. is it possible that global variables are only global to the room they're created in? This is very frustrating.. in normal C/C++ programs if you make a global variable it's global everywhere!!
Blind 2017 年 10 月 17 日 上午 11:57 
Ok, now that definitely is an anomaly - you did it correctly and global variables are contained in memory until game end. The only time you need to re-initialize globals is if you have a save/load system and that doesn't seem to be the case

Double check to see if there's a spelling error, and if that doesn't work - take a screenshot of the actual error (or copy/paste the text) so we can analyze it.
Trainiac 2017 年 10 月 17 日 下午 4:59 
This is just a guess, but try setting the object holding the global variables persistant so it moves with the rooms.
Monolith Tyriss 2017 年 10 月 18 日 下午 6:36 
Actually I'll do one better. Since this is SO insanely early in my work at making anything, I'll just upload a tiny .zip of the entire project. (It's only like 23k or so). See if anyone else gets errors trying to run it, maybe some other eyes looking at it can tell me what I did wrong! The file's here: https://www.dropbox.com/s/0d9jjowuc36jqfn/GMS2%20Test.zip?dl=0
charlesdophix 2017 年 10 月 22 日 下午 5:41 
did you make an initiate variables room, then set the variable in the creation code?
Monolith Tyriss 2017 年 10 月 25 日 上午 12:00 
引用自 charlesdophix
did you make an initiate variables room, then set the variable in the creation code?

If you look I've included the entire project file as a .zip above your comment. I did indeed try that, and it still didn't work!
< >
正在显示第 1 - 7 条,共 7 条留言
每页显示数: 1530 50

发帖日期: 2017 年 10 月 17 日 上午 10:53
回复数: 7