Ask a simple question, get a simple answer

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: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Isaac wrote:
minmay wrote:
Isaac wrote:Loops are useful for their incrementing value, and this pair of loops only runs once each for fixed values that could be static variables.
Uh, each loop runs twice, for a total of four iterations of the inner loop.
Does it? Maybe I missed it; I thought the loops were using stepping 2, and that a loop like 16,18,2 would only run once, and be at 18. Image
It would run twice. Once with a value of 16, and once with a value of 18. Like how

Code: Select all

for i = 1,3 do
runs 3 times.
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
Duncan1246
Posts: 404
Joined: Mon Jan 19, 2015 7:42 pm

Re: Ask a simple question, get a simple answer

Post by Duncan1246 »

minmay wrote:
Isaac wrote:Loops are useful for their incrementing value, and this pair of loops only runs once each for fixed values that could be static variables.
Uh, each loop runs twice, for a total of four iterations of the inner loop.
Isaac wrote:
minmay wrote:
Isaac wrote:Loops are useful for their incrementing value, and this pair of loops only runs once each for fixed values that could be static variables.
Uh, each loop runs twice, for a total of four iterations of the inner loop.
Does it? Maybe I missed it; I guess I thought that the loops were using stepping 2, and that a loop like 16,18,2 would only run once, and be at 18. Image

*Yep, it runs twice, and I should have known it. It shows the downside of reading and posting scripts at 4AM. :D
Off by 1 errors have their own special sting to them; when you realize you've made one.
Yes, intervals and loops are often confusing, and yes I hesitate a moment before building such a poor double loop, but at least it's shorter to write...Not a little bonus in a script rather long already!
The purpose is:
  • Putting 4 power_gem_item on 4 pedestals start a process:
    The power_gem_item raise (becoming power_gem),pedestals disappears
    A beacon emerge from underwater
    Power_gems merge together in front of the beacon
    An essence is made(with generic animation)
The trick is that I use a beacon_furnace without model and obstacle components, and a beacon with the same surface and clickable components as the standard furnace... It's works :mrgreen:
Here it is:
SpoilerShow

Code: Select all

goout_timer.timer:stop()

function surfaceContains6(surface, item)
   for _,i in surface:contents() do
      if i.go.name == item then
         return true
	  else return false	
      end
   end
end
function gemstarter()
	local gemcount=0
	local pedeslist={pedestal_17,pedestal_18,pedestal_19,pedestal_20}
	for i =1,4 do
		if surfaceContains6(pedeslist[i].surface,"power_gem_item")	then
		gemcount=gemcount+1
		end
	end
	if gemcount==4 then
		goout_timer.timer:start()
		spawn("furnace_explosion",2,4,17,0,0) 
		for i=1,4 do
				for _,j in pedeslist[i].surface:contents() do
				j.go:destroy()
				end
				pedeslist[i]:destroy()
				for px=2,4,2 do
					for py=16,18,2 do
					spawn("power_gem",2,px,py,0,0)
					end
				end
		end
	end
end
	
function raisebeacon()
	
	local y = beacon_water_adhoc_1:getWorldPositionY()
	local level = beacon_water_adhoc_1.level
	local px = beacon_water_adhoc_1.x
	local py = beacon_water_adhoc_1.y
	local face = beacon_water_adhoc_1.facing

	y = y + 0.04

	if y>-0.5 then 
	beacon_water_adhoc_1:setPosition(px,py,face,0,level)
	castle_bridge_130:setPosition(px,py,face,0,level)
	castle_bridge_154:setPosition(1+px,py,face,0,level)
	end

	if y>0 then -- Stop the elevator
	y=0
	goout_timer.timer:stop()
	prepareessence()
	delayedCall(self.go.id,10.0,"makeessence")
	end

	beacon_water_adhoc_1:setWorldPositionY(y)
	castle_bridge_130:setWorldPositionY(y)
	castle_bridge_154:setWorldPositionY(y)

for obj in castle_bridge_154.map:entitiesAt(1+px,py) do
		if obj.gravity then -- Only move things that would be lifted
			if (math.abs(obj:getWorldPositionY()-y)<0.5) then
			obj:setWorldPositionY(y+0.04)
			end
		end
	end
end

function prepareessence()
	for px=2,4,2 do
			for py=16,18,2 do
				for e in party.map:entitiesAt(px,py) do	
					if e.name=="power_gem" then
					e:setPosition(3,17,1,0,party.level)
					end
				end
			end
	end
end

function makeessence()
	for  e in party.map:entitiesAt(3,17) do	
		if e.name=="power_gem" then
		e:destroy()
		end
	end
	for i=1,4 do
		beacon_furnace_4.surface:addItem(spawn("power_gem_item").item)
	end
end
You see that it's a patchwork, I have a bunch of guys to credit in my mod.
If you see better ways to do that, or issues hidden, thanks to point them, I am a newbee in Lua scripting
Duncan
The Blue Monastery (LOG1)
download at:http://www.nexusmods.com/grimrock/mods/399/?

Finisterrae(LOG2)
download at:http://www.nexusmods.com/legendofgrimrock2/mods/61/?
User avatar
BuzzJ
Posts: 160
Joined: Sat Jan 12, 2013 4:06 pm

Re: Ask a simple question, get a simple answer

Post by BuzzJ »

minmay wrote:
petri wrote:
BuzzJ wrote:Is there a simple way to make an object or several spawn on the same tile as the party when an (here unspecified) event occurs? Where defining the location of the party is not necessary?
Something like this perhaps?

Code: Select all

party.party:spawn("dagger")
I think you meant

Code: Select all

party:spawn("dagger")
Hi, when I use this as part of

Code: Select all

defineObject{
	name = "scifi_superposed",
	baseObject = "base_item",
	components = {
		{
			class = "Model",
			model = "mod_assets/models/items/scifi_superposed.fbx",
		},
		{
			class = "Item",
			uiName = "Superposed Shotgun",
			description = "An elagant style of double barrel shotgun.",
			gfxAtlas = "mod_assets/textures/gui/scifi_items_1.tga",
			gfxIndex = 0,
			gfxIndexPowerAttack = 1,
			impactSound = "impact_blunt",
			secondaryAction = "reload",
			weight = 1.5,
			traits = { "firearm" },
		},
		{
			class = "FirearmAttack",
			attackPower = 22,
			cooldown = 1.5,
			attackSound = "gun_shot_large",
			ammo = "shotshell",
			clipSize = 2,
			jamChance = 12,
			requirements = { "firearms", 1 },
		},
		{
			class = "ReloadFirearm",
			uiName = "Reload",
			name = "reload",
			requirements = { "firearms", 1 },
			party:spawn("scifi_spentshotshells"),
			gameEffect = "Reloads both chambers of the shotgun."
		},
	},
Grimrock2 dungeon editor kicks me out the following error: "attempt to index global 'party' (a nil value)"

does this mean I need to define what the party is somewhere? If so, how?

edit: same result on either party.party:spawn("dagger") or party:spawn("dagger")
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Uh...did you mean to put that in an onAttack hook? Because what you're trying to do right now is put the return value of party:spawn("scifi_spentshotshells") as the value for key 1 in the ReloadFirearm component. Which wouldn't make sense even if it didn't. And it can't possibly not crash, because how could the party possibly exist before object definitions are even run? The party would have to be created before the party object is even defined!
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
BuzzJ
Posts: 160
Joined: Sat Jan 12, 2013 4:06 pm

Re: Ask a simple question, get a simple answer

Post by BuzzJ »

minmay wrote:Uh...did you mean to put that in an onAttack hook? Because what you're trying to do right now is put the return value of party:spawn("scifi_spentshotshells") as the value for key 1 in the ReloadFirearm component. Which wouldn't make sense even if it didn't. And it can't possibly not crash, because how could the party possibly exist before object definitions are even run? The party would have to be created before the party object is even defined!
No, my intent was to spawn scifi_spentshotshells at the party's feet at the conclusion of the ReloadFirearm action. How would I delay running the items script until after the party object is defined? Or do I need to define it?
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

The function call you have in your code will execute when the item definition is executed, which will be when the dungeon is first loaded. To do what you describe, you need to have an ItemActionComponent that reloads the firearm (not a ReloadFirearmComponent because that doesn't support onAttack) and put the party:spawn() call inside its onAttack hook.
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: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Duncan1246 wrote:...
function raisebeacon()
...
You should be able to get a smoother rise by halving the increments, (or perhaps quartering); while reducing the timer interval.

*My test: https://www.dropbox.com/s/fm8dmyb4tmxro ... n.avi?dl=0
(Using different scripting, though.)
User avatar
Duncan1246
Posts: 404
Joined: Mon Jan 19, 2015 7:42 pm

Re: Ask a simple question, get a simple answer

Post by Duncan1246 »

Isaac wrote:
Duncan1246 wrote:...
function raisebeacon()
...
You should be able to get a smoother rise by halving the increments, (or perhaps quartering); while reducing the timer interval.

*My test: https://www.dropbox.com/s/fm8dmyb4tmxro ... n.avi?dl=0
(Using different scripting, though.)
I agree, in initial version I have a quartered step, but my beacon is on elevation -7, so it was too long wait for the player. I tried first to lower interval on the timer, but it seems that it has no effect under 0.05. I couldn't find if timers has a minimum interval value?
Duncan
The Blue Monastery (LOG1)
download at:http://www.nexusmods.com/grimrock/mods/399/?

Finisterrae(LOG2)
download at:http://www.nexusmods.com/legendofgrimrock2/mods/61/?
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Duncan1246 wrote:
Isaac wrote:
Duncan1246 wrote:...
function raisebeacon()
...
You should be able to get a smoother rise by halving the increments, (or perhaps quartering); while reducing the timer interval.

*My test: https://www.dropbox.com/s/fm8dmyb4tmxro ... n.avi?dl=0
(Using different scripting, though.)
I agree, in initial version I have a quartered step, but my beacon is on elevation -7, so it was too long wait for the player. I tried first to lower interval on the timer, but it seems that it has no effect under 0.05. I couldn't find if timers has a minimum interval value?
Duncan
Can they see it the whole way up, as it travels?
Or...perhaps have the increments increase mid-way, and reduce again, closer to the top.
minmay
Posts: 2780
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

Timers can only activate once per frame. What you want to do is use an extremely short interval like 0.001, which will trigger every frame, then scale the world position change with Time.deltaTime().
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.
Post Reply