How i can make snake? (SOLVED)

HI, I have a js code that draws and moves a snake, I want to transfer it to defold, but I have some difficulties…

/main/scripts/player.script:27: bad argument #2 to 'set_position' (url expected, got userdata)
stack traceback:
  [C]:-1 : in function set_position
  /main/scripts/player.script:27: in function </main/scripts/player.script:23>

code on JS:

let x = [],
    y = [],
    segNum = 20,
    segLength = 18;

for (let i = 0; i < segNum; i++) {
	x[i] = 0;
	y[i] = 0;
}

function setup() {
	createCanvas(710, 400);
	strokeWeight(9);
	stroke(255, 100);
}

function draw() {
	background(0);
	dragSegment(0, mouseX, mouseY);
	for (let i = 0; i < x.length - 1; i++) {
		dragSegment(i + 1, x[i], y[i]);
	}
}

function dragSegment(i, xin, yin) {
	const dx = xin - x[i];
	const dy = yin - y[i];
	const angle = atan2(dy, dx);
	x[i] = xin - cos(angle) * segLength;
	y[i] = yin - sin(angle) * segLength;
	segment(x[i], y[i], angle);
}

function segment(x, y, a) {
	push();
	translate(x, y);
	rotate(a);
	line(0, 0, segLength, 0);
	pop();
}

code on Defold:

local mousePos = vmath.vector3(0,0,0)

local x = {}
local y = {}
local segNum = 20

local seg = "#segm" 
local obj = {}

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

	for i = 0, segNum, 1 do
		x[i] = 0
		y[i] = 0
		local onj = factory.create("/go#factory", vmath.vector3(x[i], y[i], 0), nil, {}, 1.0)
		table.insert(obj, msg.url(onj))
		print(obj[i]);
	end
	
end

function update(self, dt)
	dragSegment(0,mousePos.x,mousePos.y)
	for i = 0, #x-1, 1 do
		dragSegment(i+1,x[i],y[i])
		go.set_position("/instance0",vmath.vector3(x[i],y[i],0))
	end
end

function dragSegment(i,xin,yin)
	local dx = xin-x[i]
	local dy = yin-y[i]
	local angle = math.atan2(dy,dx)
	x[i] = xin - math.cos(angle) * 1
	y[i] = yin - math.sin(angle) * 1
	segment(x[i],y[i],a)
end

function segment(_x, _y, a)
	
end

function on_input(self, action_id, action)
	mousePos = vmath.vector3(action.x, action.y, 0)
end

how it was supposed to work

Thanks to everyone who read this. I will be very grateful to everyone who will help.

go.set_position() takes parameters as follows:

go.set_position(position,[id])

You seem to have it the other way around.

Furthermore, it looks like you are trying to hardcode the address of a dynamically generated game object name? If the script intends to move the game object that the script is attached to, you can ignore addressing entirely and just call:

go.set_position(position)

Actually, looking at the example you linked (and the fact you’re running a for loop) makes me think you are trying to move multiple segments in different game objects. If they are created through a factory, you need to store the reference to the game object when they are created (factory.create() returns this). If you have created the game objects manually (placing them in the editor), then the easiest way of finding the address of the game object in question is by:

print(msg.url())

More on addressing in Defold:

4 Likes

thanks

There’s also a snake tutorial for defold :wink:
Might be a good read if you’re interested!

1 Like