Re-use of animation pushable block?

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Re-use of animation pushable block?

Post by minmay »

minmay wrote:

Code: Select all

-- Move an object a certain distance on each axis with specified velocities,
-- canceling any existing movement *and canceling existing callbacks without calling them*.
-- gobj: object to move
-- move: table with info about how to move the object. Supports the following fields:
--   dx, dy, dz: distance to move each axis (can be nil)
--   vx, vy, vz: velocity to move each axis (sign should match corresponding distance)
--   callbackx, callbacky, callbackz: functions to call when finished moving on axis (can be nil), first argument passed will be the object
--   callbackxargs, callbackyargs, callbackzargs: additional arguments to pass to callbacks (can be nil)
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Re-use of animation pushable block?

Post by THOM »

Okay - most things are working. Now I am fighting with the details.

What I am trying to do is having a mine-cart moved. If there are railings on the tile in front: move. If not: don't move. If there is a monster on the tile: don't move.
Also there should be a special sound played, if moving is impossible.

My problem: I can't get it get to work, that the "don't move sound" is only played, when moving is impossible. It is played on every move.
Or it isn't played, but then I can't get it to work, that moving inside monsters is impossible.

:?

Here is my last code. (movesound correctly isn't played, but monster doesn't stop move)

Code: Select all

			onClick = function(self)
	     		   	for e in self.go.map:entitiesAt(self.go.x , self.go.y+1) do
					if e.name:find("ph_mine_rail_straight") then 
						for f in self.go.map:entitiesAt(self.go.x , self.go.y+1) do
							if f:getComponent("monster") == true then 
								--do nothing
							else
								gobj.script.moveObjectDistance(self.go,{dz=-3,vz=-1.6, })
								playSoundAt("minecart", party.level, self.go.x, self.go.y)
							end
						end
					break;
					else
						playSound("party_move_overloaded")
					end
				end
			end,
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Re-use of animation pushable block?

Post by minmay »

1. getComponent() will never return "true", it returns a component.
2. you should test if the square is blocked instead
3. you are playing the sound and calling moveObjectDistance [number of entities in the square]^2 times, you should only call them once for each move
4. you need to verify that the cart is not already moving before you try to move it, otherwise you're going to get bogus distances. use callbackz for this
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Re-use of animation pushable block?

Post by THOM »

I am still confused.
my new code works so far: cart is moved when railings are on the next tile and tile is not blocked.

Code: Select all

         onClick = function(self)
    			for e in self.go.map:entitiesAt(self.go.x , self.go.y-1) do
               			if e.name:find("ph_mine_rail_straight") and
					           not party.map:isBlocked(self.go.x, self.go.y-1, 0) then
                        				gobj.script.moveObjectDistance(self.go,{dz=3,vz=1.6, })
                        				playSoundAt("minecart", party.level, self.go.x, self.go.y)
					            break; 
					         end
				end
			end,
what I do not get to work is the fail-sound.
I used

Code: Select all

               	else
                  			    playSound("party_move_overloaded")
					end
over the "break", under the "break": result is alway the same - the sound is played but no movement happens...
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Re-use of animation pushable block?

Post by minmay »

your 'else' placement doesn't make sense. look again at the 'if' it belongs to:

Code: Select all

if e.name:find("ph_mine_rail_straight") and not party.map:isBlocked(self.go.x, self.go.y-1, 0) then
your 'else' block is going to run once for every entity found before the ph_mine_rail_straight entity if the square is not blocked, or once for every entity in the entire square if it is blocked.
what you wanted was:

Code: Select all

         onClick = function(self)
             for e in self.go.map:entitiesAt(self.go.x , self.go.y-1) do
               if e.name:find("ph_mine_rail_straight") then
                 if not party.map:isBlocked(self.go.x, self.go.y-1, 0) then
                   -- move the cart
                   gobj.script.moveObjectDistance(self.go,{dz=3,vz=1.6, })
                   playSoundAt("minecart", party.level, self.go.x, self.go.y)
                   -- we're done
                   return
                 else
                   -- square is blocked; fall through to failure sound
                   break
                 end
              end
            end
            -- this is reached if no mine rail is found OR if the square is blocked
            playSound("party_move_overloaded")
         end,
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
THOM
Posts: 1281
Joined: Wed Nov 20, 2013 11:35 pm
Location: Germany - Cologne
Contact:

Re: Re-use of animation pushable block?

Post by THOM »

Ah, thanks. Now it's pefect.

Sadly, things like "break" and "return" are still mysteries for me...
THOM formaly known as tschrage
_______________________________________________
My MOD (LoG1): Castle Ringfort Thread
My MOD (LoG2): Journey To Justice Thread | Download
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Re-use of animation pushable block?

Post by minmay »

"return" - returns from the function, if you didn't know this you really need to review what a function is. (Functions with multiple return points are arguably bad practice.)

"break" - when the "break" statement is reached, the current loop immediately exits (it's called break because it "breaks out" of the loop) and the code after it is executed. (Using "break" at all is even more arguably bad practice, but in this context it's not too bad.)
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Re-use of animation pushable block?

Post by Isaac »

THOM wrote:Ah, thanks. Now it's pefect.

Sadly, things like "break" and "return" are still mysteries for me...
Break exits the loop it is in. Return leaves the function entirely, and ~returns to the calling function; optionally with returned values given. A function that returns a value can be assigned in to a variable in another part of the script, or other script.

*Return statements must be followed by an end statement. If execution reaches the return statement, anything after/below the end statement is skipped/ignored.

Code: Select all

--snippet

function add(num1, num2)
 return num1 + num2
end

var = add(2,2)  
print(var)
--var then gets the value of 4

With break,

Code: Select all

for x = 1,2 do
	for y = 1, 100 do
		print(y)
		if y>5 then	
			break
		end
	end
end

--     prints 1 to 6 ~twice. When y increments to greater than 5, 
--	execution breaks out of the current loop; though it's still inside the x loop,
--	so it then enters the y loop again... and breaks at 6 again... then the x loop completes. 
Post Reply