Game Development Community

Calling aglobal function from another global function

by Nir Ziso · in Torque Game Builder · 09/04/2006 (5:17 am) · 7 replies

Call to a global function from another global function(solved)

I set up my global scene on the onlevelloan event likt this

function table::onLevelLoaded(%this)
{

if(!sceneWindow2D.getUseObjectMouseEvents())
{ sceneWindow2D.setUseObjectMouseEvents(true);}

$Myscene= %this;
.
.
.
.
}

I made afuction that create an object something like this:
function Drawobject()
{

%temp= new t2dStaticSprite()
{
scenegraph =$Myscene;
class="dummy";
config= "dummycards";
};

}

Now I have another global function and I try to call Drawobject()

function calldrawobject()
{
Drawobject();
}

I get into the function DrawObject but the object is not drawn on screen why?

#1
09/04/2006 (10:05 am)
Your onLevelLoaded should look like:

function table::onLevelLoaded(%this, %scenegraph)
{
    ...
    $Myscene= %scenegraph;
    ...
}
#2
09/04/2006 (10:42 am)
Table is my level i did it but its not work is there is another way to define aglobal scenegraph?
#3
09/04/2006 (12:12 pm)
@Nir, I think Ben is talking about this bit:

function table::onLevelLoaded(%this, [b]%scenegraph[/b])

[b]$Myscene[/b]= %scenegraph;

Pay attention to what is missing and the order... =P

The %scenegraph is being passed into the onLevelLoaded function. Then you need to store it to a global variable ($Myscene) so you can call it later. (Globals are denoted with a $ and local vars are denoted with a %).

FYI: There is no trick to calling a function from another function... you don't even have to worry about their ordering in the cs files. (Functions know about each other regardless of what order they are initially parsed in).

Here are some debugging tips for future reference:

First of all, check to see if the object is being created at all:

Add this to drawObject after the object is created:

echo("==============================");
echo("DrawObject() - Object Created: ", %temp);
echo("==============================");

Run the game and find the %temp id in the console then type the following into console:

echo(isObject(add the id here));

If it is an object has been created it will return 1 else 0.

If it HASN'T been created then there is something wrong with one of the object creation parameters. If it HAS been created then it is a visibility or positioning problem. This will tell you where to start looking for issues.

-Unk
#4
09/04/2006 (9:16 pm)
@Nir

Are you saying that your level scenegraph is called 'table' ?

If that is the case, then you don't even have to assign it to a global var, you can just reference it by that name.

function Drawobject()
{
    %temp= new t2dStaticSprite()
    {
        scenegraph = table;
    };
}

but still try Unk's suggestions to make sure you really understand what is actually happening and what isn't.
#5
09/04/2006 (11:40 pm)
Thanks a lot but I still have a problem when I call the function drawobject() from another function
That have a class and an instance of this class so the function work.
When I call this function from a global function without a class then it is not work

the object is created when i call him from the global function but i do not see him on screen
and it is not aproblem of position baecause from another function with aclass when i call the function
drawobject() i see the object on the screen why?

what i see is that the function show that $Myscene is not an object
#6
09/05/2006 (9:31 am)
Yes, I am suspecting that $MyScene is never getting initialized the way you think it is. Read my previous post. I'm not convinced that your onLevelLoaded is getting called on 'table'

Also, if your scenegraph is named table, then you don't need to create a $MyScene. Just use table directly.
#7
09/05/2006 (10:34 am)
I found the problem i call my function outside all the function and this function is call before onlevel load
thanks alot for your help