SOLVED access an object via its name(a string)

Is it possible to access an object via its name (a string) ?
For example: I have a string named "obj_item", can I access the real obj_item (or its object_index)?
 

TsukaYuriko

☄️
Forum Staff
Moderator
Yes, via asset_get_index. Why are you trying to do this, though? Reflection is generally slow and should not be how you handle things, as there are usually more efficient solutions available.
 
Yes, via asset_get_index. Why are you trying to do this, though? Reflection is generally slow and should not be how you handle things, as there are usually more efficient solutions available.
I'm trying to control the objects on the client-side by receiving socket messages from the server.
Actually, I have implemented this using switch case statements(to detect which object to create)
and a ds_map (to restore the relation between object_indexes and their names).

However, this is not flexible enough, as I have to change a bunch of code once I add or modify any of my objects. Now I want to change the code structure to make life a bit easier. o_O

Any suggestions would be helpful.
 

TsukaYuriko

☄️
Forum Staff
Moderator
I'd suggest sending something other than object names. Ideally, this would be something that can be indexed, such as integers, as you can use them to index an array or similar data structure. This could be something akin to object IDs, but not directly object IDs, as that may cause problems if the client and server don't use the same IDs for the same objects (e.g. different versions). You could define your own object IDs to use for sending over the network, then translate them back into object IDs locally.

At that point, turning a network ID into an object ID is as simple as object_id = network_to_object_map[network_id];.
 
I'd suggest sending something other than object names. Ideally, this would be something that can be indexed, such as integers, as you can use them to index an array or similar data structure. This could be something akin to object IDs, but not directly object IDs, as that may cause problems if the client and server don't use the same IDs for the same objects (e.g. different versions). You could define your own object IDs to use for sending over the network, then translate them back into object IDs locally.

At that point, turning a network ID into an object ID is as simple as object_id = network_to_object_map[network_id];.
Reasonable. And now I have a pertinent idea.
I can use reflection to automatically generate the network_to_object_map after the game starts.
In this way, I no longer have to maintain the maps, and modify related code every time I change the objects.
Meanwhile, I can avoid the shortcoming of reflection (being slow).
 
I'd suggest sending something other than object names. Ideally, this would be something that can be indexed, such as integers, as you can use them to index an array or similar data structure. This could be something akin to object IDs, but not directly object IDs, as that may cause problems if the client and server don't use the same IDs for the same objects (e.g. different versions). You could define your own object IDs to use for sending over the network, then translate them back into object IDs locally.

At that point, turning a network ID into an object ID is as simple as object_id = network_to_object_map[network_id];.

I have succeeded on the Windows Platform.
But when I try to run it on html5 platform, it throws the following error:
Unhandled Exception - Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them in file undefined at line undefined
###game_end###-1

It seems that using a "network_to_object_map" goes against the strict mode of javascript, so I can't do this on html5 platform.
Again, any suggestions would be helpful. Thanks.
 
Top