Jump to content

Scripting/Triggering


Oger-Lord
 Share

Recommended Posts

Is there a tutorial for triggering and scripting?

And a list with commands?

I failed creating a mod which simply sends the message hello world to all players at the beginning of the game.

My Code:

function HelloWorld() {}

HelloWorld.prototype.Init = function()
{
var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);


for each (var ent in cmpPlayerManager.playerEntities){
var cmpPlayer = Engine.QueryInterface(ent, IID_Player);

var notification = {"player": cmpPlayer.GetPlayerID(),"message": "Hello World!"};
cmpGUIInterface.PushNotification(notification);
}

};

Link to comment
Share on other sites

Hi Lord,

We don't have general triggers up and running at the moment. We hope to add these soon.

You can take a look at the AI scripts and see how they send out messages at the start of the game and get an idea for what the scripting interfaces look like if you are interested.

I haven't done much script implementation work, so I don't know if there is any good documentation out there. I did a quick search and most of it looks pretty outdated. If you do some research and figure some things out please feel free to update the Wiki.

Let me know if you have any more specific questions and I will try to get you an answer.

Link to comment
Share on other sites

You can find some useful info about writing components here: How to write components

I failed creating a mod which simply sends the message hello world to all players at the beginning of the game.

This patch should do this:

HelloWorldComponent.patch


Index: source/simulation2/Simulation2.cpp
===================================================================
--- source/simulation2/Simulation2.cpp (revision 9803)
+++ source/simulation2/Simulation2.cpp (working copy)
@@ -105,6 +105,7 @@
LOAD_SCRIPTED_COMPONENT("GuiInterface");
LOAD_SCRIPTED_COMPONENT("PlayerManager");
LOAD_SCRIPTED_COMPONENT("Timer");
+ LOAD_SCRIPTED_COMPONENT("HelloWorld");

#undef LOAD_SCRIPTED_COMPONENT
}
Index: binaries/data/mods/public/simulation/components/interfaces/HelloWorld.js
===================================================================
--- binaries/data/mods/public/simulation/components/interfaces/HelloWorld.js (revision 0)
+++ binaries/data/mods/public/simulation/components/interfaces/HelloWorld.js (revision 0)
@@ -0,0 +1,2 @@
+Engine.RegisterInterface("HelloWorld");
+
Index: binaries/data/mods/public/simulation/components/HelloWorld.js
===================================================================
--- binaries/data/mods/public/simulation/components/HelloWorld.js (revision 0)
+++ binaries/data/mods/public/simulation/components/HelloWorld.js (revision 0)
@@ -0,0 +1,19 @@
+function HelloWorld() {}
+
+HelloWorld.prototype.Schema =
+ "<a:component type='system'/><empty/>";
+
+
+HelloWorld.prototype.Init = function()
+{
+ var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
+
+ for (var i = 0; i < 8; i++)
+ {
+ var notification = {"player": i+1, "message": "Hello player " + (i + 1)};
+ cmpGUIInterface.PushNotification(notification);
+ }
+}
+
+Engine.RegisterComponentType(IID_HelloWorld, "HelloWorld", HelloWorld);
+

Edit: And as Kenny said in case if you want to program AI it is probably better to start from learning existing AI code and bots, you can find it here: http://trac.wildfiregames.com/browser/ps/trunk/binaries/data/mods/public/simulation/ai

Edit 2: After discussion with our more experienced programmers I improved my patch:

HelloWorldComponent.patch


Index: source/simulation2/Simulation2.cpp
===================================================================
--- source/simulation2/Simulation2.cpp (revision 9803)
+++ source/simulation2/Simulation2.cpp (working copy)
@@ -105,6 +105,7 @@
LOAD_SCRIPTED_COMPONENT("GuiInterface");
LOAD_SCRIPTED_COMPONENT("PlayerManager");
LOAD_SCRIPTED_COMPONENT("Timer");
+ LOAD_SCRIPTED_COMPONENT("HelloWorld");

#undef LOAD_SCRIPTED_COMPONENT
}
Index: binaries/data/mods/public/simulation/components/interfaces/HelloWorld.js
===================================================================
--- binaries/data/mods/public/simulation/components/interfaces/HelloWorld.js (revision 0)
+++ binaries/data/mods/public/simulation/components/interfaces/HelloWorld.js (revision 0)
@@ -0,0 +1,2 @@
+Engine.RegisterInterface("HelloWorld");
+
Index: binaries/data/mods/public/simulation/components/HelloWorld.js
===================================================================
--- binaries/data/mods/public/simulation/components/HelloWorld.js (revision 0)
+++ binaries/data/mods/public/simulation/components/HelloWorld.js (revision 0)
@@ -0,0 +1,32 @@
+function HelloWorld() {}
+
+HelloWorld.prototype.Schema =
+ "<a:component type='system'/><empty/>";
+
+
+HelloWorld.prototype.Init = function() {
+ this.firstUpdate = true;
+};
+
+/**
+ * Perform logic after first update, i.e. when simulation was started
+ */
+HelloWorld.prototype.OnUpdate = function(msg) {
+ if (this.firstUpdate)
+ {
+ this.firstUpdate = false;
+
+ var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
+ var cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
+ // number of players without gaia (player 0)
+ var numPlayers = cmpPlayerManager.GetNumPlayers() - 1;
+
+ for (var i = 0; i < numPlayers; i++)
+ {
+ var notification = {"player": i+1, "message": "Hello player " + (i + 1)};
+ cmpGUIInterface.PushNotification(notification);
+ }
+ }
+};
+
+Engine.RegisterComponentType(IID_HelloWorld, "HelloWorld", HelloWorld);

Now it perform logic in more appropriate moment (when simulation started, so all data loaded). In particular this solves problem with actual number of players, because if you will try to get it in first version of patch, you will get 0, because PlayerManager component has 0 players after it's initialization (see PlayerManager.prototype.Init) and they added later (in Player.js:LoadPlayerSettings)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...