Legacy GM How to add to ds_list instance variable if variable exists?

F

Facet

Guest
Hello

I'm trying to save instance information to ds_list, and everything works well, but some problems with variables.
I want to add variable only if exists and trying something like this:
Code:
if !is_undefined(inst.hp)
                    ds_list_add(list, inst.hp);
I'm sure I'm wrong, but how to do it properly?

Thank you ++
 

SnoutUp

Member
Could you elaborate, why are you doing this? Because this just looks wrong... Why would you add hp value to the list? Maybe ds_map would be better for that if you plan to save more properties of the instance?
I think you can't use is_undefined with properties of the instances, unless hp is already set to undefined (hp = undefined;). I just learned that myself by trying out your code.

It would be the best if you only use instances which have a hp property while saving that hp property. Check what kind of object it is, add hp if that object has hp.
 
F

Facet

Guest
Could you elaborate, why are you doing this? Because this just looks wrong... Why would you add hp value to the list? Maybe ds_map would be better for that if you plan to save more properties of the instance?
I think you can't use is_undefined with properties of the instances, unless hp is already set to undefined (hp = undefined;). I just learned that myself by trying out your code.

It would be the best if you only use instances which have a hp property while saving that hp property. Check what kind of object it is, add hp if that object has hp.
This is only part of code of course, but here problem is visible I think. I'm adding list to ds_map and this working good. Whole code is too long, but this is loop with all instances in room and they are added to list, their x,y and other data, then it's stored in ds_map - it working good and saving/loading map. I just don't know how to add variable if exists and not add when not exists.

For example:
obj_enemy have hp and it should be ds_list_add(list, inst.hp) - something like this
obj_tree don't have hp and it can't be add to list, so I want to avoid it.
But I have to add both obj_tree and obj_enemy to one list - if possible. Maybe not.

edited

Ok, for now I have something like this and it working:
Code:
if object_get_parent(inst.object_index) == obj_enemy_parent
                    ds_list_add(list, inst.hp);
Maybe better solution?
 
Last edited by a moderator:

SnoutUp

Member
Well, I would give them some sort of type variable like... type = TYPE_ENEMY (this could be constant so you could have autocomplete) and TYPE_OBSTACLE and etc. So you would know that obstacle type doesn't have HP and you don't need to read or save it. I still wouldn't use lists for this. Add object properties to the map, then add it to the list, then add that map to the list. Don't forget to use ds_list_mark_as_map if you're gonna use json_encode later.


This is only part of code of course, but here problem is visible I think. I'm adding list to ds_map and this working good. Whole code is too long, but this is loop with all instances in room and they are added to list, their x,y and other data, then it's stored in ds_map - it working good and saving/loading map. I just don't know how to add variable if exists and not add when not exists.

For example:
obj_enemy have hp and it should be ds_list_add(list, inst.hp) - something like this
obj_tree don't have hp and it can't be add to list, so I want to avoid it.
But I have to add both obj_tree and obj_enemy to one list - if possible. Maybe not.
 
F

Facet

Guest
You are right, but I think parent system works better and is more easy to set/unset. With TYPE_**** you have to add code in create event what is longer than select parent. From my perspective. And here maybe better, different solution.

Anyway, is_undefined for variables would be best solution imo :D
 

SnoutUp

Member
You are right, but I think parent system works better and is more easy to set/unset. With TYPE_**** you have to add code in create event what is longer than select parent. From my perspective. And here maybe better, different solution.

Anyway, is_undefined for variables would be best solution imo :D
Well, suit yourself. Having "type" variable would give you freedom from parent system, because it can get complicated when project grows. Anyway, you can just add hp or all the variables you need to all the objects then. Have a ParentObject which would hold everything you need and inherit its create event.
 
A

Aura

Guest
You're misunderstanding how is_undefined() works. It does not tell you if a variable has been declared. The functionality for that is now obsolete and this function is NOT a replacement. It tells you if a value is not defined.

The Manual entry on data types said:
An undefined value (also known as a "null" value") is one where an expression doesn't have a correct value, although it is syntactically correct, and so must return something. For example, say you have a ds_map and use the function ds_map_find_value(). Now, what happens when the map does not have the value being looked for? Well, since the function is correctly formatted, and the issue is that the no such value exists, then it would return the constant undefined, and you can check for this constant as you would check for true or any other value.
The function checks for that; using it for checking if a variable has been declared is a wrong interpretation of the Manual.
 
Top