Not working url (SOLVED)

I have 2 go, both are created through a factory and in a certain part of the code just something happens. I just want to get the url of one of the objects through the code that is contained in these objects and get this both objects, although logically there should be a little different (… instance0 # canCol and … instance1 # canCol)

url: [cannonSCene:/instance0#canCol]

code:

local myCol
myCol = msg.url()
myCol.fragment = "canCol"
print(myCol)

Do you get an error? If so which one? I don’t see anything wrong with the code you shared. You need to show a bit more.

Hi, I’m sorry I didn’t answer for so long. The console does not throw errors, I cannot show the code in full due to some bug fixes. In fact, this is the only problem. I can throw you the current code, but it uses some kind of crutch. As I understand it, the problem is that the engine considers some objects to be 1. By calling the msg.url (".") Function on both objects, we get the same thing (ending instance0), although it should be (instance0 and instance1) :grinning:

local isActivate = false;
local rot;
local maxRot;
local minRot;
local fire = false;
local canFire = true
local plr;

local force = 1500

function init(self)
	msg.post(".", "acquire_input_focus")
	rot = go.get_rotation();
	maxRot = rot.z+45
	minRot = rot.z-45
end

function update(self, dt)
	
	if(isActivate)then 
		
		if(-math.deg(go.get_rotation().z) <= -21.9)then
			go.cancel_animations(".", "rotation")
			go.animate(".", "euler.z", go.PLAYBACK_ONCE_FORWARD, minRot, go.EASING_LINEAR, 2)
		elseif(math.deg(go.get_rotation().z) <= -21.9)then
			go.cancel_animations(".", "rotation")
			go.animate(".", "euler.z", go.PLAYBACK_ONCE_FORWARD, maxRot, go.EASING_LINEAR, 2)
		end
		if(fire and canFire)then
			local angle = go.get_rotation().z*2

			local _pos = vmath.vector3(go.get_position().x+50*math.cos(angle)*force,go.get_position().y+50*math.sin(angle)*force,0)
			--msg.post("@render:", "draw_line", { start_point = go.get_position(), end_point = _pos, color = vmath.vector4(1,0,0,1) })
			
			local col1 = plr;
			local col2 = plr;

			col1.fragment = "canCol"
			col2.fragment = "collisionobject"

			local myCol
			myCol = msg.url()
			myCol.fragment = "cannon"
			--go.delete(myCol)
			myCol.fragment = "cannonCol"
			msg.post(myCol, "disable")
			msg.post(col1, "enable")
			msg.post(col2, "enable")
			
			fire = false
			canFire = false

			msg.post(col2, "apply_force",{force = _pos,position = go.get_position()})

			go.cancel_animations(".", "euler.z")
			particlefx.play("#cannonFire")
			isActivate = false
			local plrUrS = plr
			plrUrS.fragment = "ball"
			msg.post("/ball#ball", "enableCol")
			
			--go.delete(".")
		end
	end
	
	--print(go.get_rotation())
end

function on_message(self, message_id, message, sender)
	if(message_id == hash("inCannon"))then
		
		isActivate = true;
		plr = sender
		go.set_position(go.get_position(),sender)
		go.animate(".", "euler.z", go.PLAYBACK_ONCE_FORWARD, minRot, go.EASING_LINEAR, 1)
		canFire = true
	end
end

function on_input(self, action_id, action)
	if(action_id == hash("interactive") and canFire == true)then
		if(action.released)then
			fire = true;
		end
	end
end

If you remove the comment on this line

go.delete(myCol)

then everything works, but if we use more than 2 objects, then everything breaks down again. Sorry in advance for the gif artifacts

One thing to note is that these variables will be shared by all instances of this script. Is this script used on multiple game objects?

Yes, the script hangs on several objects that are created through a factory, but shouldn’t declaring variables through local in a function protect the variables from being used on other objects? If this is not the case, then how do I protect the variables from this?

Correct, if the variables are local to the function, they’re not shared with another script.

local isActivate = false;
local rot;
local maxRot;
local minRot;
local fire = false;
local canFire = true
local plr;

local force = 1500

These local variables are not inside a function, so they will be shared by all instances that use the script.

If each instance of the script needs to use variables that are not shared, you have to use self:

self.is_activated = false

I try to avoid using self if I can, however, because it’s much slower than using local variables.

2 Likes

This variable is used in only one script. This script hangs on objects that are created through a factory. I wrote print () and I get these are the values ​​for the guns

url: [cannonSCene:/instance0#cannon]
url: [cannonSCene:/instance4#cannon]
url: [cannonSCene:/instance3#cannon]
url: [cannonSCene:/instance2#cannon]

I will try to edit the code a little later.

I rewrote code:

function init(self)
	msg.post(".", "acquire_input_focus")

	self.isActivate = false
	self.rot = go.get_rotation()
	self.maxRot = self.rot.z+45
	self.minRot = self.rot.z-45

	self.fire = false
	self.canFire = true
	self.plr = nil

	self.force = 1500
end

function update(self, dt)
	if(self.isActivate == true)then
		if(-math.deg(go.get_rotation().z) <= -21.9)then
			go.cancel_animations(".", "euler.z")
			go.animate(".", "euler.z", go.PLAYBACK_ONCE_FORWARD, self.minRot, go.EASING_LINEAR, 2)
		elseif(math.deg(go.get_rotation().z) <= -21.9)then
			go.cancel_animations(".", "euler.z")
			go.animate(".", "euler.z", go.PLAYBACK_ONCE_FORWARD, self.maxRot, go.EASING_LINEAR, 2)
		end

		if(self.canFire == true and self.fire == true)then
			local angle = go.get_rotation().z*2
			local _pos = vmath.vector3(go.get_position().x+50*math.cos(angle)*self.force,go.get_position().y+50*math.sin(angle)*self.force,0)

			self.myCol = nil
			self.myCol = msg.url()
			self.myCol.fragment = "cannonCol"
			msg.post(self.myCol, "disable")

			self.fire = false
			self.canFire = false
			self.isActivate = false
			
			go.cancel_animations(".", "euler.z")
			particlefx.play("#cannonFire")

			msg.post("/ball#ball", "enableCol")

			self.plrCol = self.plr
			self.plrCol.fragment = "collisionobject"
			msg.post(self.plrCol, "apply_force",{force = _pos,position = go.get_position()})

			--go.delete()
		end
	end
end

function on_message(self, message_id, message, sender)
	if(message_id == hash("inCannon"))then

		self.isActivate = true;
		self.plr = sender
		go.set_position(go.get_position(),sender)
		go.animate(".", "euler.z", go.PLAYBACK_ONCE_FORWARD, self.minRot, go.EASING_LINEAR, 1)
		self.canFire = true
	end
end

function on_input(self, action_id, action)
	if(action_id == hash("interactive") and self.canFire == true and self.isActivate == true)then
		if(action.released)then
			self.fire = true;
		end
	end
end

and thats worked

3 Likes