Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dmSDK: Added possibility to load collection proxies and spawn factories from C++ #8667

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from

Conversation

JCash
Copy link
Contributor

@JCash JCash commented Mar 12, 2024

This update adds more functionality to our C++ sdk, which is part of our effort to allow a developer to write the game logic using C++.
It is an ongoing task, and we'll add more functionality along the way.

dmsdk/extension/extension.h

Allows for more control of from within an extension:

  • dmExtension::EVENT_ID_ENGINE_INITIALIZED - called at the very end of dmEngine::Init()
  • dmExtension::EVENT_ID_ENGINE_DELETE - called at the very beginning of dmEngine::Delete()

dmsdk/gameobject/gameobject_props.h

Removed Lua dependency when creating game object properties.
Added property container type to sdk for making it possible to spawn game objects:

  • dmGameObject::HPropertyContainer - Used when spawning game objects/factories
  • dmGameObject::HPropertyContainerBuilder - Used when creating a HPropertyContainer

dmsdk/gameobject/gameobject.h

Added more functionality that allows for spawning game objects, and getting components.

dmsdk/gameobject/component.h

Added more type safety (also for readability) using two new typedefs:

  • dmGameObject::HComponentWorld
  • dmGameObject::HComponentInternal

Misc

Removed deprecated function:

  • dmGameObject::GetComponentFromLua(), in favor of dmScript::GetComponentFromLua()

Fixes: #8930

PR checklist

  • Code
    • Add engine and/or editor unit tests.
    • New and changed code follows the overall code style of existing code
    • Add comments where needed
  • Documentation
    • Make sure that API documentation is updated in code comments
    • Make sure that manuals are updated (in github.com/defold/doc)
  • Prepare pull request and affected issue for automatic release notes generator
    • Pull request - Write a message that explains what this pull request does. What was the problem? How was it solved? What are the changes to APIs or the new APIs introduced? This message will be used in the generated release notes. Make sure it is well written and understandable for a user of Defold.
    • Pull request - Write a pull request title that in a sentence summarises what the pull request does. Do not include "Issue-1234 ..." in the title. This text will be used in the generated release notes.
    • Pull request - Link the pull request to the issue(s) it is closing. Use on of the approved closing keywords.
    • Affected issue - Assign the issue to a project. Do not assign the pull request to a project if there is an issue which the pull request closes.
    • Affected issue - Assign the "breaking change" label to the issue if introducing a breaking change.
    • Affected issue - Assign the "skip release notes" is the issue should not be included in the generated release notes.

Example of a well written PR description:

  1. Start with the user facing changes. This will end up in the release notes.
  2. Add one of the GitHub approved closing keywords
  3. Optionally also add the technical changes made. This is information that might help the reviewer. It will not show up in the release notes. Technical changes are identified by a line starting with one of these:
    1. ### Technical changes
    2. Technical changes:
    3. Technical notes:
There was a anomaly in the carbon chroniton propeller, introduced in version 8.10.2. This fix will make sure to reset the phaser collector on application startup.

Fixes #1234

### Technical changes
* Pay special attention to line 23 of phaser_collector.clj as it contains some interesting optimizations
* The propeller code was not taking into account a negative phase.

Comment on lines +277 to +289
{
dmExtension::Params params;
params.m_ConfigFile = engine->m_Config;
params.m_ResourceFactory = engine->m_Factory;
if (engine->m_SharedScriptContext) {
params.m_L = dmScript::GetLuaState(engine->m_SharedScriptContext);
} else {
params.m_L = dmScript::GetLuaState(engine->m_GOScriptContext);
}
dmExtension::Event event;
event.m_Event = dmExtension::EVENT_ID_ENGINE_DELETE;
dmExtension::DispatchEvent( &params, &event );
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New feature:
Since the extension system isn't aware of the game loop as such, I introduced two new events EVENT_ID_ENGINE_DELETE and EVENT_ID_ENGINE_INITIALIZED so that the extension can act upon it.

I.e. this helps with things like: don't load things before the game has been initialized, and don't delete your things too late.

Comment on lines 220 to 222
dmGameObject::HComponentWorld m_World;
/// User data storage pointer
uintptr_t* m_UserData;
dmGameObject::HComponentInternal m_UserData;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding more type safety.

@@ -86,6 +86,34 @@ namespace dmGameObject
*/
typedef struct CollectionHandle* HCollection;

/*#
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from internal header.

* @typedef
* @name HComponent
*/
typedef void* HComponent;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A HComponent, is intended to be a fully resolved pointer, to be used with our c++ sdk's.

* @typedef
* @name HComponentInternal
*/
typedef uintptr_t HComponentInternal;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A HComponentInternal, may be anything that the component type needs (e.g. a uin16_t handle). It needs to be resolved (handled by our component extensions) to get an actual pointer.

@@ -97,18 +104,46 @@ namespace dmGameSystem
return dmGameObject::UPDATE_RESULT_OK;
}

void LoadComplete(CollectionProxyComponent* proxy)
void LoadComplete(CollectionProxyComponent* proxy, dmGameObject::Result result)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusing the same flow for two use cases: messages vs c++ invokation

{
FactoryResource* resource = CompFactoryGetResource(component);
(void)world;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While not technically needed, it seems nice to have a similar api from all the components in the form "CompMyTypeDoSomething(world, component)"

Comment on lines +462 to +482
dmGameObject::HInstance CompFactorySpawn(HFactoryWorld world, HFactoryComponent component, dmGameObject::HCollection collection,
uint32_t index, dmhash_t id,
const dmVMath::Point3& position, const dmVMath::Quat& rotation, const dmVMath::Vector3& scale,
dmGameObject::HPropertyContainer properties)
{
dmGameObject::HPrototype prototype = CompFactoryGetPrototype(world, component);
const char* path = CompFactoryGetPrototypePath(world, component);

dmGameObject::HInstance instance = dmGameObject::Spawn(collection, prototype, path, id, properties, position, rotation, scale);
if (instance != 0x0)
{
dmGameObject::AssignInstanceIndex(index, instance);
}
else
{
dmGameObject::ReleaseInstanceIndex(index, collection);
}
return instance;
}


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from script_factory.cpp, helping us to encapsulate the functionality more.

@@ -557,11 +557,11 @@ namespace dmGameSystem
InitParametersFromDescription(component, ddf);
}

void* CompLabelGetComponent(const dmGameObject::ComponentGetParams& params)
dmGameObject::HComponent CompLabelGetComponent(const dmGameObject::ComponentGetParams& params)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still a void*, but easier to understand what it's supposed to return.

@@ -235,16 +237,51 @@ namespace dmGameSystem
* end
* ```
*/
static int FactoryComp_CreateWithMessage(lua_State* L, dmGameObject::HCollection collection, dmGameObject::HInstance sender_instance, dmMessage::URL* receiver,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the more surprising case: When does this actually happen?
I.e. we call factory.create, but don't have a game object instance set?

I left it "as-is" for now. Potential candidate for removal imho.

@JCash JCash requested a review from Jhonnyg March 12, 2024 15:41
Jhonnyg
Jhonnyg previously approved these changes Mar 13, 2024
Copy link
Contributor

@Jhonnyg Jhonnyg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of stuff! But great stuff!

@@ -123,7 +123,7 @@ static dmGameObject::HInstance Spawn(dmResource::HFactory factory, dmGameObject:

static dmGameObject::HInstance Spawn(dmResource::HFactory factory, dmGameObject::HCollection collection, const char* prototype_name, dmhash_t id)
{
return Spawn(factory, collection, prototype_name, id, 0, 0, Point3(0, 0, 0), Quat(0, 0, 0, 1), Vector3(1, 1, 1));
return Spawn(factory, collection, prototype_name, id, 0, Point3(0, 0, 0), Quat(0, 0, 0, 1), Vector3(1, 1, 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this using the new spawn function introduced in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the local test_gamesys.cpp:Spawn (see above), and it in turn uses the new dmGameObject::Spawn which takes a HProperties.

JCash added 2 commits May 15, 2024 14:15
# Conflicts:
#	engine/gameobject/src/gameobject/gameobject.cpp
#	engine/gamesys/src/gamesys/test/test_gamesys.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spawn factories (stars)
2 participants