Forum

> > Stranded II > Scripts > The command "free" doesn't work with variables
Forums overviewStranded II overview Scripts overviewLog in to reply

English The command "free" doesn't work with variables

19 replies
To the start Previous 1 Next To the start

old The command "free" doesn't work with variables

xjonx
User Off Offline

Quote
Okay, so I have created a menu that can create a unit using selectplace. The unit is there permanently. So, I have created a script that; upon pressing E on it, it will get the units current class and id, and remove it using free. This script is added to the created unit using addscript and a text container. Now I know that the create command is considered a return command, but free isn't. Every time I use free with variables, it always says "Expecting semicolon" even though there IS a semicolon! I know I have typed the free EXACTLY correct (including the syntax)! Help me!
Acording to the command reference: Free "Class", ID [, Count]
(I know that you don't put brackets, the brackets are there to tell that part of the syntax is OPTIONAL)
Script >

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
You can remove SELF from those 4 lines...and your missing a comma in the 3rd one: $zofobj = getz ("self" self); should be $zofobj = getz ("self",self);

1
2
3
4
5
6
7
8
9
10
11
12
on:use {
     $xofobj = getx ("self", self);          //remove self
     $yofobj = gety ("self", self);          //remove self
     $zofobj = getz ("self" self);           //remove self & missing comma
     $yawofobj = getyaw ("self", self);  //remove self
     $classofobj = currentclass();
     $idofobj = currentid();
     $nameofobj = name ($classofobj, $idofobj);
     play "barkbranchfire.wav";
     msg "An $nameofobj ($classofobj) with an id of $idofobj and yaw rotation of $yawofobj has been removed at x: $xofobj, y: $yofobj, z: $zofobj", 1, 6000;
     free $classofobj, $idofobj, 1;
}

I believe the missing comma is causing the current error, but even when you fix it you will likely get another error for the extra SELF I am suggesting is incorrect syntax...I dont do it that way so I dont know it is an error. I use simply ("self")

1
2
3
4
5
6
7
8
9
10
11
12
on:use {
     $xofobj = getx ("self");
     $yofobj = gety ("self");
     $zofobj = getz ("self");
     $yawofobj = getyaw ("self");
     $classofobj = currentclass();
     $idofobj = currentid();
     $nameofobj = name ($classofobj, $idofobj);
     play "barkbranchfire.wav";
     msg "An $nameofobj ($classofobj) with an id of $idofobj and yaw rotation of $yawofobj has been removed at x: $xofobj, y: $yofobj, z: $zofobj", 1, 6000;
     free $classofobj, $idofobj, 1;
}

Actually this line too:
free $classofobj, $idofobj, 1;

Can simply be:
free "self";

In this case freeing "self" nullifies the count 1, so count it is not needed...because your only deleting the unit you chose to "use" the E button on...thus it's count 1 by default.
edited 2×, last 01.02.17 03:28:53 am

old Re: The command "free" doesn't work with variables

xjonx
User Off Offline

Quote
@user JasJack67:
It works on the first unit, but when spawning an second one and using it, it gives me a type out if range error. And thanks telling me about when to use self, it really helped.

Edit:
Turns out the variable that stores the name doesn't store the name and defaults to 0. And, if its any help, here is the selectplace script:
script >
edited 1×, last 01.02.17 04:47:32 am

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
Quote
Turns out the variable that stores the name doesn't store the name and defaults to 0


missing " "
$nameofobj = name ($classofobj, $idofobj);

should be:
$nameofobj = name ("$classofobj", $idofobj);


Where do you have defined: $selchoose, $settype, and $setyaw?

Line 10 you are adding the text from info 5 where info 5 is a sprite? and 5 should be "5"
addscript "$type", $id2, 5;

should be:
addscript "$type", $id2, "5";


Line 11 "id2" should be id2
freeze "$id2", 1;

should be:
freeze $id2, 1;


After you fix the small errors, if it does not work try to comment out line 11 addscript using // and see if it works without it...cuz there might be some error with using ID 5 sprite...I generally use a file path here to add a script/text.

edit Line 7 missing " "
1
$id2 = create ($type, $placement, $placex, $placez);
should be:
1
$id2 = create ("$type", $placement, $placex, $placez);

edit Line 8 missing 2 " "
setrot "$type", $id2, self, $placeyaw, self;

should be:
setrot "$type", $id2, "self", $placeyaw, "self";


self should always be "self" far as 'Im aware.
edited 9×, last 01.02.17 02:16:45 pm

old Re: The command "free" doesn't work with variables

xjonx
User Off Offline

Quote
@user JasJack67:
Still, I get a type out of range error upon removing a unit a second time.
I have a dialogue (appears on attack2 event) that is called at an info sprite. The script I'm giving you is PART of the dialogue since the other parts don't interfere with these scripts.
menu were you can choose the unit to spawn >

startspawn page >

info sprite 5 is at the OP^^ but i'll repost the fixed version.
info sprite 5 >

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
@info sprite 5 you did it again, lol

Line 11 missing " "
free self;

should be:
free "self";


But also I do not get how you did your buttons for the menu...here is an example of how I do mine. The scripts are executed underneath at the PAGE event under script=start and script=end.

I can not say for sure there is errors or not in the way you did your button scripts.

In this example from my mod the player is viewing a planting menu. Notice how the player is choosing to plant 1 of 3 bushes. If he clicks button 1 then the page=bush1 is executed, and that is where the script runs. If he clickes button 2 then page=bush2 runs etc.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
page=bush
title=Planting Bush
text=start
!4I have chosen to plant a bush.
!4What bush shall I plant?

!5Other fruiting bushes can be
!5grown by planting it's fruit.

!5Such as, Hemp, Tomato, Potato,
!5Corn, Berry, Grape, Grain, and
!5Flowers.
text=end
ibutton=gfx\icons\baldbush01.bmp, bush1, Bald Bush
ibutton=gfx\icons\bush04.bmp, bush2, Variegated Bush
ibutton=gfx\icons\bush11.bmp, bush3, Denseleaf Bush

page=bush1
script=start
$x=use_x();
$y=use_y();
$z=use_z();
freestored "unit",1,15,1;
$typ=22;
$id=create("object",$typ,$x,$z);
$tmp=growtime($typ);
$tmp=(-$tmp);
$tmp++;
spawntimer $id,$tmp;
if (skillvalue("plant")>=200){
         process "planting bald bush",300;
}else{
         process "planting bald bush",3000;
}
play "dig.wav";
play "mat_leaf1.wav";
event "iskill_plant","global";
closemenu;
script=end

page=bush2
script=start
$x=use_x();
$y=use_y();
$z=use_z();
freestored "unit",1,15,1;
$typ=26;
$id=create("object",$typ,$x,$z);
$tmp=growtime($typ);
$tmp=(-$tmp);
$tmp++;
spawntimer $id,$tmp;
if (skillvalue("plant")>=200){
         process "planting variegated bush",300;
}else{
         process "planting variegated bush",3000;
}
play "dig.wav";
play "mat_leaf1.wav";
event "iskill_plant","global";
closemenu;
script=end

page=bush3
script=start
$x=use_x();
$y=use_y();
$z=use_z();
freestored "unit",1,15,1;
$typ=55;
$id=create("object",$typ,$x,$z);
$tmp=growtime($typ);
$tmp=(-$tmp);
$tmp++;
spawntimer $id,$tmp;
if (skillvalue("plant")>=200){
         process "planting denseleaf bush",300;
}else{
         process "planting denseleaf bush",3000;
}
play "dig.wav";
play "mat_leaf1.wav";
event "iskill_plant","global";
closemenu;
script=end

Yours above seems odd that your scripts are actually in the ibutton line (i didn't know if you could do that or not)...but I can not say for sure if that is the problem or not you will have to test it after you fix "self", or change your format like mine is in this example. After the gfx\icons\name.bmp, is the name of the page that will be executed when the button is clicked...bush1, bush2, and bush3.

Lastly, is it possible you meant to use icons as button images, and they are in a icon folder? That would mean the gfx\name.bmp should be gfx\icons\name.bmp (going out on a limb here...)

edit:___________________________________
Here is the translated command info: Notice the bold I highlighted.

Dialogue "Home", "Source"
Categories: interface
Sorry, not available in english!
Opens a window with dialog text and buttons. The start page for the dialog is specified with Start. A dialog definition is specified via Source. This can be either an external file (relative to the mod folder) or the ID of an info in which the dialog definition was written.

Dialog definitions
Dialog definitions consist of the following elements:
Page = value - top of page with page description (not visible)
Title = value - title of the page (displayed in the game)
Text = start / text = end - between these lines the text of the page is defined
Script = start / script = end - between these lines a script, which is executed when the page is called, can be defined
Button = page, text - creates a button, which calls a certain page when you click. A maximum of 10 buttons per page can be created
Ibutton = Icon, page, text - a button where you can specify an icon as a frame (number) or file (path relative to the mod folder)


As the page for buttons you can also specify the following:
Action: close - the dialog closes when you click.
Script: xyz - the xyz script is executed when you click. It can contain NO comma since everything is displayed as the label for the button from the first comma.
Event: xyz - the event xyz is executed everywhere (= global).

No comma?
edited 3×, last 02.02.17 04:35:58 am

old Re: The command "free" doesn't work with variables

xjonx
User Off Offline

Quote
@user JasJack67:
Still get a type out of range error. Also, I would have to create 9 x 6 pages, each for every button. Yes, putting scripts inside ibuttons 2nd comma actually works, but you can't put commas in the script (or it will intercept the part of the script after the comma as button text) so it's only useful for changing variables and starting events. The global map script checks for the event on my map. Also, I have no icons folder in my gfx folder, icons like prof.bmp and stuff are all in the gfx folder. There are some icons for game menus in sys/gfx/, but that's all I have of gfx.

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
Well thats good to know man thanks.

The only other thing I can come up with is your lines you declare a variable as a "text" like:

$yawtext = "0 Dagrees Yaw";


I dont think this is right...you should have:

$yawtext = 0;


and then in your message:

msg "$yawtext Dagrees Yaw";


You have several of these variable="text" that I think do not work:
$choosetext="Orange Butterfly";

$settype="unit";

Can not compute!

and Dagrees is spelled Degrees
edited 1×, last 02.02.17 05:33:32 am

old Re: The command "free" doesn't work with variables

xjonx
User Off Offline

Quote
@user JasJack67:
$choosetext is what is displayed on the menu so you know what the current unit that is going to spawn. The variable doesn't affect the use script at all, it is just for displaying text. $settype is for the create in on:selectplace, the other, $idofobj, is set to update to the current unit used on. If I didn't, for example: I select a lion unit from the menu, spawn it. Now that on use it sees what $settype and removes it. Which works the first time, however, if we were to spawn a lion then select soldier from the menu BUT close the menu and then use the lion, the script will now look for a soldier to remove and will give an error. Also putting text for variables doesn't cause any problems (besides only being useful in msg) and as long as there are quotes around the text, they value will be set to text. Another thing is that I can't use msg, as the menu will overlap the message in chat. I also have a feeling that currentid() and currentclass() are adding on to the variable rather than replacing on each use since it works removing the unit on use, but the second time it gives me a type out of range.
edited 1×, last 02.02.17 06:36:58 am

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
So on:select you are using one of those variables that you assigned a "text" to. I dont think you can do that right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
on:selectplace {
     $placex = selectplace_x();
     $placez = selectplace_z();
     $placement = $selchoose;
     $type = $settype;            //this $settype is that text variable from your page=spawnunit
     $placeyaw = $setyaw;
     $id2 = create ($type, $placement, $placex, $placez);
     setrot "$type", $id2, self, $placeyaw, self;
     addstate "$type", $id2, 17;
     addscript "$type", $id2, 5;
     freeze $id2, 1;
     play "magic.wav";
     closemenu;
}

You may have to replace that $type variable you declared as $settype="unit"? It should be returning a number instead of text "unit" i think.

1
$type = currentclass();

currentclass() returns the class number.

If that doesn't make sense then you might have to use the IF/ELSE in this script to identify the current Class you are looking for. if($type=this){do this}else{do this} I am simply not sure where your calling the event from... the game.inf global or from the player/unit,1 or whatever. currentclass() would be referring to the current thing the script is attached to ofcoarse.
edited 4×, last 02.02.17 03:07:05 pm

old Re: The command "free" doesn't work with variables

xjonx
User Off Offline

Quote
@user JasJack67:
settype is the menu's variable for the global script to refer to. Remember, this is the selectplace script, so currentclass would get the map's class (wtf, how would that work?). The seltype variable is for the MENU ONLY and $type if for the CREATE command only. classofobj is for the FREE command only. Also "free "class (ie: "unit/object/item/info")", ID, amount. The class is set to text AS IT SHOULD HAVE TEXT. If else won't work, since there are over 45 (I think) different units! Also, the create is at global map script with selectplace event that is started from the menu, the menu is opened via right click by attaching a dialogue start script to the player (unit ID 1). The delete command is attached to the unit when created (this is so the script can work independently for every unit, ie there would be raptor you just spawned, then you spawned a lion, then you pressed e on one of them so it would independently check the specific unit's id and class you used on. Also, units aren' the only thing I plan to spawn, I plan to add menu's for objects and items, so that's why I'm making global universal variables and independent variables.

Edit: I'm an idiot, I forgot to set the variable updated on each unit to be local. I get back to you ya to see if that works. ALSO, I have left click to spawn a rock, and the free command, again, doesn't work with variables.
This script creates and removes the rock >


Edit 2:
Nope, setting them to local still gave me the type out of range error on second use.
Quote >
edited 5×, last 03.02.17 09:13:49 am

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
hmmm...ill keep looking...there is still something wrong there I think. Also Id2=create is missing " " around $type and no space between create & (.

1
$id2 = create("$type", $placement, $placex, $placez);

Those little things like missing " " will throw errors ofcoarse.

Maybe too, when you create the first unit you could use on:create to change the appropriate variables for the second unit you say you get the error for? Know what I mean?

you could use getlocal & setlocal if you made them all local variables as you stated...even if they are global you may need to set them for the next unit to be spawned, at that moment it's created? idk.
edited 3×, last 03.02.17 02:24:24 pm

old Re: The command "free" doesn't work with variables

Hurri04
Super User Off Offline

Quote
user xjonx has written
1
free "object", $id, $penamount;     //results in a "expecting semicolon" error

here's a hint:
in the s2 cmd free command reference it says:
Quote
If the command is supposed to relate to an item, the parameter Count can determine how many items shall be deleted - otherwise, all of them will be deleted.

also notice how only items can stack (both in the inventory and when placing them in the world) but objects can not.

so since you are not trying to delete an "item" the [count] parameter is not optional. for "object" it can not be used at all.

you're getting the "expecting semicolon" error because that's exactly what the command expects after the second parameter.

old Re: The command "free" doesn't work with variables

xjonx
User Off Offline

Quote
@user JasJack67:
The type out of range error is fixed now but now on:create doesn't work, now all variables return as 0. So there is obviously something wrong with how I'm setting up the variables...

@user Hurri04:
Hmmm... interesting, I'll make sure to pay attention when reading the command reference. This doesn't work, as I have created like 6 objects with the return id: $id, and the free command has set to look for an object with id of: $id. It doesn't remove all of them. Also, what if I wanted to delete a certain amount of objects in the future? How would I go about that?

VARIABLE SET UP >

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
Your errors for missing " " are killing me.

line4:
local "$zofobj = getz ("self");

after ("self")
local "$zofobj = getz ("self")";


lol, hope that's the final fix!

I never set variables up that way...I do this:

1
2
3
4
5
6
7
8
9
10
on:create {
     local $xofobj,$yofobj,$zofobj,$yawofobj,$classofobj,$idofobj,$nameofobj;
     $xofobj = getx ("self");
     $yofobj = gety ("self");
     $zofobj = getz ("self");
     $yawofobj = getyaw ("self");
     $classofobj = currentclass();
     $idofobj = currentid();
     $nameofobj = name ("$classofobj", $idofobj);
}

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
hey sorry for the delay in getting back to ya. Listen, I used the name command in my spoilage script. I found a bug in the command that returns a "0" for the name, for some of the items I was checking. I had to do a work around and actually find the items that was returning a "0" and use it's actual name in the "msg".

take a look...where you see if($tmp==119) is a worm that spoils...it returns a "0" for it's name so I had to make the "msg" actually say "worm". Then the others it checks that return "0"...the last one works for the rest of the 100's of random items it assigns to $tmp...they return the name like it is intended.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
on:spoilage4 {
	local $tmp,$tmp0,$tmp1,$tmp2,$id;
	$id=currentid();
	$tmp0=random(1,15);
	if($tmp0==1){
		$tmp=random(254,279);
	}elseif($tmp0==2){
		$tmp=random(9,16);
	}elseif($tmp0==3){
		$tmp=random(31,46);
	}elseif($tmp0<=5){
		$tmp=random(66,99);
	}elseif($tmp0<=7){
		$tmp=random(115,126);
	}elseif($tmp0==8){
		$tmp=random(3700,3710);
	}elseif($tmp0==9){
		$tmp=random(3610,3630);
	}elseif($tmp0<=11){
		$tmp=random(4873,4892);
	}elseif($tmp0==12){
		$tmp=random(4902,4909);
	}else{
		$tmp=random(4958,4995);
	}
	if($tmp==119){
		if(count_stored("unit",1,119)>=1){
			msg "Worm",1,9000;
			msg "...has spoiled!",3,9150;
			freestored "unit",1,119,1;
			$id=create("item",4888,0,0);
			store $id,"unit",1;
		}
	}elseif($tmp==256){
		if(count_stored("unit",1,256)>=1){
			msg "Corn Shuck",1,9000;
			msg "...has spoiled!",3,9150;
			freestored "unit",1,256,1;
			$id=create("item",4888,0,0);
			store $id,"unit",1;
		}
	}elseif($tmp==257){
		if(count_stored("unit",1,257)>=1){
			msg "Corn Cob",1,9000;
			msg "...has spoiled!",3,9150;
			freestored "unit",1,257,1;
			$id=create("item",4888,0,0);
			store $id,"unit",1;
		}
	}elseif($tmp==3618){
		if(count_stored("unit",1,3618)>=1){
			msg "Chicken Sandwich",1,9000;
			msg "...has spoiled!",3,9150;
			freestored "unit",1,3618,1;
			$id=create("item",4888,0,0);
			store $id,"unit",1;
		}
	}elseif($tmp==3619){
		if(count_stored("unit",1,3619)>=1){
			msg "Fish Sandwich",1,9000;
			msg "...has spoiled!",3,9150;
			freestored "unit",1,3619,1;
			$id=create("item",4888,0,0);
			store $id,"unit",1;
		}
	}elseif($tmp==278){
		if(count_stored("unit",1,278)>=1){
			msg "Fertile Cornseed",1,9000;
			msg "...has spoiled!",3,9150;
			freestored "unit",1,278,1;
			$id=create("item",4888,0,0);
			store $id,"unit",1;
		}
	}elseif(count_stored("unit",1,$tmp)>=1){
		$tmp1=getstored("unit",1,$tmp);
		if(compare_material("item",$tmp1,"none")==1){
			$tmp2=name("item",$tmp);
			msg "$tmp2",1,9000;
			msg "...has spoiled!",3,9150;
			freestored "unit",1,$tmp,1;
			$id=create("item",4888,0,0);
			store $id,"unit",1;
		}
	}
}
notice at the bottom $tmp2 equals the "name" and it returns the name for all but 6 items that give a "0".

msg "$tmp2",1,9000;

6 out of 100's of items returned a "0" so maybe there is the same issue for "units" I do not know for sure as I did not test them., or have a script try to return the "name" of a unit.

edit p.s. I do not put spaces between a command and the first ( like you do, and I do not know if that is a syntax error...for example you have name ( and I have name(...or you have getx ( and I use getx( ...I think I found errors doing that before with some commands...putting a space where it doesn't belong.
edited 3×, last 11.02.17 08:24:39 pm

old Re: The command "free" doesn't work with variables

xjonx
User Off Offline

Quote
@user JasJack67:
Actually I did the no space behind change, it didnt make a difference. Another thing is, this wont work as the object or unit you are using is a wild card, meaning that it could be a lot of thing meaning VERY complex variable management. Soon I upload a video of my map and its scripts and pm you the map file and the external files it uses. Thanks and please be patient.

Actually, its not only the name (also thanks for telling me that bug) but its the id, class and everything else!

I hope you understand,
John Nathaniel

old Re: The command "free" doesn't work with variables

Hurri04
Super User Off Offline

Quote
@user JasJack67: the problem you describe is one I encountered myself once.
it turns out that you can't have the substrings "and" or "or" in the values of variables that represent strings. this is because the interpreter sees those as the "&&" and "||" operators.

so when you use
$tmp2=name("item",$tmp);
where the s2 cmd name command tries to return "worm", the = operator tries to assign the $tmp2 variable with "w||m". the result of this operation can only be 0 or 1. and unless "w" and "m" are interpreted as variables without a "$" in front, that leaves only 0.

so your workaround seems like the only possible solution (that I'm aware of) to me as well. although it should be possible to remove some duplicate lines from your exception handling there.

as for the spaces: those are (apart from inside strings of course) completely optional since they are ignored by the interpreter. my personal preference is to have spaces around operators ("&&", "||", ">", "<", "==", "=", etc.), spaces between ") {" and spaces after "," for better readability.

old Re: The command "free" doesn't work with variables

JasJack67
Super User Off Offline

Quote
John,
No problem, and I will certainly take a look at it when you send it. You also be patient, most times it's worth the wait to have it work as you intend.

Hurry,
Holy cow! I see it! Thank you for pointing out the "and" "or" in my item names...it just never dawned on me to see the similarities in the names. I am so happy you are here in this community, now I can go change 6 item names and shorten that script by 56 lines! This will also result in reducing feeling a lag spike when the on:spoilage4 is called cuz it had to check all those scattered ID's... then those 6 items... before it used the $tmp2/name.

If there is anything I can ever do to help you out (not really talking about script debug) let me know...you have helped me through so many script issues, and many not seen, as I have found answers in search that lead to some of your other posts.

Anyhoot! Thank YOu!
To the start Previous 1 Next To the start
Log in to reply Scripts overviewStranded II overviewForums overview