请问一下,未知变量是什么情况?
gamemaker吧
全部回复
仅看楼主
level 2
violetjasa 楼主

___________________________________________
ERROR in
action number 1
of Mouse Event for Left Pressed
for object bbuilding:
In script build_buildings:
Error in code at line 6:
if(country_player.resource>the_building.cost)
^
at position 43: Unknown variable cost
我在游戏中设定了建筑所需的资源为cost,当可以建造时从国家的总资源resource里扣除。但程序运行时报错
这是建造建筑的脚本,其中argument0是我传进来的参数
var id_build;
var can_place;
var the_building;
the_building=argument0;
can_place=0;
if(country_player.resource>the_building.cost)
{
if place_empty(mouse_x,mouse_y)
{
can_place=1;
}
else
{
//cout....cantplace
}
if (can_place=1)
{
id_build=instance_create(mouse_x,mouse_y,the_building);
id_build.owner=1;
country_player.resource=country_player.resource-the_building.cost;
}
}
这里是我设置的farm变量
下面是我设置的国家变量,但是这个在上面那一段代码就没有报错
2017年07月07日 08点07分 1
level 12
If(country_player.resource)>the_building.cost一句有误,the_building 是变量,后面点cost意义不明。改为大于cost。
2017年07月07日 09点07分 2
谢谢,我在使用the_building的时候应该就等同于使用farm,the_building.cost相当于farm.cost,这样应该没问题啊[疑问]
2017年07月07日 13点07分
level 2
violetjasa 楼主
@大小板栗仔,谢谢,但是还是报错
2017年07月07日 13点07分 3
level 12
好吧我看错了,原来的写法无误。检查一下argument0是否为farm?如果不是则会出现未知变量。(script_execute(build_building,farm))
2017年07月07日 14点07分 4
刚刚回头仔细地看了一下代码…发现没有写错的地方…会不会是参数写错了…我也是这么想的。我估计楼主直接填写的是对象名,而不是实例名,要知道…对象可不会初始化,要实例才会。
2017年07月07日 14点07分
level 11
[酷] 如果我没有猜错,你传参数那边写的一定是对象的名字,而不是实例的名字
对象是不会初始化事件的,而实例会。
所以你这个写法不行。要换一个。
2017年07月07日 14点07分 5
很好奇,既然你都会写脚本了,为什么不尝试一下学学点更好用的东西?你可以学一下配对(map),就是你可以给一个变量名设定一个值,然后当你输入那个变量名的时候返回的是那个变量名所储存的值
2017年07月07日 14点07分
是的,我传参数参数的时候写的是对象的名字。但是没有办法传实例的名字,因为这个对象的实例还没有创建(只有资源大于花费时才可以创建)。
2017年07月08日 00点07分
谢谢,我用了创建对象后索引的cost,就可以了,但是前面的if(country_player.resource>the_building.cost)还是报错[笑眼]
2017年07月08日 00点07分
level 11
我想,你可以这么写
##script name: buildmap_init;
globalvar buildmap;
buildmap=ds_map_create();
ds_map_add(buildmap,建筑的对象名,需要的资源数)
***以此类推
##script name: buildcost;
return ds_map_find_value(buildmap,argument0);
//其中argument0是建筑的对象名
另外,我不知道配对返回的是不是无论值是文本还是数字返回的都是文本还是会根据值的内容返回特定内容的类型的值。
就比如123,不知道他返回的是"123"还是123
2017年07月07日 14点07分 6
另外,我是直接在贴吧上面写的代码…不要复制粘贴,看看思路自己写一遍就好了。
2017年07月07日 14点07分
level 7
膜触
2017年07月11日 10点07分 8
1