+9 votes

Set_meta, has_meta and get_meta. When would you use this?

in Engine by (693 points)
recategorized by

1 Answer

+8 votes

Meta data can be useful in cases where it's difficult to distinguish objects from each other by looking at their primary attributes like .name or at their type.

Meta data is especially useful in collision handling where you might need to handle collisions with a, say, StaticBody2D - but your level is full of StaticBody2D's - the ground, platforms, walls etc. so to easily find out what type you've collided with you give them a meta data entry type (e.g. .set_meta('type','wall')) - later on when your player collides with something you look at the meta data for the object and handle the collision accordingly.

... you could also let objects share state via meta data or similar.

So, yeah - use it for whatever you find useful?

by (36 points)

Wouldn't groups be easier to use for this?

Because group you can only ask a node if it is in group X. If you have just "wall" and "ground" that query is simple. If you have more types, you will need to ask for each possible group. In effect you'd need to do multiple 'finds' instead of being able to use a match statement.

The other way to think of meta data is it is a way to add a "property" without needing to attach a script.

Groups are powerful when needing to address certain nodes anywhere in the scene tree.

Another aspect of meta is that it allows you to store any sort of data and use them dynamically in response to events. Mimicking that with groups would be unfeasible.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.