Game Development Community

Instantiating SimObjects

by Joseph Rioux · in iTorque 2D · 10/13/2012 (3:11 pm) · 1 replies

Alright, here is my test class, goobClass, defined in the file game/gameScripts/goob.cs. It has only one function, foo()

function goobClass::foo(%this)
{
echo("Hello, world!");
}



And here's my startGame function in game.cs

function startGame(%level)
{
exec("./goob.cs");

Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);

new ActionMap(moveMap);
moveMap.push();

//The part I'm wondering about
echo("ngoobClass will now say "hello, world"");
$Goob = new SimObject(){class = goobClass;};
$Goob.foo();

$enableDirectInput = true;
activateDirectInput();
enableJoystick();

sceneWindow2D.loadLevel(%level);
}



This is what I get in the console when I run the game:

goobClass will now say "hello, world"
game/gameScripts/game.cs (23): Unknown command foo.
Object (1316) SimObject



Does anyone know why this doesn't work? Thanks!








#1
10/15/2012 (6:49 pm)
The class property is just a string that is set based off of the object's actual class. Setting it yourself doesn't change the object's class. Instead, your instantiation should look like this:
$Goob = new SimObject(goobClass) {}

That said, classes are usually assigned within the editor rather than in code. What most people do is add sprites to their levels and then assign classes to them through the editor.

By the way, it's best to exec your script files in main.cs. There's a line in there where game.cs is exec'ed. Best practice is to exec your other script files after that.