random spawner problem & other questions

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
The cube
Posts: 94
Joined: Tue Apr 23, 2013 6:09 pm
Location: Barren Desert

random spawner problem & other questions

Post by The cube »

Hi guys, my first post :oops:

so, i have a room in my dungeon, where you must block blobs flying from the other side of the room.
there are 3 spawners, and i would like it so that every 3 seconds one of them spawns a blob.
Here`s what i have scripted so far:
SpoilerShow
function randomspawn()
local randomnum = math.randomNumber(3)
if randomnum = 2 then
spawner_2:activate
else
if randomnum = 3 then
spawner_3:activate
else
if randomnum = 1 then
spawner_1:activate
end
end
end
This doesnt work :(

Ok, to the second problem. how do you set the traits of a character?
is it something like this:

local i = party:getChampion(1)
i.setTrait1("head_hunter")
end

The third problem: how do you add more characters to a 1 champion party?

these are the only problems i have encoutered so far, `cause there is so many awesome tutorials here!
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: random spawner problem & other questions

Post by JohnWordsworth »

Hi there, and welcome to the forums! Great to see that you are getting stuck in with Grimrock modding, it's a lot of fun.

1. SPAWNER SCRIPT

There are just a few problems in your script I think that are preventing it from working as expected. First of all, math.randomNumber does not exist, the method call should be math.random(3). This means that 'randomnum' is actually being assigned 'nil' and then nothing else is being called (if it even runs at all).

I would update your script as follows;
SpoilerShow

Code: Select all

function randomSpawn()
  local random = math.random(3);
  local spawnerId = "spawner_" .. random;
  local spawner = findEntity(spawnerId);

  if ( spawner == nil ) then 
    print("Could not find spawner:", spawnerId);
    return;
  end

  spawner:activate();
end
To break this down, you get a random number from the set [1, 2, 3] and then you generate the desired spawnerId by concatenating the string "spawner_" with the number you generated (so spawnerId will be either spawner_1, spawner_2 or spawner_3). Then you find the entity in the level based on that id and store a reference to that entity in the local variable 'spawner'. Next up we just do a check to make sure that the spawner actually exists. This prevents a crash if something goes wrong at release, and prints a friendly error message when debugging. If the spawner exists, we activate it.

IMPORTANT: Note that you must also have a timer object with an interval of '3' that connects to your script. Remember to set the initial state of the timer to 'running' (or set it running via a pressure plate when you enter the room) otherwise your script will never be called.

2. CHAMPION TRAITS

Instead of 'setting' and 'unsetting' traits, think of it more as a list of traits that each individual champion has. So, instead of using setTrait you would actually use the methods addTrait()/removeTrait()/hasTrait(). They are all described on this page here: http://www.grimrock.net/modding/scripting-reference/

You also need to call them using : instead of . like so...
SpoilerShow

Code: Select all

local champ = party:getChampion(1);
champ:addTrait("head_hunter");
3. ADDING MORE CHARACTERS

I don't know this one off the top of my head, but I think all 4 champions always exist in a party, it's just a case of enabling them or not. So, if you know that champion 2 is turned off, you should be able to do something like the following;
SpoilerShow

Code: Select all

local champ = party:getChampion(2);

if ( champ:getEnabled() == false ) then
  champ:setName("Some Name");
  -- Set Everything Else here
    champ:setEnabled(true);
end
Hope this helps. Be sure to check out the scripting reference: http://www.grimrock.net/modding/scripting-reference/, it's really useful!
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Post Reply