Returning a value to another function
by Christian · in Torque Game Engine · 03/01/2007 (9:10 pm) · 11 replies
What is the best way to have access or update local variables from 1 function to another.
For example:
Function number1 will keep being run and updated, but if it always goes to firstai and starts it over, there will be many firstai functions running at the same time when I just need 1 running...but with the updated numbers. The %fish and %notfish can't be globals because there of other parts of this program. Is there a way to do this with something such as return or something I haven't mentioned? Thanks.
For example:
function number1(%obj, %tuna, %pizza)
{
%fish = %obj.food[%tuna];
%notfish = %obj.food[%pizza];
firstai(%fish, %notfish);
}
function firstai(%fish, %notfish)
{
//do a bunch of stuff and have functions going out from me, but eventually come back here with the updated numbers from %fish and %notfishFunction number1 will keep being run and updated, but if it always goes to firstai and starts it over, there will be many firstai functions running at the same time when I just need 1 running...but with the updated numbers. The %fish and %notfish can't be globals because there of other parts of this program. Is there a way to do this with something such as return or something I haven't mentioned? Thanks.
#2
03/01/2007 (9:29 pm)
Yes thank you.
#3
Is that proper? Thank You
03/01/2007 (9:56 pm)
A follow up to this. For a large amount of things to returnfunction blablah()
{
%bot.aggro[%x] = %enemy;
%bot.amount[%x] += %damage;
return %bot.amount[0];
return %bot.aggro[0];
return %bot.amount[1];
return %bot.aggro[1]; //etc all through em all?
}%botaggro0 = blablah();
Is that proper? Thank You
#4
You can try to do something like this
Aun.
03/01/2007 (10:19 pm)
I don't think that will be possible You can try to do something like this
function blablah(){
%bot.aggro[%x] = %enemy;
%bot.amount[%x] += %damage;
return %bot.amount[0] SPC %bot.aggro[0] SPC %bot.amount[1] SPC %bot.aggro[1];
}%bot = blablah(); %a = getWord(%bot , 0 ); %b = getWord(%bot , 1 ); %c = getWord(%bot , 2 ); %d = getWord(%bot , 3 );
Aun.
#5
function1 will be constantly updated from another function.
The problem is that function2 will not have %data1, %data2, %data3 being constantly updated into it. So it won't be able to return it.
Is there a way to reach in and grab function1's result somehow?
If I had in function1
than function2 would be constantly be going over and over. Thanks again.
03/02/2007 (11:49 am)
Thank you both for the replies. I'm not quite sure how to implement this into what my program needs to do. It basically hasfunction function1(%randomAI, %data1, %data2, %data3)
{
//calculations here
%randomAI.eat[%x] = %newdata;
%TransferMe = %randomAI.eat[%x];
return %TransferMe;
}function1 will be constantly updated from another function.
function function2(%randomAI)
{
%TransferMe = function1();
}The problem is that function2 will not have %data1, %data2, %data3 being constantly updated into it. So it won't be able to return it.
Is there a way to reach in and grab function1's result somehow?
If I had in function1
function2(%randomAI, %data1, %data2, %data3);
than function2 would be constantly be going over and over. Thanks again.
#6
Note that you are currently giving demo examples of code that is completely in the global namespace (global functions instead of namespace methods) and that isn't necessarily best practice.
03/02/2007 (12:05 pm)
Dynamic fields on objects are very useful for this type of thing--either the primary object itself, or a ScriptObject that can be used to store a bunch of information.Note that you are currently giving demo examples of code that is completely in the global namespace (global functions instead of namespace methods) and that isn't necessarily best practice.
#7
03/03/2007 (8:46 am)
How would you go about setting up namespace methods for something like this?
#8
In general, when you are dealing with objects, and fields within objects, you want to be doing namespace functions.
In TGE, most namespaces are derived either from the underlying c++ classes of your objects, or a datablock name if your object uses datablocks.
You haven't shown where you are instantiating any of your objects, so we have no way of knowing what underlying class you are using. It would be best if you describe more of what your overall goal is here, and what types of objects you are instantiating, or especially look and study how stock starter kits deal with objects and namespaces.
03/03/2007 (12:51 pm)
Well, you haven't really discussed what your total problem statement is, so it's hard to say!In general, when you are dealing with objects, and fields within objects, you want to be doing namespace functions.
In TGE, most namespaces are derived either from the underlying c++ classes of your objects, or a datablock name if your object uses datablocks.
You haven't shown where you are instantiating any of your objects, so we have no way of knowing what underlying class you are using. It would be best if you describe more of what your overall goal is here, and what types of objects you are instantiating, or especially look and study how stock starter kits deal with objects and namespaces.
#9
03/04/2007 (1:13 am)
The objects that need to have the class members/hold data to them are players. Just the armor class I believe is correct. The specifics of what I need the players to store (each) is 10 strings (other players) and 10 numbers. I've been searching and working all week and haven't found a good resource on this. I origionally wanted to store them as an array, but I don't think that is possible anymore as class members. Basically my question would be than how would I make it so that I can have each player store something in the same manner of %player.setnameshape("John");/.getnameshape(). So I can access them from anywhere in the game such as that? I believe I can figure out the rest if I can do one. Thanks.
#10
It's as simple as created the dynamic fields on the object handle, such as:
%obj.value1 = "Greg";
%obj.value2 = 12;
etc.
Anywhere that you have that object id, you can access the dynamic fields you've created:
%myLocalVariable = %obj.value1;
echo(%myLocaVariable);
-->Greg
03/04/2007 (1:40 am)
You are making things too difficult on yourself!It's as simple as created the dynamic fields on the object handle, such as:
%obj.value1 = "Greg";
%obj.value2 = 12;
etc.
Anywhere that you have that object id, you can access the dynamic fields you've created:
%myLocalVariable = %obj.value1;
echo(%myLocaVariable);
-->Greg
#11
03/04/2007 (11:27 am)
Haha, I was celebrating like the Vikings just won when I read this. I can't believe it's that simple. Thank you!
Torque Owner Tim Heldna
Here's a basic example of a return statement:
function getFishType() { %fish = smelly; return %fish; }Now you can use this to retrive the value of %fish: