global object broken (ish?)

take this as given:

Code:
class Game_Map
  attr_reader :map_id
  def setup(id)
  @map_id = id
  ...
  $map = self
  end
end

def map_id
  return id
end

def id
 @map_id
end

this, works.
Code:
MODULE::HASH[integer] = val

this, doesn't.
Code:
MODULE::HASH[$map.map_id] = val

values are correct all around.
"p $map", returns Game_Map
"p $map.map_id" , returns @map_id
everything as expected.

MODULE::HASH[$map.map_id]
returns "can't read property map_id of nil"...... and I'm calling $map *countless* other times elsewhere, everywhere! D:<

thoughts?
and I'm calling $map *countless* other times elsewhere, everywhere! D:
And by the time you called it, probably the $map variable is not nil anymore

Where you set this though? If it's a Constant declaration, it won't work
Code:
MODULE::HASH[$map.map_id] = val
I came up with $map while working with move routes, for characters to access other event's properties in the same map.
while on any map, there's bound to be events moving around, so $map *works*.... I can see them moving.
while on the same map, at the time of talking to a character which triggers a battle, right before it triggers the battle it stores the current map_id in that hash so that the battle comes back to the same map after porting the player over to another map
as you can imagine, that back and forth of changing maps calls setup(id), which affects $map.
well,... if I use $map.map_id, it doesn't work.... if I manually put the id of the map that's supposed to be being handled, it does.

there's no way the code is wrong, it should work in both cases!
I have other maps using the same code, and with a manually edited id, it works
I *had* to manually write the id, because it wouldn't work the other way, which is written in function of the same map being played, which can't not-work!
I still can't really understand what are you trying to achieve and why would u you use $map instead of $game_map which basically they're the same. If use use the code "$map = self" which here "self" is the same as "$game_map", everything changed on "$game_map" would directly affecting "$map" as well without you even redefining "$map = self" on setup. Unless you perform a deep copy by using Marshal.dump. So it's really unnecessary.

But it's getting out of topic
From what I've understand from your word. You're trying to do this in chronology
> Event before the battle (show message, etc)
> Stores map ID in hash
> *crash here*
> Battle

Correct?
It might be better if you put your full script here as well as the script calls
it crashes at the moment of storing the ID, but only if I use map_id from the $map object, yes.
if I store the ID manually, and retrieve it as usual, it works just fine.

the storing of $map works, the hash within the module works, the battle sequence works, accessing the $map object works,.... but using the map_id result of $map as an integer to point into a hash, doesn't.
WHY?!?!?!?! ;_;;_;

I can't post the script, it's huge, it's a whole program.

(I didn't use $game_map because of a limitation in a script call which can't exceed X many characters, so I shortened it, and it stuck)
Are you saying, this works
Code:
id = $map.map_id
hash[id] = val
but it doesnt?
Code:
hash[$map.map_id] = val

As the error told you that $map is nil, have you tried to print on console right before the storing the hash if $map is indeed nil?

(I didn't use $game_map because of a limitation in a script call which can't exceed X many characters, so I shortened it, and it stuck)
you can use this just fine
Code:
def map
  $game_map
end

But really, without the context, I can't pinpoint where it might be wrong. And the mistake could be the silly one
As the error told you that $map is nil, have you tried to print on console right before the storing the hash if $map is indeed nil?
I don't need to, I'm standing right next to the guy, surrounded by events moving around, each one calling $map to get their bearings on me.
$map can't be nil anywhere! the very map I'm standing on defines $map at the moment of starting.

I also noticed this error only happens in the compiled game, not in the test run.
I can print everything to the console just fine in the test run, but the console is disabled in the compiled game.

I just thought of a theory, though:
I did change the way the interpreter interprets (?) script objects: they always look one instruction ahead to check if the next call is a script, for buffering purposes during a scripted message.
the hash thing is a script object, and the battle does trigger a map change.
........... maybe it's caching a map change (and a cleaning of the $map variable) just before it calls the hash? ¯\_ツ_/¯
I can print everything to the console just fine in the test run, but the console is disabled in the compiled game.
I forgot where I get this, but this should enable console on compiled game
Spoiler
Code:
def attach_console
  # Get game window text
  console_w = Win32API.new('user32','GetForegroundWindow', 'V', 'L').call
  buf_len = Win32API.new('user32','GetWindowTextLength', 'L', 'I').call(console_w)
  str = ' ' * (buf_len + 1)
  Win32API.new('user32', 'GetWindowText', 'LPI', 'I').call(console_w , str, str.length)
 
  # Initiate console
  Win32API.new('kernel32.dll', 'AllocConsole', '', '').call
  Win32API.new('kernel32.dll', 'SetConsoleTitle', 'P', '').call('RGSS Console')
  $stdout.reopen('CONOUT$')
 
  # Sometimes pressing F12 will put the editor in focus first,
  # so we have to remove the program's name
  game_title = str.strip
  game_title.sub! ' - RPG Maker VX Ace', ''
 
  # Set game window to be foreground. This is purely for user convenience
  hwnd = Win32API.new('user32.dll', 'FindWindow', 'PP','N').call(0, game_title)
  Win32API.new('user32.dll', 'SetForegroundWindow', 'P', '').call(hwnd)
end

# Call this at the beginning
attach_console

I don't need to
And you just refuses to do the debugging. Everything might looks fine, but could actually falls apart.
Debug everything, check if this line is executed, check if the variable is valid, check everything there, why would you don't want to just because everything else works?

Again, you don't even show how you do the script call (like how about you post a screenshot), nor showing the code
I did a whole debugging of it. I know my code!
I posted here because I'm faced with something that can't theoretically be possible!

I just downloaded the original script again... it's untouched since I compiled it.
I'm going to do a full recompile and try to force that error again... my theory, is that I may have missed a check somewhere, which I later fixed, but still slipped into the compiled version (because it's not happening in the latest version I have)
but I'm telling you: I did a whole printout of a million things a million times, I know how to track a bug.

(now that I see that console code, I think I *had* it some time, but I deleted it)
If you know how to debug, and u stumbled upon something theoretically impossible, then I'd say the same thing. It's impossible.
You posted here hoping for help, but without much context (and u still refuse to give one), I can't really help but giving debugging tips.

What would the replies you expect anyway? I can't think the one other than giving a debugging tip, code tip, and "can I look at how u did thing?". Good luck on your whole debugging regardless. Hope that piece of code above helps you at least.

Let me know if you find the culprit
I've isolated the problem to a failed check for refresh after loading a save.
$map is definitely working on one case, when defining $map as the player transfers maps, but not after loading a save file (aparently)

double-checking by forcing the moving events to call $map as well, after loading (should crash)
Oh I see, then that explains. In that case you might as well as use this if you want to shorten the script call without worrying about save
Code:
def map
  $game_map
end
I just performed the test, and the results are...... inconclusive.
it doesn't crash.... I don't get it.

a call to the same object, from two different events, in the same map, pulled from the same save, one time it crashes, another time it doesn't.

.........I'll cut my loses and replace $map for "def map", I'm tired of going over it.

EDIT: trying one last thing: I was putting the player start right on the map in question, I'll recompile moving the player start to the original position, and load from another map (from the player start perspective).... maybe the problem is moving the player from another map to the buggy map in question, which doesn't happen when the player start is already in that map.

EDIT2: confirmed.
loading a save file from a cold start (not previously running) of the game, doesn't reload $map.
the AI events load first, and I'm getting a move route crash, as expected.
previously, I was running reloads of save files with an already loaded game, which somehow kept $map defined somewhere, and it wasn't transferring the player anywhere because the player start was already there.
I'm done trying to fix it.... I'll go with the safe option.
One thing to mention would be that when you load a game the map is only reset up if your project was updated in the meantime, updating all events and returning them to their original positions (see lines 176 and 242-248 in the DataManager script; $data_system.version_id becomes a new random number each time the project is saved).

Apart from that I also second TheoAllens suggestion. Depending on how your lengthy script command looks like it might even be a good idea turning the entire scriptcode into a single method call.
I did look into version_id, and I'm sure I'm already counting on it somewhere else in the game, but somehow the saving/loading got mixed up.
it's working (ish) now, and I'm packing the thing up (again!) as it is.
the error came up because I had to do a full test because of some other totally unrelated error.... I'm done with patching it, it's gonna have to wait until a the new version comes along, where it is already patched.

Original thread: https://forums.rpgmakerweb.com/threads/global-object-broken-ish.92915/