Bobs scripting woes

TheBob
Posts: 76
Joined: Sat Jul 06, 2013 6:04 pm

Re: Bobs scripting woes

Post by TheBob »

Finally I have some time to continue scripting a bit. Yay!
Of course, I forgott the whole LUA syntax again in the meantme... nay!
And of course I'm immediately facing a problem that doesn't make any sense at all to me... oh wey!

It's fairly simple: The script I wrote (rather extensive, actually) started to produce assertion failures in SmartPtr.h as soon as I registered an event. Oh well, I thought, nothing ever works in the first go, so let's find out where the problem is!

I started to remove code in hope that I'll eventually get to the point where the error doesn't happen anymore, so I'd know what's wrong.
Strangely, I didn't reach this point. And I have nothing left to take away! No really, nothing. Here's what the file looks like at the moment:

Code: Select all

local onGameStart = function ()

end



local onEnterSystem = function(ship)

end


local onFrameChanged = function (body)

end

Event.Register("onFrameChanged", onFrameChanged)
Event.Register("onGameStart", onGameStart)
Event.Register("onEnterSystem", onEnterSystem)
And the error still happens. It's kind of hilarious, really.
If I don't register the events, it doesn't produce a crash (no matter whether in this state or in the full version, which are some 350 lines at the moment). But of course it doesn't do anythig then, either.

If I do register the events, it doesn't work even if I literally don't do anything.
Clearly there must be something ridiculously basic I'm overlooking. I hope someone has a good laugh at this and then tells me what I'm doing wrong...
walterar
Posts: 95
Joined: Sat Sep 14, 2013 4:48 pm

Re: Bobs scripting woes

Post by walterar »

Add this line to the beginning of the script:

Code: Select all

local Event = import("Event")
And look at this:

https://github.com/pioneerspacesim/pioneer/pull/1499

And this:

https://github.com/pioneerspacesim/pioneer/pull/2417
TheBob
Posts: 76
Joined: Sat Jul 06, 2013 6:04 pm

Re: Bobs scripting woes

Post by TheBob »

Right. Someone changed the rules while I wasn't looking :P

Thanks!
TheBob
Posts: 76
Joined: Sat Jul 06, 2013 6:04 pm

Re: Bobs scripting woes

Post by TheBob »

I'm kind of wondering here, shouldn't it be possible to arrange it so that the whole API can be imported with one call? Just saying it might help future scripters along a bit...
TheBob
Posts: 76
Joined: Sat Jul 06, 2013 6:04 pm

Re: Bobs scripting woes

Post by TheBob »

Uh, silght problem here again... I'm getting different values from a Rand object although it is always initialised with the same seed...

I have a global seed, that is generated at game start, then saved in the savegame and on subsequent loadings loaded from there, sice I want stuff to be differently distributed each game, but to be otherwise consistent in the game.
The seed for every system is taken from this global seed and the seed of the first body in a system, which is also consistent.

I have checked both the loaded global seed (aptly named "consistentRandomStuffSeed" and the seed I'm modifying it with. They are consistent, alright. None the less, the first value generated by the Rand object isn't. Here's what the relevant code looks like:

Code: Select all

	bodies = Game.system:GetBodyPaths()
	--sanity check in case there's a system without any actual bodies. you never know...
	if #bodies < 1 then
		return
	end

	local localSeed = (bodies[1]:GetSystemBody().seed + consistentRandomStuffSeed) / 2
	local starSystemRand = Rand:New(localSeed)

	local debugString = string.interp("local seed: {seed}", {seed = localSeed})
	print(debugString)

	local randomValue = starSystemRand:Integer(0, 5)

	debugString = string.interp("first generated value: {value}", {value = randomValue})
	print(debugString)

--afterwards follows a whole lot of attribute checking and generation which isn't really essential here, as the problem already manifests itself.
localSeed is consistent (the global seed also, if you're wondering, its just not shown here). However, I always get other values for randomValue. Anything I'm overlooking here?
lwho
Posts: 72
Joined: Thu Jul 04, 2013 9:26 pm
Location: Germany

Re: Bobs scripting woes

Post by lwho »

I just tried from the LUA console and always get consistent results. The following code always returns 4 for me:

Code: Select all

R = import("Rand")
r = R.New(42)
r:Integer(0, 5)
Are you sure that your localSeed is really a number and not a string? Could you please show the code, where consistentRandomStuffSeed is set!

This is the C++ function implementing Rand.New:

Code: Select all

static int l_rand_new(lua_State *l)
{
	int seed = int(time(0));
	if (lua_isnumber(l, 1))
		seed = lua_tointeger(l, 1);
	LuaObject<Random>::PushToLua(new Random(seed));
	return 1;
}
The only loophole I can see there is the check whether the passed argument from LUA is a number. If that check fails for some reason the time-based seed set at the first line will be used. Maybe it would be better to fail (i.e. return nil) if an argument was passed to New but was not a number.

EDIT: Hmm, even if I pass the number is passed as a string (i.e. "42" instead of 42) it is detected as a number and correctly converted to an integer.
TheBob
Posts: 76
Joined: Sat Jul 06, 2013 6:04 pm

Re: Bobs scripting woes

Post by TheBob »

The only loophole I can see there is the check whether the passed argument from LUA is a number. If that check fails for some reason the time-based seed set at the first line will be used.
Weeeell... my seed has the potential of having a period (since it's formed by a division by 2). That might do the trick here, might it not?
And for some weird reason, math.floor doesn't do anything about it (no Idea what's wrong there... I'm flooring several values in that code, but when I print them to the console they still aren't integers. Any suggestions what that could be about (I'm not getting any errors, it's just not doing anything) would be welcome, or another way to do it (No such thing as casting to integer in LUA, it would seem... I'm starting to dislike that bytey-wimey wibbly-wobbly ball of non-declared datatypes where I never know for sure what I'm working with...)
Could you please show the code, where consistentRandomStuffSeed is set!

Code: Select all

local consistentRandomStuffSeed
local loaded_data

local onGameStart = function ()
	--creating seed if new game
	if not loaded_data then
		consistentRandomStuffSeed = Engine.rand:Integer()
	else
		consistentRandomStuffSeed = loaded_data.consistentRandomStuffSeed
		loaded_data = nil
	end
end

local serialize = function ()
	return {consistentRandomStuffSeed = consistentRandomStuffSeed}
end

local unserialize = function (data)
	loaded_data = data
end

Serializer:Register("RandomStuff", serialize, unserialize)
lwho
Posts: 72
Joined: Thu Jul 04, 2013 9:26 pm
Location: Germany

Re: Bobs scripting woes

Post by lwho »

I made my test also with various floating point numbers (42.1, 42.9, 42.9999999) and they all seemed to be truncated to 42 properly. So, if I think, if the integer part of your localSeed is always the same it should work.

I don't see a problem in your code, but I'll try if I can reproduce it with your code snippets.
lwho
Posts: 72
Joined: Thu Jul 04, 2013 9:26 pm
Location: Germany

Re: Bobs scripting woes

Post by lwho »

I can reproduce it with your code. And indeed the lua_isnumber does not trigger. I don't know why, yet.

In the meantime: Could you please open a github issue, I'll attach my information there.
lwho
Posts: 72
Joined: Thu Jul 04, 2013 9:26 pm
Location: Germany

Re: Bobs scripting woes

Post by lwho »

Stop!

I see the error: It must be Rand.New not Rand:New. The latter passes the Rand table as first parameter to New and localSeed as the second
Post Reply