Not logged inOpenClonk Forum
Up Topic General / Help and Questions / Schedule
- - By Kandif [pl] Date 2012-12-18 19:20
var start = Random(2)+1;
if(start==1) var xb=82, yb=50;
if(start==2) var xb=244, yb=50;
Schedule(nil,Format("CreateObject(MyNameObject, xb, yb, NO_OWNER)"), 7,0);
//Schedule(nil,"CreateObject(MyNameObject, xb, yb, NO_OWNER)", 7,0); also tried this

It doesn't work. How to fix it ?
Parent - By Zapper [de] Date 2012-12-18 20:25
You cannot (at least should not) declare variables inside a condition and use them outside, see this example:
if(false) var x = 1;
Log("%d", x);

x will never be declared here, what would happen?

If you want to do it like that, you will have to do:
var x, y;
if(foo) { x = 82; y = 50; }
..


What did the error say, btw? :)
- - By Kandif [pl] Date 2012-12-18 20:30
I did not have error.
It don't be create. =.=

var start = Random(2)+1;
if(start==1) var xb=82, yb=50;
if(start==2) var xb=244, yb=50;
CreateObject(Ball, xb, yb, NO_OWNER)

/\
it work, but i want to create it after time.
Parent - By Caesar [de] Date 2012-12-18 20:49 Edited 2012-12-18 20:56
Ah, so you didn't understand that line in #openclonk. I'll elaborate:

If you Schedule something, all the information available at the time of command execution is the object context to execute it in (nil in your case) and the command string. That string contains, for example, "xb". xb however, can only be understood by C4Script and turned from the name into a value at the time of the function execution. Which means: You will have to make the string contain the actual value, not the name of the variable. (Side note: Schedule may not stay that way forever, that's a more advanced topic though.)

How you do that: Format.
For example: Format("SetWealth(0,%d)", w) evaluates to "SetWealth(0,100)", given w was 100.
In your case that would be: Schedule(nil,Format("CreateObject(%i, %d, %d, NO_OWNER)", MyNameObject, xb, yb), 7,0);
Parent - By Zapper [de] Date 2012-12-18 20:52
Ah, I get what you mean now.

The stuff in Schedule does not know anything about the other variables. You will have to put it directly into the string:
For example:
var x = 300; Schedule("Explode(x)",..) <- doesnt work
Schedule("Explode(300)", ..); <- works
Schedule(Format("Explode(%d)", x), ..); <- works
Up Topic General / Help and Questions / Schedule

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill