🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

About engine design

Started by
2 comments, last by WitchLord 16 years, 11 months ago
Hi, First, sorry for my english, I hope you can understand me :) I'm programming a little game engine and I want to use AngelScript to the script system. Let me tell what I want and the problems I found. - The game use entities, and one entity can have a script. Diferents entities can use the same script file. - It's impossible create and destroy entities from script. The script only use handles. I use: pScriptEngine->RegisterObjectType( "Entity", 4, asOBJ_PRIMITIVE ); pScriptEngine->RegisterObjectBehaviour( "Entity", asBEHAVE_ADDREF, void f()", asFUNCTION( Script_EmptyFunction ), asCALL_GENERIC ); pScriptEngine->RegisterObjectBehaviour( "Entity", asBEHAVE_RELEASE, "void f()", asFUNCTION( Script_EmptyFunction ), asCALL_GENERIC ); Script_EmptyFunction is a empty function, I not control de references, because the entities creates and destroys in C++. Is this correct? - My idea is that differents scripts files have differents asIScriptEngine, and the entities that use that script, have differents asIScriptContext. In the script files, in a OnInit function, I pass an argument with the handle to the entity that use that script: Entity@ m_pEntity; void OnInit( Entity@ pEntity ) { @m_pEntity = pEntity; } And in the OnUpdate function, call to the entity parent. void OnUpdate( float fDelta ) { m_pEntity->SetPos( 20, 30 ) //i.e. } Problems & questions: - The global variable is shared by all contexts in the same asIScripEngine. What's the solution? before every call to script function (i.e. OnUpdate) I must set the entity handle? //... iFuncID = m_pScriptEngine->GetFunctionIDByName( NULL, "SetHandle" ); m_pScriptContext->SetArgObject( 0, m_pEntity ); m_pScriptContext->Execute(); iFuncID = m_pScriptEngine->GetFunctionIDByName( NULL, "OnUpdate" ); m_pScriptContext->SetArgFloat( 0, fDelta ); m_pScriptContext->Execute(); - Reading the forums I saw a thread talking about the contexts, and said that when a context finish it must be destroyed. But if I execute the script every tick (OnUpdate) I must create and destroy a new context every tick? I think that's all :), thank you!! [Edited by - Malandrin on August 1, 2007 6:12:33 AM]
Advertisement
Quote:
Script_EmptyFunction is a empty function, I not control de references, because the entities creates and destroys in C++. Is this correct?


Yes, this is the recommended way to do it until I have improved AngelScript in this regard.

I recommend you use one script engine for all your scripts. Just use a different module name for each of them. You can even expose different application functions to different modules by using configuration groups.

Global variables are shared for all contexts, but global variables in different modules are different, even if they have the same name. Unless you want to share variable between contexts, you should avoid global variables. You could set the global variable before every call, but in my opinion it is not a very good design. Instead, I recommend you either pass the variable as a parameter to each function, or that you use script classes to store the variables that each entity should use.

Quote:
- Reading the forums I saw a thread talking about the contexts, and said that when a context finish it must be destroyed. But if I execute the script every tick (OnUpdate) I must create and destroy a new context every tick?


No, you don't need to release the context after every execution. You can store the context reference for the next time. You just need to reset it with a call to Prepare() each time you wish to call a function from the beginning. If you have functions that are suspended, you should resume them by calling the Execute() function directly without the Prepare() call.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Hi,

I have doubts about Prepare() function on ScriptContext. I have 7 entities playing an animation. If I don't use scripts, the framerate is 380fps. When I use scripts(7) the framerate down to 90fps. I call a OnUpdate() function in every script every tick, and OnUpdate() is empty (is a test function). The time goes in the Prepare() function, is this the normal behaviour? I try this m_pScriptContext->Prepare( asPREPARE_PREVIOUS ); and the result is the same. All this occur in debug mode, in release the framerate is 380.

Recapitulating, I am making something wrong and for that reason my framerate is so low? or this is a normal behaviour and I can be quiet? :)

Thank you.
If you get 380 fps in release mode while still calling the script you can safely assume you're calling the scripts correctly.

The Prepare method is quite heavy as it does quite a lot, including allocating memory. In debug mode it is even more so, because there are a lot of extra validations added by the compiler.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement