• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED] Simple Problem With Room Creation Code

Calvert

Member
When inside of a room, the room name should appear in the HUD screen.

In the Room Creation Code for my first room, I've got:
Code:
global.roomname = A00;
obj_controller Draw Event:
Code:
font=font_add_sprite(spr_fontcave,45,false,1)
draw_set_font(font)
display_set_gui_size(view_wview[0], view_hview[0]);

draw_text(1,1,string(global.roomname));
Whenever I enter my first room, I should read "A00" in the top left my of HUD screen. Instead, I get errors like this:

Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_controller:

global variable name 'roomname' index (100017) not set before reading it.
 at gml_Object_obj_controller_DrawEvent_1 (line 8) - draw_text(1,1,string(global.roomname));
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_controller_DrawEvent_1 (line 8)
It seems to be an issue with variables not being set in time, I don't know. Can I use a room creation code for something like this?
 
Last edited:

Calvert

Member
Put quotes around it...

Code:
global.roomname = "A00";
It works! I didn't know I had to put quotation marks there. Thanks.

Also for anyone reading this who has the same problem, I still couldn't get it working until I added that little ";" semicolon at the end of the room creation code. It makes all the difference.
 

TheouAegis

Member
A couple things here.

First off, you can draw numbers without making them strings; GM will convert them automatically. Your roomname isn't a number, though -- it has a letter in it (we'll ignore hexadecimals for now). Since it's not a number, you need quotes around it ("A00") to signify that it is a string and not a variable reference.

Second, what should have happened when you ran the game is it should have said the variable A00 (belonging to whatever the first object in your resource tree is) was not yet set, because your original code says, "set global.roomname to the value of the variable A00;" and clearly you don't have a variable A00. ....Why you were getting the error on global.roomname not being set is beyond me; it kept giving me errors on that A00, not global.roomname.

Third, the string() function is only used for converting numbers to strings so that you can concatenate them. For example, draw_text(x,y, score) will display just fine with no error; however draw_text(x,y,"Score: "+score) will throw an error because you are trying to concatenate a real with a string; the proper use is draw_text(x,y,"Score: "+string(score)). To put two numbers of arbitrary length together, you would make them both strings and concatenate them, like draw_text(x,y,string(score)+string(health)). If you knew the sizes of the numbers, you could just multiply one by however many places you need and then add them, though; for example, draw_text(x,y, score*100+health-1) if health would never go over 100.
 
Top