GDevelop C++ Platform
Platform for developing native games based on OpenGL and SFML with GDevelop
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
RuntimeObject Class Reference

A RuntimeObject is something displayed on the scene. More...

#include <RuntimeObject.h>

Inherited by RuntimeSpriteObject.

Public Types

using Ref = RuntimeObject *
 
using OwningPtr = RuntimeObject *
 

Public Member Functions

 RuntimeObject (RuntimeScene &scene, const gd::Object &object)
 Construct a RuntimeObject from an object. More...
 
 RuntimeObject (const RuntimeObject &object)
 Copy constructor. Calls Init().
 
RuntimeObjectoperator= (const RuntimeObject &object)
 Assignment operator. Calls Init().
 
virtual ~RuntimeObject ()
 Default destructor.
 
virtual std::unique_ptr< RuntimeObjectClone () const
 Must return a pointer to a copy of the object. A such method is needed to do polymorphic copies. More...
 
virtual bool ExtraInitializationFromInitialInstance (const gd::InitialInstance &position)
 Called by RuntimeScene when creating the RuntimeObject from an initial instance. More...
 
virtual bool Draw (sf::RenderTarget &renderTarget)
 Draw the object. More...
 
const gd::String & GetName () const
 Get the name of the object.
 
const gd::String & GetType () const
 Get the type of the object.
 
int GetZOrder () const
 Query the Z order of the object.
 
void SetZOrder (int zOrder_)
 Change the Z order of the object.
 
bool IsHidden () const
 Return if the object is hidden or not.
 
bool IsVisible () const
 Return if the object is visible ( not hidden )
 
void SetHidden (bool hide=true)
 Hide/Show the object.
 
void SetLayer (const gd::String &layer_)
 Change the layer of the object.
 
const gd::String & GetLayer () const
 Get the layer of the object.
 
bool IsOnLayer (const gd::String &layer_) const
 Check if the object is on a layer.
 
sf::FloatRect GetAABB () const
 Get the object AABB.
 
virtual std::vector< Polygon2d > GetHitBoxes () const
 Get the object hitbox(es) More...
 
virtual std::vector< Polygon2d > GetHitBoxes (sf::FloatRect hint) const
 Get the object hitbox(es) preferably intersecting with hint. More...
 
bool IsCollidingWith (RuntimeObject *other, bool ignoreTouchingEdges=false)
 Check collision between two objects using their hitboxes. More...
 
bool IsCollidingWithPoint (float pointX, float pointY)
 Check if a point is inside the object collision hitboxes. More...
 
RaycastResult RaycastTest (float x, float y, float angle, float dist, bool closest)
 Check if a ray intersect any object hitbox. More...
 
bool SeparateFromObjects (const std::vector< RuntimeObject *> &objects, bool ignoreTouchingEdges=false)
 Check collision with each object of the list using their hitboxes, and move the object according to the sum of the move vector returned by each collision test. More...
 
virtual bool CursorOnObject (RuntimeScene &scene, bool accurate)
 Return true if the cursor is on the object. More...
 
virtual void Update (const RuntimeScene &scene)
 Called at each frame, before events and rendering. More...
 
signed long long GetElapsedTime (const RuntimeScene &scene) const
 Return the time elapsed since the last frame, in microseconds, for the object. More...
 
virtual float GetWidth () const
 Get the width of the object, in pixels.
 
virtual float GetHeight () const
 Get the height of the object, in pixels.
 
virtual void SetWidth (float)
 Set the new width of the object. More...
 
virtual void SetHeight (float)
 Set the new height of the object. More...
 
virtual bool SetAngle (float)
 Get the angle of the object, in degrees. More...
 
virtual float GetAngle () const
 Get the angle of the object, in degrees.
 
float GetX () const
 Get the X coordinate of the object in the layout.
 
float GetY () const
 Get the Y coordinate of the object in the layout.
 
void SetX (float x_)
 Change X position of the object. More...
 
void SetY (float y_)
 Change Y position of the object. More...
 
virtual void OnPositionChanged ()
 
virtual float GetDrawableX () const
 Get the real X position where is renderer the object. More...
 
virtual float GetDrawableY () const
 Get the real Y position where is renderer the object. More...
 
virtual float GetCenterX () const
 Get the X position of the center, relative to the position returned by GetDrawableX().
 
virtual float GetCenterY () const
 Get the Y position of the center, relative to the position returned by GetDrawableY().
 
double GetSqDistanceTo (double x, double y)
 Get squared distance, in pixel, between the object and the specified position. More...
 
bool ClearForce ()
 Delete all forces applied to the object.
 
Object variables

Members functions providing access to the object variables.

const RuntimeVariablesContainerGetVariables () const
 Provide access to variables of the object.
 
RuntimeVariablesContainerGetVariables ()
 Provide access to variables of the object.
 
Behavior related functions

Functions related to behaviors management.

void DoBehaviorsPreEvents (RuntimeScene &scene)
 Call each behavior so that they do their work before events.
 
void DoBehaviorsPostEvents (RuntimeScene &scene)
 Call each behavior so that they do their work after the events were runn.
 
RuntimeBehaviorGetBehaviorRawPointer (const gd::String &name)
 
RuntimeBehaviorGetBehaviorRawPointer (const gd::String &name) const
 
bool HasBehaviorNamed (const gd::String &name) const
 Return true if the object has the behavior with the specified name.
 
void AddBehavior (const gd::String &name, std::unique_ptr< RuntimeBehavior > behavior)
 Add the specified behavior to the object.
 
Others IDE related functions

Members functions used by the IDE

virtual void GetPropertyForDebugger (std::size_t propertyNb, gd::String &name, gd::String &value) const
 Called by the debugger so as to get a property value and name. More...
 
virtual bool ChangeProperty (std::size_t propertyNb, gd::String newValue)
 Called by the debugger so as to update a property. More...
 
virtual std::size_t GetNumberOfProperties () const
 Must return the number of available properties for the debugger.
 

Protected Member Functions

void Init (const RuntimeObject &object)
 Initialize object using another object. Used by copy-ctor and assign-op. More...
 

Protected Attributes

gd::String name
 The full name of the object.
 
gd::String type
 
float X
 X position on the scene.
 
float Y
 Y position on the scene.
 
int zOrder
 
bool hidden
 True to prevent the object from being rendered.
 
gd::String layer
 Name of the layer on which the object is.
 
std::map< gd::String, std::unique_ptr< RuntimeBehavior > > behaviors
 
RuntimeVariablesContainer objectVariables
 List of the variables of the object.
 
std::vector< Forceforces
 Forces applied to the object.
 

Forces

Members functions providing access to built-in force system used to move the objects

Force force5
 
bool UpdateForce (float ElapsedTime)
 
float TotalForceX () const
 
float TotalForceY () const
 
float TotalForceAngle () const
 
float TotalForceLength () const
 

Functions meant to be used by events generated code

void DeleteFromScene (RuntimeScene &scene)
 
void PutAroundAPosition (float positionX, float positionY, float distance, float angleInDegrees)
 
void AddForce (float x, float y, float clearing)
 
void AddForceUsingPolarCoordinates (float angle, float length, float clearing)
 
void AddForceTowardPosition (float positionX, float positionY, float length, float clearing)
 
void AddForceToMoveAround (float positionX, float positionY, float angularVelocity, float distance, float clearing)
 
void AddForceTowardObject (RuntimeObject *object, float length, float clearing)
 
void AddForceToMoveAroundObject (RuntimeObject *object, float velocity, float length, float clearing)
 
void PutAroundObject (RuntimeObject *object, float length, float angleInDegrees)
 
void RotateTowardPosition (float Xposition, float Yposition, float speed, RuntimeScene &scene)
 
void RotateTowardAngle (float angleInDegrees, float speed, RuntimeScene &scene)
 
void Rotate (float speed, RuntimeScene &scene)
 
bool VariableExists (const gd::String &variable)
 
void SetXY (const char *xOperator, float xValue, const char *yOperator, float yValue)
 
void Duplicate (RuntimeScene &scene, std::map< gd::String, std::vector< RuntimeObject *> *> pickedObjectLists)
 
void ActivateBehavior (const gd::String &behaviorName, bool activate=true)
 
bool BehaviorActivated (const gd::String &behaviorName)
 
bool IsStopped ()
 
bool TestAngleOfDisplacement (float angle, float tolerance)
 
double GetSqDistanceWithObject (RuntimeObject *other)
 
double GetDistanceWithObject (RuntimeObject *other)
 
bool SeparateFromObjects (std::map< gd::String, std::vector< RuntimeObject *> *> pickedObjectLists, bool ignoreTouchingEdges=false)
 
void SeparateObjectsWithoutForces (std::map< gd::String, std::vector< RuntimeObject *> *> pickedObjectLists)
 
void SeparateObjectsWithForces (std::map< gd::String, std::vector< RuntimeObject *> *> pickedObjectLists)
 
static gd::Variable & ReturnVariable (gd::Variable &variable)
 
static double GetVariableValue (const gd::Variable &variable)
 
static const gd::String & GetVariableString (const gd::Variable &variable)
 
static bool VariableChildExists (const gd::Variable &variable, const gd::String &childName)
 
static void VariableRemoveChild (gd::Variable &variable, const gd::String &childName)
 
static void VariableClearChildren (gd::Variable &variable)
 
static unsigned int GetVariableChildCount (gd::Variable &variable)
 

Detailed Description

A RuntimeObject is something displayed on the scene.

Games don't directly use this class: Extensions can provide object by deriving from this class, and redefining functions:

See also gd::Object which is used to store the initial objects before they are put on the scene.

See also
gd::Object

Constructor & Destructor Documentation

◆ RuntimeObject()

RuntimeObject::RuntimeObject ( RuntimeScene scene,
const gd::Object &  object 
)

Construct a RuntimeObject from an object.

The default implementation already takes care of setting common properties ( name, type, behaviors... ). Be sure to call the original constructor if you redefine it:

MyRuntimeObject(RuntimeScene & scene, const MyObject
& object) : RuntimeObject(scene, object)
{
//...
}
Note
The constructor can take a specialized gd::Object as its second parameter which is the gd::Object sub-class declared in ExtensionBase::AddRuntimeObject (the first template parameter)

Member Function Documentation

◆ AddForceTowardPosition()

void RuntimeObject::AddForceTowardPosition ( float  positionX,
float  positionY,
float  length,
float  clearing 
)

Add a force toward a position

◆ ChangeProperty()

bool RuntimeObject::ChangeProperty ( std::size_t  propertyNb,
gd::String  newValue 
)
virtual

Called by the debugger so as to update a property.

Parameters
propertyNbthe property number
newValuethe new value as an UTF8 string
Returns
true if property was changed, false if it not possible.

Implementation example:

if ( propertyNb == 0 ) {return SetSomeProperty(ToFloat(newValue));}
else if ( propertyNb == 1 ) {return false;} //Changing property is not
allowed: returning false.

Reimplemented in RuntimeSpriteObject.

◆ Clone()

virtual std::unique_ptr<RuntimeObject> RuntimeObject::Clone ( ) const
inlinevirtual

Must return a pointer to a copy of the object. A such method is needed to do polymorphic copies.

Just redefine this method in your derived object class like this:

return new MyRuntimeObject(*this);

Reimplemented in RuntimeSpriteObject.

◆ CursorOnObject()

bool RuntimeObject::CursorOnObject ( RuntimeScene scene,
bool  accurate 
)
virtual

Return true if the cursor is on the object.

Parameters
sceneThe scene the object belongs to.
accurateIf true, the test should be precise (depending on the object type). Otherwise, a simple bouding box test is made.
Returns
bool if the cursor is on the object

Reimplemented in RuntimeSpriteObject.

◆ Draw()

virtual bool RuntimeObject::Draw ( sf::RenderTarget &  renderTarget)
inlinevirtual

Draw the object.

Parameters
renderTargetThe SFML Rendertarget where object must be drawn.

Reimplemented in RuntimeSpriteObject.

◆ ExtraInitializationFromInitialInstance()

virtual bool RuntimeObject::ExtraInitializationFromInitialInstance ( const gd::InitialInstance &  position)
inlinevirtual

Called by RuntimeScene when creating the RuntimeObject from an initial instance.

Note
The RuntimeScene already takes care of setting common properties (position, angle... ) according to the InitialInstance. You only need to initialize extra properties specific to your object.

Reimplemented in RuntimeSpriteObject.

◆ GetBehaviorRawPointer() [1/2]

RuntimeBehavior * RuntimeObject::GetBehaviorRawPointer ( const gd::String &  name)

Only used by GD events generated code

◆ GetBehaviorRawPointer() [2/2]

RuntimeBehavior * RuntimeObject::GetBehaviorRawPointer ( const gd::String &  name) const

Only used by GD events generated code

◆ GetDrawableX()

virtual float RuntimeObject::GetDrawableX ( ) const
inlinevirtual

Get the real X position where is renderer the object.

Most of the time, this will return the same value as GetX().
However, some objects may allow the origin point to be moved: In this case, you need to redefine this method.

Reimplemented in RuntimeSpriteObject.

◆ GetDrawableY()

virtual float RuntimeObject::GetDrawableY ( ) const
inlinevirtual

Get the real Y position where is renderer the object.

Most of the time, this will return the same value as GetY().
However, some objects may allow the origin point to be moved: In this case, you need to redefine this method.

Reimplemented in RuntimeSpriteObject.

◆ GetElapsedTime()

signed long long RuntimeObject::GetElapsedTime ( const RuntimeScene scene) const

Return the time elapsed since the last frame, in microseconds, for the object.

Objects can have different elapsed time if they are on layers with different time scales.

◆ GetHitBoxes() [1/2]

std::vector< Polygon2d > RuntimeObject::GetHitBoxes ( ) const
virtual

Get the object hitbox(es)

Note
Default implementation returns a basic bounding box, according to the object width/height and angle.

Reimplemented in RuntimeSpriteObject.

◆ GetHitBoxes() [2/2]

std::vector< Polygon2d > RuntimeObject::GetHitBoxes ( sf::FloatRect  hint) const
virtual

Get the object hitbox(es) preferably intersecting with hint.

Note
The default implementation returns all the hitbox given by GetHitBoxes()

◆ GetPropertyForDebugger()

void RuntimeObject::GetPropertyForDebugger ( std::size_t  propertyNb,
gd::String &  name,
gd::String &  value 
) const
virtual

Called by the debugger so as to get a property value and name.

Warning
value should be set to an UTF8 encoded string. Implementation example:
if ( propertyNb == 0 ) {name = _("MyObjectProperty"); value =
gd::String::From(GetSomeProperty());} else if ( propertyNb == 1 ) {name =
_("AnotherProperty"); value = GetAnotherPropety();}

Reimplemented in RuntimeSpriteObject.

◆ GetSqDistanceTo()

double RuntimeObject::GetSqDistanceTo ( double  x,
double  y 
)

Get squared distance, in pixel, between the object and the specified position.

Parameters
xX coordinate of the point
yY coordinate of the point

◆ Init()

void RuntimeObject::Init ( const RuntimeObject object)
protected

Initialize object using another object. Used by copy-ctor and assign-op.

Warning
Don't forget to update me if members were changed!

◆ IsCollidingWith()

bool RuntimeObject::IsCollidingWith ( RuntimeObject other,
bool  ignoreTouchingEdges = false 
)

Check collision between two objects using their hitboxes.

Note
If bounding circles of objects are not colliding, hit boxes are not tested.
Parameters
otherThe other object for collision to be tested against.
ignoreTouchingEdgesIf true, then edges that are touching each other, without the hitbox polygons actually overlapping, won't be considered in collision.

◆ IsCollidingWithPoint()

bool RuntimeObject::IsCollidingWithPoint ( float  pointX,
float  pointY 
)

Check if a point is inside the object collision hitboxes.

Parameters
pointXThe point x coordinate.
pointYThe point y coordinate.
Returns
true if the point is inside the object collision hitboxes.

◆ OnPositionChanged()

virtual void RuntimeObject::OnPositionChanged ( )
inlinevirtual

Object can use this function to do special work when position is changed.

Reimplemented in RuntimeSpriteObject.

◆ RaycastTest()

RaycastResult RuntimeObject::RaycastTest ( float  x,
float  y,
float  angle,
float  dist,
bool  closest 
)

Check if a ray intersect any object hitbox.

Parameters
xThe raycast source X
yThe raycast source Y
angleThe raycast angle
distThe raycast max distance
closestGet the closest or farthest collision mask result?
Returns
A raycast result with the contact points and distances

◆ SeparateFromObjects()

bool RuntimeObject::SeparateFromObjects ( const std::vector< RuntimeObject *> &  objects,
bool  ignoreTouchingEdges = false 
)

Check collision with each object of the list using their hitboxes, and move the object according to the sum of the move vector returned by each collision test.

Note
Bounding circles of objects are not checked.
Parameters
objectsThe vector of objects to be used.
ignoreTouchingEdgesIf true, then edges that are touching each other, without the hitbox polygons actually overlapping, won't be considered in collision.
Returns
true if the object was moved.

◆ SeparateObjectsWithForces()

void RuntimeObject::SeparateObjectsWithForces ( std::map< gd::String, std::vector< RuntimeObject *> *>  pickedObjectLists)

◆ SeparateObjectsWithoutForces()

void RuntimeObject::SeparateObjectsWithoutForces ( std::map< gd::String, std::vector< RuntimeObject *> *>  pickedObjectLists)

◆ SetAngle()

virtual bool RuntimeObject::SetAngle ( float  )
inlinevirtual

Get the angle of the object, in degrees.

The angle can be not changed if the object doesn't want/is not designed to.

Reimplemented in RuntimeSpriteObject.

◆ SetHeight()

virtual void RuntimeObject::SetHeight ( float  )
inlinevirtual

Set the new height of the object.

The height can be not changed if the object doesn't want/is not designed to.

Reimplemented in RuntimeSpriteObject.

◆ SetWidth()

virtual void RuntimeObject::SetWidth ( float  )
inlinevirtual

Set the new width of the object.

The width can be not changed if the object doesn't want/is not designed to.

Reimplemented in RuntimeSpriteObject.

◆ SetX()

void RuntimeObject::SetX ( float  x_)
inline

Change X position of the object.

Note
This method cannot be redefined: Redefine OnPositionChanged() to do extra work if needed.

◆ SetY()

void RuntimeObject::SetY ( float  y_)
inline

Change Y position of the object.

Note
This method cannot be redefined: Redefine OnPositionChanged() to do extra work if needed.

◆ Update()

virtual void RuntimeObject::Update ( const RuntimeScene scene)
inlinevirtual

Called at each frame, before events and rendering.

Note
The default implementation does nothing.

Reimplemented in RuntimeSpriteObject.

◆ UpdateForce()

bool RuntimeObject::UpdateForce ( float  ElapsedTime)

Automatically called at each frame so as to update forces applied on the object.

Member Data Documentation

◆ behaviors

std::map<gd::String, std::unique_ptr<RuntimeBehavior> > RuntimeObject::behaviors
protected

Contains all behaviors of the object. Behaviors are the ownership of the object

◆ force5

Force RuntimeObject::force5
Deprecated:
Old custom force used to manage collisions.

◆ type

gd::String RuntimeObject::type
protected

Which type is the object. ( To test if we can do something reserved to some objects with it )

◆ zOrder

int RuntimeObject::zOrder
protected

Z order on the scene, to choose if an object is displayed before another object.


The documentation for this class was generated from the following files: