Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
How do I make script variable increase every time function is being executed?

dekon_17
Registered User
Quote
2021-09-13 10:48:28

I want to make the spawn system for my shooter game, because if I change something in parameters of one enemy, then I should do this with the parameters of every single copy of this one. To avoid that, I decided to make a little script that will spawn objects on spawn points. But I need to understand how do I make a number of spawn points grow... Ah, I am not good at explaining things, so, here's the code:
/*
<action jsname = "action_Spawn" description = "Spawn object in multiple locations">
<property name = "ObjectToSpawn" type = "scenenode"/>
<property name = "SpawnNodeName" type = "string"/>
</action>
*/

action_Spawn = function()
{
};

action_Spawn.prototype.execute = function()
{
var me = this;
this.registeredFunction = function() { me.Begin(); };
ccbRegisterOnFrameEvent(me.registeredFunction);
}

action_Spawn.prototype.Begin = function()
{
if (SpawnNum == null)
{
var SpawnNum = 0
var Obj = this.ObjectToSpawn;
};

var Num = ++SpawnNum; //I thought this will make it bigger every time the function is being used, but nah, it doesn't...
print (Num);

var Spawn = ccbGetSceneNodeFromName(this.SpawnNodeName + Num);
if (Spawn)
{
var Pos = ccbGetSceneNodeProperty(Spawn, "Position");
var CopyObj = ccbCloneSceneNode(Obj);
ccbSetSceneNodePositionWithoutCollision(CopyObj, Pos.x, Pos.y, Pos.z);
ccbSetSceneNodeProperty(CopyObj, "Visible", true);
print ("Spawned " + Num + " object");
}
else
{
ccbUnregisterOnFrameEvent(this.registeredFunction);
print ("No more spawn points, process done!");
};
};



sven
Registered User
Quote
2021-09-13 12:22:58

Easiest way to do it is:

/*
<action jsname = "action_Spawn" description = "Spawn object in multiple locations">
<property name = "ObjectToSpawn" type = "scenenode"/>
<property name = "SpawnNodeName" type = "string"/>
</action>
*/


SpawnNum = 0;

action_Spawn = function()
{
};

action_Spawn.prototype.execute = function()
{
var me = this;
this.registeredFunction = function() { me.Begin(); };
ccbRegisterOnFrameEvent(me.registeredFunction);
}

action_Spawn.prototype.Begin = function()
{

var Num = ++SpawnNum; //I thought this will make it bigger every time the function is being used, but nah, it doesn't...
print (Num);

var Spawn = ccbGetSceneNodeFromName(this.SpawnNodeName + Num);
if (Spawn)
{
var Pos = ccbGetSceneNodeProperty(Spawn, "Position");
var CopyObj = ccbCloneSceneNode(this.ObjectToSpawn);
ccbSetSceneNodePositionWithoutCollision(CopyObj, Pos.x, Pos.y, Pos.z);
ccbSetSceneNodeProperty(CopyObj, "Visible", true);
print ("Spawned " + Num + " object");
}
else
{
ccbUnregisterOnFrameEvent(this.registeredFunction);
print ("No more spawn points, process done!");
};
};



dekon_17
Registered User
Quote
2021-09-13 13:19:41

Damn, thanks a lot, sven! I am also surprised that this thing works pretty well, since most of my scripts usually don't work at all. Also, is the only difference is that it makes SpawnNum = 0 at the begining of script?


dekon_17
Registered User
Quote
2021-09-13 13:27:13

I've also kinda modified it to make spawn points invisible after spawning enemy! And also deleted a bunch of "print" stuff:
/*
<action jsname = "action_Spawn" description = "Spawn object in multiple locations">
<property name = "ObjectToSpawn" type = "scenenode"/>
<property name = "SpawnNodeName" type = "string"/>
</action>
*/


SpawnNum = 0;

action_Spawn = function()
{
};

action_Spawn.prototype.execute = function()
{
var me = this;
this.registeredFunction = function() { me.Begin(); };
ccbRegisterOnFrameEvent(me.registeredFunction);
}

action_Spawn.prototype.Begin = function()
{

var Num = ++SpawnNum;

var Spawn = ccbGetSceneNodeFromName(this.SpawnNodeName + Num);
if (Spawn)
{
var Pos = ccbGetSceneNodeProperty(Spawn, "Position");
var CopyObj = ccbCloneSceneNode(this.ObjectToSpawn);
ccbSetSceneNodePositionWithoutCollision(CopyObj, Pos.x, Pos.y, Pos.z);
ccbSetSceneNodeProperty(CopyObj, "Visible", true);
ccbSetSceneNodeProperty(Spawn, "Visible", false);
}
else
{
ccbUnregisterOnFrameEvent(this.registeredFunction);
};
};



sven
Registered User
Quote
2021-09-13 18:33:29

This script will show you how it -how variables works..

Just add it to project.
if mouseklick RIGHT then execute it..
klick2-3 times and read what it prints then you see how variables will change and how they work.

I dont know correct terms that i added in comments.. but i hope it helps to understand it.

/*
<action jsname = "action_example" description = "1111 action_example AAAA">
</action>
*/

//This variable will be Global SHARED variable you can access it by other script also and it will be defined once..
global_counter=0;

action_example = function()
{
this.counter=0;//This is Local variable will be setup as zero in every call
};

action_example.prototype.execute = function()
{
var me = this;
this.registeredFunction = function() { me.Begin(); };
ccbRegisterOnFrameEvent(me.registeredFunction);
}

action_example.prototype.Begin = function()
{



var variablea=0;//declare var variablea in loop
variablea+=1;//add one to variablea

this.counter +=1;// here i add 1 to local var until it ticks to 10



if (this.counter==10)//if its 10 then set +1 to global var and exit
{
global_counter +=1;//add one to global var
ccbUnregisterOnFrameEvent(this.registeredFunction);//after 10 end this loop

print("variablea " +variablea);//you see it will be always 1 because you will define it 10 times again and again from null
print("this.counter " +this.counter);//print info -you see it always restarts from 0 in every execute and ends up 10
print("global_counter " +global_counter);//print info -you see it ticks +1 in every execute and never goes to 0

}

};



DouweDabbe
Guest
Quote
2021-09-14 02:56:01

@ sven,

I believe that the:
global counter in your last script will only be defined and initialized when this particular action has been applied for at least one time.

the other actions and behaviour in this scene
that are supposed to know about this global variable and
are reading and or changing this globalcounter variable
will create an error if executed before
the global counter creating action has been applied at least once.

globalcounter is an action global var.
(best seen as only global for this action file)

real global vars better defined in mygamename.js

am i right ?


sven
Registered User
Quote
2021-09-14 08:02:34

DouweDabbe

That is correct.. i usually load some script on startup of game that consists all shared variables i need -usually i call script something INIT.js


DouweDabbe
Guest
Quote
2021-09-15 06:35:40

Ooh ???

you know how to call external scripts in coppercube?

can you show what statements / functions you use ?
to call init.js



this implies that we can also call scene specific external scripts

my intro and outtro scenes don't need path finding

thanks


just_in_case
Moderator
Quote
2021-09-15 07:37:59

You can simply call external scripts, by using the same name as the project name or by defining the script to be used as main script. You can also use commandline argument to specify the script, if you own studio edition.
There are other methods as well to define the script.


arik1
Registered User
Quote
2021-10-24 02:00:47

I guess I don't understand...What is the syntax for calling external scripts??? Can you give me an example in the form of an action or behavior??? And hilight the area(s) where the external scripts are called???


Guest
Guest
Quote
2021-10-24 15:56:52

@arik1

I was messing around with the idea of calling external stuff a couple weeks ago, and I eventually decided the idea is pointless because then your code is exposed and you end up with a bunch of loose files. Not sure how others have done it, but this is what I found in my notes:

function functionFromLine(src, line) {

var content = ccbReadFileContent(src);

var lineArray = content.split("\n");

var output = lineArray[line];

var F = new Function (output);

return(F());
}

functionFromLine("data.txt", 1);


All this does is define a function with two params: src and line, where src is the text file your script is in and the line is the line on which the script is written. The function reads the src file, splits it into an array based on lines ("\n"), and then defines F as a function based on the output of the read array.
When I invoke the function, my src is "data.txt" but you can call it data.js bc js is just a text file. Heck, you can call it data.football if you want.

Personally I don't think keeping data files outside of the exe is a good idea even if you write them at runtime to ensure they aren't tampered with. It just seems like extra work for little reward. You can just as easily copy your js file into a Before first drawing do something/execute js script and be done with it without an external file. I mean 90% of CC programming is function calls and variables ... or so it seems to me ... but I just code as a hobby.

Enough blathering. Hope this helped in some way.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Int?rnational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact