Forum

> > CS2D > Scripts > Object exists error
Forums overviewCS2D overview Scripts overviewLog in to reply

English Object exists error

30 replies
Page
To the start Previous 1 2 Next To the start

old Object exists error

Accurator
User Off Offline

Quote
I'm getting the following error: LUA ERROR (ai_update_living): bots/includes/build.lua:17: attempt to call a boolean value

Line 17:
1
if object(0,tilex,tiley) "exists"==true then

On the site it says for object: "exists: boolean, true if object with this ID exists, false otherwise"

So it's supposed to be a boolean, but the error says it isn't?
...I actually don't really know what a boolean is...

old Re: Object exists error

Infinite Rain
Reviewer Off Offline

Quote
@user Accurator:
He asked what is that suppose to mean... He didn't say that it was a solution. Can't you read?

Admin/mod comment

Can't you be at least slightly nicer to people? /Engi

old Re: Object exists error

Starkkz
Moderator Off Offline

Quote
Firstly, you're calling the function incorrectly. It's object(id, "exists"), if you want to know if there's a object in a certain position of the map, use cs2d lua cmd objectat.
1
2
3
if objectat(tilex, tiley) > 0 then
-- The object does exist.
end

Secondly, in your code you've set up a variable named "object", same as the CS2D function cs2d lua cmd object. If you make a variable, it will overwrite the function (It will be removed) since Lua functions work as variables and functions at the same type. (It's a pointer variable of type "function" that contains a chunk of code and can be called)

old Re: Object exists error

Accurator
User Off Offline

Quote
@user Infinite Rain: Didn't really get what it meant. Here's the code, if thats needed...

Code >


@user Starkkz: Where did I make the variable?

old Re: Object exists error

EngiN33R
Moderator Off Offline

Quote
user Starkkz has written
Secondly, in your code you've set up a variable named "object", same as the CS2D function cs2d lua cmd object. If you make a variable, it will overwrite the function (It will be removed) since Lua functions work as variables and functions at the same type. (It's a pointer variable of type "function" that contains a chunk of code and can be called)

Looking at his code (not pseudo at all), I don't think he did. I think the construction object(0, tilex, tiley) returns a boolean variable (probably false), and adding "exists" right after it is equivalent to calling it, hence the error.

1
2
object(0, tilex, tiley) "exists"
object(0, tilex, tiley)("exists")

Is the same principle as the require call:
1
2
require "module"
require("module")

@user Accurator: Just use
1
if objectat(tilex, tiley) > 0 then
instead of what you're using now. If you're already using it, I don't know why it doesn't work. It should.

old Re: Object exists error

Accurator
User Off Offline

Quote
It's working now, since it seems to be calling building_properties(id). However, now I get the following error:

LUA ERROR (ai_update_living): bots/includes/build.lua:180: attempt to compare number with nil

Line 180:
1
if maxhealth>=0 then

function building_properties(id) >

old Re: Object exists error

Starkkz
Moderator Off Offline

Quote
@user EngiN33R: I considered it a pseudo-code because you can find a conditional like this.
1
if ("repairable" == "1") then
That's a pointless conditional, why would you be comparing two constant strings that are completely different from each other? It's pseudo-code because he was trying to check if it was a repairable object, but if he doesn't setup a proper conditional then it's not going to work as he expects.

old Re: Object exists error

Accurator
User Off Offline

Quote
Tried some things, and I don't get any errors now. However, the bots still won't repair/upgrade. I think thats because of this: I edited the code so that if a building isn't type 1-15 the "maxhealth", "repairable" and "upgradable" strings (they are, right) are false (is this for booleans only?). If the string "maxhealth" is false, the "healthpercent" value wont be generated. I think the code ALWAYS considers them false...

code >

old Re: Object exists error

Starkkz
Moderator Off Offline

Quote
@user Accurator: What you did was comparing two different strings, that's different from comparing two boolean variables. Also, in Lua a boolean is not the same as 0 or 1, they're different types.

building_properties(id) >


In the previous code on which you were calling building_properties(id), just do:
1
local maxhealth, repairable, upgradeable = building_properties(id)
And when you compare those variables, don't use quotes. That will do the work.

old Re: Object exists error

Dousea
User Off Offline

Quote
@user Accurator: I don't get it. "maxhealth", "repairable", etc. are not strings. They are variables. It gonna cause an error if you are calling variable same as string. Also as user Starkkz said, 0 and 1 are not the original boolean at Lua. They are false and true.

At your building_properties function. You are calling cs2d lua cmd object function incorrectly. Are x and y arguments at the function exists? Seems no. This is the shortcut way to get object properties.
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
function building_properties (id)
	local maxhealth, healthpercent, repairable, upgradeable
	local properties = {
		{150, true, false},
		{300, false, false},
		{500, true, true},
		{1000, true, true},
		{2000, true, false},
		{1000, false, false},
		{1000, true, false},
		{500, true, true},
		{1000, true, true},
		{100, true, false},
		{750, true, true},
		{1000, true, false},
		{500, false, false},
		{500, false, false},
		{1500, true, false}
	}
	
	if (object (id, "type") > 0 and object (id, "type") < 16) then
		maxhealth = properties[object (id, "type")][1]
		repairable = properties[object (id, "type")][2]
		upgradeable = properties[object (id, "type")][3]
	end
	
	if (maxhealth > 0) then
		healthpercent = (maxhealth / 100)
	end
	
	return maxhealth, repairable, upgradeable, healthpercent
end

old Re: Object exists error

Accurator
User Off Offline

Quote
Bots still aren't repairing/upgrading.

Fixed the script a bit.
script >

old Re: Object exists error

Dousea
User Off Offline

Quote
As far as I remember, all building objects' max health are 100. You could replace your fai_build function with mine.
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
function fai_build (id)
	if (playerweapons (id, 74)) then
		ai_selectweapon (id, 74)
		
		local closeobject = closeobjects (player (id, "x"), player (id, "y"), 560)
		
		if (#closeobject > 0) then
			for key, self in pairs (closeobject) do
				if (ai_freeline (id, object (self, "x"), object (self, "y"))) then
					if (object (self, "health") < 100) then
						if (ai_goto (id, object (self, "x"), object (self, "y")) == 1) then
							ai_attack (id)
						end
					else
						table.remove (closeobject, key)
					end
				
				if (object (self, "health") >= 100) then
					if (object (self, "type") == 3 or object (self, "type") == 4 or object (self, "type") == 9 or object (self, "type") == 8 or object (self, "type") == 11) then
						ai_attack (id)
					else
						table.remove (closeobject, key)
					end
				end
			end
		else
			fai_buildstart (id)
		end
	end
end

old Re: Object exists error

Starkkz
Moderator Off Offline

Quote
@user Accurator: I realized that you are still calling object(id, tilex, tiley, "type"). That is not valid because the object function has only got two parameters: id and the type of the value you want to get. Ifyou want to know the id of a object in a certain position, do this:
1
2
local id = objectat(tilex, tiley)
local type = id > 0 and object(id, "type") or 0

old Re: Object exists error

Accurator
User Off Offline

Quote
@user Dousea: That would mean they have the same amount of HP as a player, making them get destroyed in 3 knife hits.

Now I'm getting the following error: LUA ERROR (ai_update_living): bots/includes/build.lua:55: attempt to perform arithmetic on global 'healthpercent' (a nil value)

Line 55:
1
if object(id,"health")<healthpercent*10 then

Code >

old Re: Object exists error

Starkkz
Moderator Off Offline

Quote
@user Accurator: On fai_build you're using the variable id for the player and the building, give it another name (maybe oid).

On building_properties(id) you are using object(id, "type") to compare variables but you already have the local variable type which does exactly the same. Use it.
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview