Get value of a variable from a string?

Ches Rowe

Member
For instance the string "mouse_x". Is there anyway in studio to get the value of the built-in variable mouse_x from that? From Google and scouring the manual I haven't found a way.
 
H

Homunculus

Guest
It was possible back to GM 8.1 when the code was interpreted, but it's not possible now since Studio introduced compiled code.

Can't remember the name, but there are a couple of extensions if you look around the forum and marketplace that supposedly allow you to do what you want, although I'm not really sure how that is achieved, it may be a really slow process
 

Ches Rowe

Member
It was possible back to GM 8.1 when the code was interpreted, but it's not possible now since Studio introduced compiled code.

Can't remember the name, but there are a couple of extensions if you look around the forum and marketplace that supposedly allow you to do what you want, although I'm not really sure how that is achieved, it may be a really slow process
Thanks! that's what I figured, i'm just needing it for a debug console, so speed isn't really an issue. I'll check out the marketplace.
 

Bingdom

Googledom
You can use a ds_map and access it by using map[? "mouse_x"]
Ds_maps allows you to create custom named variables, but can be resource intensive when acessed every step.
 
Last edited:
H

Homunculus

Guest
Just out of curiosity, why not use the game maker debugger?
 

Ches Rowe

Member
Just out of curiosity, why not use the game maker debugger?
I'm creating a command line basically. I have a text parser that can recognize function names, reals, objects, etc. Problem is, say with the function instance_create(x, y, obj_whatever) it works fine like instance_create(100, 100, obj_player); but with something like instance_create(mouse_x, mouse_y, obj_player); I would need a way of converting the strings of mouse_x/mouse_y to the actual values of mouse_x/mouse_y. I could do what Bingdom said and use a ds_map to store the values of the built-in variables and constantly update them, but I was just looking for a simpler way if that makes sense :)
 

Bingdom

Googledom
Oh, in that case just use real(). ;)

Edit:
Nevermind, that wouldn't work

If you want to draw text with strings mixed with numbers, do something like this:
Code:
draw_text(0,0,"Mouse_x: " + string(mouse_x));
Maybe that would help with debugging?
 

Ches Rowe

Member
Oh, in that case just use real(). ;)

Edit:
Nevermind, that wouldn't work

Tip:
If you want to draw text with strings mixed with numbers, do something like this:
Code:
draw_text(0,0,"Mouse_x: " + string(mouse_x));
I know :) , the issue is taking a string like "mouse_x" or "obj_player.hp" and turning that string into the real value of the variable. Which is basically impossible in studio from what I've gathered.
 

Nux

GameMaker Staff
GameMaker Dev.
this probably could have been done using execute_code(), but it wasn't recommended for use even when it was in the compiler.

I think your best bet is to use a switch statement where you pass the string as the selection, and have various variables you wish to read as the cases.
 
Top