Object 2 cannot get the variable from Object 1

M

Minconan

Guest
The Error Report:

----------------------------------------------------------------------------------------------------------------------------------
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_hpbar:
Variable obj_hpbar.hp(100002, -2147483648) not set before reading it.
at gml_Object_obj_hpbar_Draw_0 (line 1) - if variable_instance_exists(obj_hp,hp) {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_hpbar_Draw_0 (line 1)


----------------------------------------------------------------------------------------------------------------------------------
The Code of obj_hpbar :


[Draw]:


if variable_instance_exists(obj_hp,hp) {
barhp = obj_hp.hp
}

if variable_instance_exists(obj_hp,max_hp){

batmaxhp = obj_hp.maxhp
}

bar = (barhp / barmaxhp) * 100;
draw_healthbar(96, 32, 432, 64, bar, c_black, c_red, c_lime, 0, true, true)

----------------------------------------------------------------------------------------------------------------------------------
I also tried:

barhp = obj_hp.hp

batmaxhp = obj_hp.max_hp

bar = (barhp / barmaxhp) * 100;
draw_healthbar(96, 32, 432, 64, bar, c_black, c_red, c_lime, 0, true, true)


----------------------------------------------------------------------------------------------------------------------------------
The Code of obj_hp:

[Create]:


hp = 80
maxhp = 80

----------------------------------------------------------------------------------------------------------------------------------
I want make a heath bar for player object and I use [obj_hp] to hold the hp variable,and then, I want
[obj_hpbar] to use the (hp) and (max_hp) to drawing hp bar,but seems like there is a problem in [obj_hpbar].
[obj_hpbar] can't get the variables from [obj_hp]
 
Last edited by a moderator:

chamaeleon

Member
thanks for remind but i want know why i can't get the variable from another object...
You can, it's just that without the quotes, and without dot-notation for another instance, just writing hp means trying to access hp on the current instance for which the code is executing, not some other random instance that may or may not have it declared. The fact that you wrote obj_hp as another parameter does not have anything to do with the interpretation of hp in that call.
 
M

Minconan

Guest
You can, it's just that without the quotes, and without dot-notation for another instance, just writing hp means trying to access hp on the current instance for which the code is executing, not some other random instance that may or may not have it declared. The fact that you wrote obj_hp as another parameter does not have anything to do with the interpretation of hp in that call.
you mean I need use [ if variable_instance_exists(obj_hp,"hp" ] ? what if i remove[ if variable_instance_exists(obj_hp,hp) ],only [barhp = obj_hp.hp] ?
 

chamaeleon

Member
you mean I need use [ if variable_instance_exists(obj_hp,"hp" ] ? what if i remove[ if variable_instance_exists(obj_hp,hp) ],only [barhp = obj_hp.hp] ?
If you use the quotes, that function should return true if the obj_hp instance contains a variable named hp. In the second case, barhp will become an instance variable on the obj_hpbar instance with the value of hp stored in the obj_hp instance. If obj_hp does not have hp as an instance variable your game would crash on that line.

And on the topic of object vs instances. I'm uncomfortable with the use of using things named obj_whatever when used in the context of instances. If I have a concrete instance in a room, I would not have them named obj_whatever. If you're relying on the object name rather than instance names, please keep in mind that as soon as you have more than one instance of a given object type code that uses object names may have unexpected behavior due to writing to all instances or reading from the "wrong" instance when trying to write or read variables rather than one in a particular. I recommend only using object names in conjunction with functions that takes objects rather than instances as parameters to avoid future grief. If obj_hpbar, obj_hp, etc. are actual instance names and not names of objects in your resource tree, ignore this paragraph.
 
Top